I'm python user and I'm evaluate combination in python.
For two integers m,n, nCm=n!/(m!*(n-m)!)
So, I defined factorial and combination function.
factorial function is working, but combination function does not working.
What is the error?
1> Factorial Function
def factorial(a):
f=1
for i in range(1,a+1):
f=f*i
print(f)
2> Combination Function
def Combination(n,m):
fn=factorial(n)
fm=factorial(m)
fnm=factorial(n-m)
ncm=factorial(n)/(factorial(m)*factorial(n-m))
print(ncm)
In factorial function, For example, factorial(4)=24 is working in python.
But, In combination function,
When I typing Combination(4,2),
24
2
2
24
2
2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-18-daae2b10838c> in <module>()
----> 1 Combination(4,2)
<ipython-input-17-76c6e425ad35> in Combination(n, m)
3 fm=factorial(m)
4 fnm=factorial(n-m)
----> 5 ncm=factorial(n)/(factorial(m)*factorial(n-m))
6 print(ncm)
7
TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'
What's the matter in my coding?
Instead of printing the output of a function, you should return the result.
def factorial(a):
f=1
for i in range(1,a+1):
f=f*i
return f
def combination(n,m):
return = factorial(n)/(factorial(m)*factorial(n-m))
print combination(4,2)
One more remark: after calculating factorial(n), it will be stored in your fn variable. You shouldn't calculate it again, but rather take that value and "recycle" it.
def factorial(a):
f=1
for i in range(1,a+1):
f=f*i
return f
def Combination(n,m):
fn=factorial(n)
fm=factorial(m)
fnm=factorial(n-m)
ncm=fn/(fm*fnm)
print(ncm)
Combination(4,2)
In your code there is missing return statement in factorial function.
Related
I've implemented a fibonacci Measurement algorithm with 2 parameter n and p.
I got this issue,
TypeError Traceback (most recent call last)
<ipython-input-19-295638b26e62> in <module>
2 N = 10
3 # [F(n,p) for n in range(N)]
----> 4 print(F(10,1))
<ipython-input-12-fda62c8ec9a6> in F(n, p)
6 elif n <= p+1:
7 return n
----> 8 return F(n-1) + F(n-p-1)
TypeError: F() missing 1 required positional argument: 'p'
I have input 2 parameters n =10, p = 1, but still having this problem "missing 1 required argument". Does anyone know why and solution for this or any suggestion would be appreciated!
There could be two potential issues.
You're calling a function, F, that doesn't seem to be defined in the snippet you've attached. You might want to change it to fibonacci_of if it is supposed to call itself recursively. In addition, since the fibonacci_of accepts two parameters, you would need to call it with two arguments
If F is already defined elsewhere, it is supposed to accept more than one argument. You could check its function definition and see the parameter requirements. See attached examples.
def square(a): # Requires single parameter.
return a ** 2
def add(a, b): # Requires two parameters.
return a + b
Having issue in the follwing code, it looks like I am facing issue with either list or int operations, so where and how should I do the changes to correct this code:
cube = lambda x:x**3
def fibonacci(n):
fb=[0,1]
for i in range(2,n-1):
fb.append([fibonacci(i-1)+fibonacci(i-2)])
return fb
if __name__ == '__main__':
n = int(input())
print(list(map(cube, fibonacci(n))))
Error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-761cca8f2b48> in <module>
7 if __name__ == '__main__':
8 n = int(input())
----> 9 print(list(map(cube, fibonacci(n))))
<ipython-input-9-761cca8f2b48> in <lambda>(x)
----> 1 cube = lambda x:x**3
2 def fibonacci(n):
3 fb=[0,1]
4 for i in range(2,n-1):
5 fb.append([fibonacci(i-1)+fibonacci(i-2)])
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
You should be certain what you want fibonacci() to return. It looks like you want it to return a list since you are passing the result to map(). If so, the recursive calls will also return a list, to this line doesn't really make sense:
fb.append([fibonacci(i-1)+fibonacci(i-2)])
because fibonacci(i-1) and fibonacci(i-2) are also lists, but it looks like you expect an integer here as a result of the addition.
Since you are using a loop in the function and have a seed list, you don't need the recursion. Just index into the list fb:
def fibonacci(n):
fb=[0,1]
for i in range(2,n-1):
fb.append(fb[i-1] + fb[i-2])
return fb
n = 9
fibonacci(n)
# [0, 1, 1, 2, 3, 5, 8, 13]
With that, your cube map() should work.
You might want to replace
fb.append([fibonacci(i-1)+fibonacci(i-2)])
with
fb.append(fibonacci(i-1)+fibonacci(i-2))
Right now, you are mapping a function that takes a scalar as input onto a list containing both numbers like 0 and 1 as well as lists, like [1], for example.
I am getting an error when running the following code:
def f(x):
return x**3-x-2
def bisection(f,a,b,app=0.3):
Fa,Fb=f(a),f(b)
if Fa*Fb>0:
raise Exception('Fails')
for i in range(a,b):
x=(a+b)/2
if f*Fa>0:
a=x
else:
b=x
return x
print(bisection(f,{'a':1,'b':2},0.00003))
Below the output I get when running the script above:
TypeError: unsupported operand type(s) for ** or pow(): 'dict' and 'int'
Any ideas on how to solve this?
There are a couple of issues here. In the bisection function you are supposed to give 4 arguments, f which is a function, a and b which are numbers, and a fourth argument app which is not used in your function currently.
The first issue is that you are giving a dictionary instead of the a and b arguments. The following will work better.
bisection(f=f,a=1,b=2,app=0.00003)
I do not know the bisection method, but from what I can tell from a quick scan on the bisection method from Wikipedia, it should be an N_MAX variable which tells how many iterations you will allow before breaking.
Therefore you should not use a for loop and use a while loop instead.
The new algorithm is then
def f(x):
return x**3-x-2
def bisection(f,a,b,app=0.3, N_MAX=10):
Fa,Fb=f(a),f(b)
if Fa*Fb>0:
raise Exception('Fails')
n = 0
while n < N_MAX:
x=(a+b)/2
if f(x)==0 or ((a-b)/2) < app:
return x
n += 1
if f(x) > 0:
a = x
else:
b = x
return x
print(bisection(f,1,2,0.00003, 10))
>>> 1.5
I have a piece of code as follows:
def range_print(n, *args):
for i in range(n):
func(i)
#range_print(n=10)
def func(i):
print(i)
I get:
TypeError Traceback (most recent call last)
<ipython-input-4-b13e44263521> in <module>
2 for i in range(n):
3 func(i)
----> 4 #range_print(n=10)
5 def func(i):
6 print(i)
<ipython-input-4-b13e44263521> in range_print(n, *args)
1 def range_print(n, *args):
2 for i in range(n):
----> 3 func(i)
4 #range_print(n=10)
5 def func(i):
TypeError: 'NoneType' object is not callable
Later I solved the problem, but I don't know if there is a better way?
I want to use decorators to make my code look cleaner.
def range_print(n, *args):
def inner(func): # <----Why do I need this def to load func?
for i in range(n):
func(i)
return inner
#range_print(n=10)
def func(i):
print(i)
The reason why the function inner takes a function as a parameter, is that this is how the mechanics of Python work when you add a decorator like #range_print(n=10) to your function.
When you call your function, Python passes the function itself, together with its arguments, to the range_print decorator.
So in your case, the parameter named n in the range_print function signature is actually a function.
def list_gen(a,b,c,d):
print(a,b,c,d)
l=[]
for i in range(a,b):
for j in range(c,d):
l.append(d[i,j])
return l
When I pass the arguments to the function list_gen(0,3,0,3) I am getting below error:
TypeError Traceback (most recent call last)
<ipython-input-51-ac343943d448> in <module>()
----> 1 list_gen(0,3,0,3)
<ipython-input-49-afc3d3a347a9> in list_gen(a, b, c, d)
4 for i in range(a,b):
5 for j in range(c,d):
----> 6 l.append(d[i,j])
7 return l
TypeError: 'int' object is not subscriptable
But this code works without any issues. Can anyone tell what is the error here ?
for i in range(0,3):
for j in range(0,3):
print(d[i,j])
Apparently you have a global variable d containing a dict that you want to access. But you used d as the name of a parameter of the function, so that hides the global name. When you use d[i,j], it tries to index the number 3, which is not possible.
The solution is to use a different variable for the function parameter.
def list_gen(a,b,c,e):
print(a,b,c,e)
l=[]
for i in range(a,b):
for j in range(c,e):
l.append(d[i,j])
return l
You'll run into problems like this less often if you use more verbose, meaningful variable names than d.
The parameter in range has to be integer and since the for j in range(c,d): didn't throw error, it mean d is int and you cannot reference int using d[i,j] in the line l.append(d[i,j]) . You probably have a variable named d whose scope is active during the print statement.