is there possible to resolve this star pyramid [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Today, i have trying to resolve a small star pyramid :
Input:
5 1
Output:
*
**
***
****
Code:
x = 1
y = 0
m, e = map(int, raw_input().split())
while x < m:
print "\n" * y, "*" * e
m -= 1
e += 1
I did that but there is a better solution?? Thanks =)

I think this can be solved more easily:
stop, first = map(int, raw_input().split())
for i in range(stop - 1):
print '*' * (i + first)

just for fun >:)
class c:
def __init__(s,m,e):
s.e , s.m = sorted([e, m])
s.r = 42
def __iter__(s):
return s
def next(s):
if s.m < s.e:
t = "".join(chr(s.r) for _ in range(s.m))
s.m += 1
return t
else:
raise StopIteration
print "\n".join(c(*map(int,raw_input().split())))

n = int(raw_input())
for i in range(n): print "*"*i
This appears to do what your program intends to do, however I can't quite tell because of the issues I raised in my comment above.

Related

Extract n first consecutive numbers from a string in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Let's consider this string :
st = 'text1text6253text'
How could I please extract the two first consecutive figures ?
Expected output :
62
You can either use regex with \d{2}and return that, or go over the string:
st = 'text1text6253text'
for i in range(len(st)-1):
if st[i].isdigit() and st[i+1].isdigit():
print(st[i]+st[i+1])
break
import re
def find_con(n, s):
result = re.search('\d{%s}'%n, s)
return result.group(0) if result else result
st = 'text1text6253text'
print(find_con(2, st))
st = 'text1text6253text'
lst = list(st)
lst2 = []
for i,v in enumerate(lst):
if lst[i].isdigit() and lst[i+1].isdigit():
lst2.append(lst[i])
lst2.append(lst[i+1])
ans = int(lst2[0] + lst2[1])
print(ans)
Thanks to your answers, I built a general function that I propose you below :
def extract_n_consecutive_numbers(st,nb):
for i in range(len(st)-nb+1):
is_numeric = True
for j in range(nb):
is_numeric = is_numeric & st[i+j].isdigit()
if is_numeric :
output = ""
for j in range(nb):
output += st[i+j]
return output
return ""
Example :
extract_n_consecutive_numbers('text1text6253text',2)
Out[1]: 62

How can I do this effectively? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This problem:
Input: 123456
Result:
1+2+3+4+5+6 = 21
2+1 = 3
Return: 3
This is my code:
num = input()
print(sum(list(map(int, list(num)))))
I don't know how to do until it is 1 digit.
Try this (example in IPython):
In [1]: s = '123456'
Out[1]: '123456'
In [2]: digits = [int(j) for j in s]
Out[2]: [1, 2, 3, 4, 5, 6]
In [3]: s = str(sum(digits))
Out[3]: '21'
Repeat steps 2 and three until len(s) == 1.
One way:
while len(str(ans))>1:
ans = sum(map(int, str(ans)))
Full code:
num = 45637
ans = num
while len(str(ans))>1:
ans = sum(map(int, str(ans)))
print(ans)
Output for input 45637:
7
You can try this:
s = input()
while(len(s)>1):
s = str(sum(list(map(int,s))))
One way to do it using sum(), list comprehension and recursion,
def simulated_sum(input):
"""This is a recursive function
to find the simulated sum of an integer"""
if len(str(input)) == 1:
return input
else:
input_digits = [int(x) for x in str(input)]
latest_sum = sum(input_digits)
return simulated_sum(latest_sum)
input = int(input('Enter a number'))
print(simulated_sum(input))
DEMO: https://rextester.com/WCBXIL71483
Is this what you want? (instructions unclear):
def myfunction(number):
total = 0
answertotal = 0
for i in str(number):
total += int(i)
for i in str(total):
answertotal += int(i)
return answertotal
myfunction(123456)
This function returns 3

python not understanding simple math question [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am making mortgage calculator, I have provided information for variables p, i, n, but get error in the equation.
p[i(1 + i) ^ n] / [(1 + i) ^ n – 1]
What you shared is not valid python code. Here is an example of code that will accomplish what you are asking:
# define function:
def CalculateMortgage(p, i, n):
# calculate numerator:
numerator = p * (i *(1+i) ** n)
# calculate denominator:
denominator = ((1+i) ** n - 1)
# calculate mortgage:
mortgage = numerator/denominator
# return result:
return mortgage
# set variables:
p = 1
i = 1
n = 1
# call function:
mortgage = CalculateMortgage(p, i, n)
# print result:
print('Your mortgage is: ' + str(mortgage))

Project Euler 55 - Answer Only Off By 3 (strange!) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was solving this problem (found at http://projecteuler.net/problem=55) and I could not get it right so I searched for the answer.
It seems the answer my code is giving is only off by 3 (mine: 246 , right: 249).
It would be nice for someone to to spot the mistake. I have been trying for 3 hours now...
Here is my code:
from time import time
def rev(x):
return int(str(x)[::-1])
def Palindrome(x):
if x == rev(x): return True
else : return False
def test(x):
steps = 0
while True :
if not Palindrome(x):
steps += 1
else:
return False
if steps > 50 :
return True
x += rev(x)
def main():
starttime = time()
lychrel = 0
for i in range(1,10000):
if test(i) : lychrel += 1
elapsed = time() - starttime
print "The answer is %d found in %f seconds" %(lychrel,elapsed)
if __name__ == "__main__":
main()
Thanks in advance!
From the description at http://projecteuler.net/problem=55
Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994.
Your code does not recognize 4994 as a Lychrel number.

How to make a binomial expander in python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example
(x + y)^4 = x^4 +(4x^3)y + (6x^2)y^2 + + 4xy^3 + y^4
I'm using python 3.3.2 and I don't know where to start, I need a little guidance. I'm not asking for the answer to it, just the general steps to make this program work. I've done a few other programs before, and this is probably pushing the limit on what I can do.
SymPy already do that for you:
>>> import sympy
>>> x, y = sympy.symbols("x y")
>>> formula = (x + y) ** 4
>>> formula
(x + y)**4
>>> formula.expand()
x**4 + 4*x**3*y + 6*x**2*y**2 + 4*x*y**3 + y**4
If you need a visualization as a string with "^", you can do:
>>> str(formula.expand()).replace("**", "^")
'x^4 + 4*x^3*y + 6*x^2*y^2 + 4*x*y^3 + y^4'
The code below can be found on this site.
Usage
python binomialexpansion.py 3
Output
x^3 + 3x^2y + 3xy^2 + y^3
Code
import sys
power = int(eval(sys.argv[1]))
strpower = str(power)
coeffs = []
if power == 0:
print 1
exit()
if (power+1) % 2 == 0:
turningp = (power+1)/2
counter = 1
else:
turningp = (power+2)/2
counter = 2
for i in range(1, power+2):
if i == 1:
sys.stdout.write("x^"+strpower+" + ")
coeffs.append(1)
continue
if i == power+1:
print "y^"+strpower,
coeffs.append(1)
break
if i > turningp:
co = coeffs[turningp-counter]
counter = counter+1
else:
co = ((power-(i-2))*coeffs[i-2])/(i-1)
coeffs.append(co)
sys.stdout.write(str(co))
if power-(i-1) == 1:
sys.stdout.write("x")
else:
sys.stdout.write("x^"+str(power-(i-1)))
if i-1 == 1:
sys.stdout.write("y ")
else:
sys.stdout.write("y^"+str(i-1)+" ")
sys.stdout.write("+ ")

Categories

Resources