def fib(f, N):
f[1] = 0
f[2] = 1
for i in range(3, N + 1):
f[i] = f[i - 1] + f[i - 2]
def fiboTriangle(n):
N = n * (n + 1) // 2
f = [0]*(N + 1)
fib(f, N)
fiboNum = 1
for i in range(n):
for j in range(n-i):
print(end=" ")
for j in range(i):
print(f[fiboNum], end=" ")
fiboNum+=1
print()
for i in range(n,0,-1):
for j in range(n-i):
print(end=" ")
for j in range(i):
print(f[fiboNum], end=" ")
fiboNum+=1
print()
n = 3
fiboTriangle(n)
this is my code i get this output:
0
1 1
2 3 5
Traceback (most recent call last):
File "/home/ali/Python/emipro.py", line 38, in
fiboTriangle(n)
File "/home/ali/Python/emipro.py", line 34, in fiboTriangle
print(f[fiboNum], end=" ")
IndexError: list index out of range
but i want
diamond shape pattern in output
After receiving your comment I went back to reviewing the type of numbers you were attempting to create in a diamond format. I got a diamond with the numeric sequence you noted by changing:
N = n * (n + 1) // 2
to:
N = n * (n + 1)
Here is the full code.
def fib(f, N):
f[1] = 0
f[2] = 1
for i in range(3, N):
f[i] = f[i - 1] + f[i - 2]
def fiboTriangle(n):
N = n * (n + 1) # Changed this equation
f = [0]*(N + 1)
fib(f, N)
fiboNum = 1
for i in range(n):
for j in range(n-i):
print(end=" ")
for j in range(i):
print(f[fiboNum], end=" ")
fiboNum+=1
print()
for i in range(n,0,-1):
for j in range(n-i):
print(end=" ")
for j in range(i):
print(f[fiboNum], end=" ")
fiboNum+=1
print()
n = 3
fiboTriangle(n)
That produced the following diamond.
craig#Una:~/Python_Programs/Fibonacci$ python3 Fibonacci.py
0
1 1
2 3 5
8 13
21
Give that a try.
Related
I am trying to produce a balanced triangle but I got halfway there. I want the result in this format
1
12
123
1234
12345
1234
123
12
1
Here Is the code I have so far:
def numpat(n):
num = 1
for i in range(0, n):
num = 1
for j in range(0, i+1):
print(num, end=" ")
num = num + 1
print("\r")
n = 7
numpat(n)
First thing, try to use code tags in the questions because otherwise the spacing will not be clear.
Anyway you should conver num to a string in order to add digits to the end.
Try something like:
def numpat(n):
num = ""
for j in range(1, n+1):
num += str(j)
print(num, end=" ")
print("\r")
for j in range(len(num)):
num = num[0:-1]
print(num, end=" ")
print("\r")`
HOW IT SHOULD LOOK ALIKE
I want pattern to be like this, but I couldn't come up with the loops ((
rows = int(input("number of rows: "))
space = rows - 1
n = 5
for i in range(0, rows):
for j in range(0, space):
print("", end=" " * n)
for j in range(0, i + 1):
print("*", end=" " * n)
space = space - 1
print()
space = 1
for i in range(rows - 1, 0, +1):
for j in range(space, 0):
print(" ", end=" ")
for k in range(0, i - 1):
print("*", end=" " * n)
space = space + 1
print()
rows = int(input("number of rows: "))
width = int(input("width of your window: "))
space = 9 # space between stars
for i in range(rows):
string = " " * (i % space)
for j in range(width):
if j % space == 0:
string += "*"
else:
string += " "
print(string)
I was given this riddle :
My first idea was to solve it using python. (Later I realized algebra would have been much easier).
Here was my initial code:
a = 0
b = 0
c = 0
d = 0
for a in range(100):
for b in range(100):
for c in range(100):
for d in range(100):
if float(a + b)/4 == 8.0:
if float(a + c)/4 == 13.0:
if float(c - d)/4 == 6.0:
if float(b + d)/4 == 8.0:
print(float(a + b)/4, end=" ")
print(float(a + c)/4, end=" ")
print(float(c + -d)/4, end=" ")
print(float(b + d)/4, end=" and values are:")
print(a/4, end=" ")
print(b/4, end=" ")
print(c/4, end=" ")
print(d/4)
It worked, but obviously this is not very readable.
Looking on stack overflow leads to the solution being recursive functions, however I do not know how exactly to apply this because there are four different mathematical calculations.
For example this is what I tried at first:
val = [0, 0, 0, 0]
def recursive_solution(section):
for val[section] in range(100):
for val[section+1] in range(100):
if section == 0:
if float(val[section])/4 + float(val[section + 1])/4 == 8:
recursive_solution(1)
recursive_solution(0)
But this obviously does not work, as the second index is added to the fourth index, and one of the operations is a subtraction, and you can't add the fourth index to the fifth one because the fifth one doesn't exist.
Here's what I would do from your code: use a cartesian product and and statements:
from collections import product
for a, b, c, d in product(*([range(100)] * 4))
if (
float(a + b) / 4 == 8.0
and float(a + c) / 4 == 13.0
and float(c - d) / 4 == 6.0
and float(b + d) / 4 == 8.0
):
print(float(a + b) / 4, end=" ")
print(float(a + c) / 4, end=" ")
print(float(c + -d) / 4, end=" ")
print(float(b + d) / 4, end=" and values are:")
print(a / 4, end=" ")
print(b / 4, end=" ")
print(c / 4, end=" ")
print(d / 4)
Task:
Write a program that reads from the standard input and will return the:
sum
difference
product
of all elements in the given list.
Input
An integer n (1 <= n <= 500) that denotes the number of elements in the list. The following n integers are the next elements of the list.
Output
Three integers:
sum
difference
product
of all the elements of the list.
And these are my ideas, but I still get a mistake
from sys import stdin
def Simple_list_arithmetic():
print("Enter a positive number: ")
n = int(stdin.readline())
l = []
if n >= 1 and n <= 500:
for i in range(1, n+1):
l.append(i)
#print(l)
suma = 0
for add in range(0, len(l)):
suma = suma + l[add]
print(suma)
#return suma
difference = 2
for substract in range(0, len(l)):
difference = difference - l[substract]
print(difference)
#return difference
product = 1
for increase in range(0, len(l)):
product = product * l[increase]
print(product)
return suma, difference, product
else:
print("Wrong number.")
still wrong
suma = 0
q = [suma + l[add] for add in range(0, len(l))]
print(sum(q))
difference = 2
w = [difference - l[substract] for substract in range(0, len(l))]
print(list(w))
product = 1
e = [product * l[increase] for increase in range(0, len(l))]
print(sum(e))
still wrong
if n >= 1 and n <= 500:
for x in range(1, n+1):
print(x)
print("\n")
suma = 0
for add in range(1, n+1):
suma = suma + add
print(suma)
difference = 2
for substract in range(1, n+1):
difference = difference - substract
print(difference)
product = 1
for increase in range(1, n+1):
product = product * increase
print(product)
with map() and lambda still wrong
t = list(map(lambda add: suma + l[add], range(0, len(l))))
#return sum(t)
y = list(map(lambda increase: increase * l[increase], range(0, len(l))))
#print(y[-1])
#print(y)
try this:
x = int(input("Enter a positive number: "))
if x >= 1 and x <= 500:
sum = 0
for i in range(x,501):
sum +=i
print(sum)
diff = 2
for i in range(x,501):
diff -= i
print(diff)
prod = 1
for i in range(x, 501):
prod *= i
print(prod)
why the while loop is executing more times than expected in printing a pascal triangle?
every time the while loop is executed x is incremented by 1 whereas n remains the same
I just started learning python
please help
memo = {0:1}
def fac(n):
if n not in memo:
memo[n] = n*fac(n-1)
return memo[n]
else:
return memo[n]
def pascal(x, space):
while(x <= n):
for j in range(space):
print(" ", end = "")
for i in range(0, x+1):
print ( int(fac(x)/(fac(i)*fac(x-i))), end = " " )
print("\n", end = "")
x += 1
space -= 1
pascal(x, space)
n = eval(input())
space = n
x = 0
pascal(x, space)
You are using two methods to iterate through the numbers in the pascal function, a while loop, and a recursive call. You just need one of them.
Keeping the while loop:
def pascal(x, space):
while(x <= n):
for j in range(space):
print(" ", end = "")
for i in range(0, x+1):
print ( int(fac(x)/(fac(i)*fac(x-i))), end = " " )
print("\n", end = "")
x += 1
space -= 1
Keeping the recursive call, turning the while into an if:
def pascal(x, space):
if(x <= n):
for j in range(space):
print(" ", end = "")
for i in range(0, x+1):
print ( int(fac(x)/(fac(i)*fac(x-i))), end = " " )
print("\n", end = "")
x += 1
space -= 1
pascal(x, space)
Given 3 as input, both versions print the following:
1
1 1
1 2 1
1 3 3 1