unable to understand error in nested list comprehension - python-3.x

if __name__ == '__main__':
x = int(input())
y = int(input())
z = int(input())
n = int(input())
print[[i, j, k] for i in range( x + 1) for j in range( y + 1) for k in range( z + 1) if( i + j + k ) != n ]
The above code shows Syntax Error: invalid syntax
please help me understanding the error

You are missing parenthesis for print. Call it like so: print(...).

Related

fibonacci diamond shape pattern in python

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.

Why does my code stop after the first test case? (Problem -- https://www.hackerearth.com/practice/codemonk/)

Please review my code for the problem given in the title, it stops after showing correct output for the first test case and shows Runtime error if I put t > 1 in Hackerearth IDE.
t = int(input())
while t > 0:
t = t - 1
n, k = map(int, input().split())
A = [int(i) for i in input().split()]
p = []
if k > n:
k = k - n
elif k == n:
k = 0
else:
k = k
for j in range(k):
x = A.pop()
p.append(x)
p = p[::-1]
for j in range(len(A)):
p.append(A[j])
print(*p)

How to write a function using for and if

I'm trying to write a code for calculating the number of factors for an arbitrary integer number but unfortunately when I run that I receive a false answer
I have tried for loop without defining function in this case and I got the result.Contrary to that, when I define a function I can't see the proper result
r = 0
def factor(a):
global r
for i in range(1, a + 1):
if a % i == 0:
r += 1
return r
a = int(input())
factor(a)
for example 18 has 6 factors but I receive just 1.
Use print to check your code. Indentation in Python matters. Also, global is not needed.
def factor(a):
r = 0
for i in range(1, a + 1):
if a % i == 0:
print('i', i)
r += 1
return r
a = int(input())
print(factor(a))
It was an indentation problem: the function should only return after the loop has finished iterating.
r = 0
def factor(a):
global r
for i in range(1, a + 1):
if a % i == 0:
r += 1
return r
a = 18 # int(input())
factor(a)
output:
6

List Index out of range, trying to get the [-1] position

I've tested to see if the list is empty and I'm sure it's not empty. So why this error continues?
The code is to find the smallest amount of factorial to a sum is equal to N.
def fat(n):
if n == 0 or n == 1:
return 1
else:
return n * fat(n - 1)
n = int(input())
ns = [x+1 for x in range(n)]
listaFat = []
l = []
for x in ns:
if fat(x) < n:
listaFat.append(x)
maior = max(listaFat)
listaFat.remove(maior)
print(listaFat)
print(len(listaFat))
while (fat(listaFat[-1]) + fat(maior)) < n:
l.append(listaFat[-1])
if len(listaFat) > 0:
listaFat.remove(listaFat[-1])
continue
else: break
print(l)
print(listaFat)

Trouble with variables (Python)

I'm a newbie with programming and I'm in trouble with this code:
def supercalcx(a, b):
n = a
while a <= b:
n = n * a
a = a + 1
print(n)
The IDE give me the error: "TypeError: can't multiply sequence by non-int of type 'str'", but I'm sure the inputs are ints or floats, can anyone explain me the problem. Thanks !
This function works:
>>> def supercalcx(a, b):
... n = a
... while a <= b:
... n = n * a
... a = a + 1
... print(n)
...
>>> supercalcx(2, 4)
48
Your function does not convert between data types. A very crude method of this is to do the following below:
def supercalcx(a,b):
n = int(a)
a = int(a)
b = int(b)
while a <= b:
n = n * a
a = a + 1
print(n)
A couple of suggestions to improve your code:
A function should rarely have the print() function inside of it; instead, use the return keyword. You can change a = a + 1 to a += 1 and n = n * a to n *= a. You can also introduce try and except which will attempt to perform whatever is tabbed under try and if something throws an error specified by the except block, it will then perform whatever is tabbed under except. A somewhat improved version is below:
def supercalcx(a, b):
try:
n = int(a)
a = int(a)
b = int(b)
except ValueError:
return "Unable to convert to integers!"
while a <= b:
n *= a
a += 1
return n
print(supercalcx("1", 2))
print(supercalcx(1, 2))

Resources