Hey all just started practicing leetcode problems in python and was confused as to why my code wasn't working. I seem to be having a problem with undefined name when I try to run my code. Appreciate the help. Getting runtime error saying that n = solvingThisThing(n) is not defined
class Solution:
def solvingThisThing(n: int):
sum_of_squares = 0
digit = 0
while(n!=0):
digit = n % 10
sum_of_squares += (digit * digit)
n = n // 10
return sum_of_squares
def isHappy(n: int):
past_nums = set()
past_nums.add(n)
while(True):
if(n == 1):
return True
n = solvingThisThing(n)
if n in past_nums:
return False
else:
past_nums.add(n)
return False
Since the solvingThisThing method is a part of the Solution class, you have to run it from the solution class.
n=Solution.solveThisThing(n)
should work instead of
n=solveThisThing(n)
Related
Hey everyone hope you all doing great!
I was practicing my python, I write a code to execute the factorials of the given Number but the code didn't execute!
The code
def factorial_recursive(n):
if n == 1 or n == 0:
return 1
return n * factorial_recursive(n-1)
f = factorial_recursive(5)
print(f)
After running Python File: None
where the answer must be 120 as factorial of 5 is 120
You have your second return statement indented in the if statement. The second return should be for any other cases. Here is the correct function:
def factorial_recursive(n):
if n == 1 or n == 0:
return 1
return n * factorial_recursive(n-1)
f = factorial_recursive(5)
print(f)
Outputs: 120
You need to call the function like so:
f=factorial_recursive(4)
print(f)
If you actually want to use it on some number (like 4).
Also, you should learn to format your code on stack overflow. Makes it easy for others to understand your code.
(I'm a total beginner)
I want to write a recursive function which can tell me if a number is a prime number or not: but I keep getting the same recursion error :
here is my code :
from math import *
def has_a_divider(n,p):
if n%p==0:
return True
elif has_a_divider(n,(p-1))!= True:
return False
else:
return False
def is_prime(a):
if has_a_divider(a,sqrt(a))==False:
return True
else:
return False
Your problem is sqrt(a) which returns a float instead of the int your code requires.
You can replace it by ceil(sqrt(a)) to get an int from it.
Also, this code does not work for finding if the number is prime as all numbers will have at least 1 has a divider. A possible solution is to update has_a_divider to check for dividers bigger than 1.
A possible implementation:
from math import ceil, sqrt
def has_a_divider(n,p):
if p < 2:
return False
if n % p==0:
return True
return has_a_divider(n,(p-1))
def is_prime(a):
if a == 1:
return False
return not has_a_divider(a,ceil(sqrt(a)))
print(is_prime(3))
print(is_prime(5))
print(is_prime(4))
def fib(n):
if n<= 1:
return n
else:
return(fib(n-1)+fib(n-2))
def comp():
L=[]
for i in range(1,4000000):
if i % 2 ==0:
L.append(fib(i))
return sum(L)
print(comp())
What is wrong with this code? It does not return anything but it looks good according to me.
you should return sum(L) from function not from for loop follow below code
def fib(n):
if n<= 1:
return n
else:
return(fib(n-1)+fib(n-2))
def comp():
L=[]
for i in range(1,20):
if i % 2 ==0:
L.append(fib(i))
return sum(L)
print(comp())
and other thing look at the range its too much,because of this it will take some time or may produce any memory related error, so reduce it for testing.
The return statement is set to the wrong increment. It is executed the first time i % 2 == 0 becomes true (which is i == 2 in your case).
def fib(n):
if n<= 1:
return n
else:
return(fib(n-1)+fib(n-2))
def comp():
L=[]
for i in range(1,4000000):
if i % 2 ==0:
L.append(fib(i))
return sum(L)
print(comp())
The above code is not going to work, though. Are you aware of how big this number would get?
Try
for i in range(1,40):
as a start. It took quite a few seconds on my machine. Result is 63245985.
This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 5 years ago.
I wrote a program which finds the super digit of a problem, IE: 9876 = 9+8+7+6 = 30 = 3+0 = super digit = 3
This works fine whenever the program doesn't call on itself, but in the case above, I will get a final integer value of 3, and it will print as such, but when I go to return it, it returns None. Im wondering why this might be the case?
here is the code:
def super_digit(n):
sup_Digit = 0
intArray = [int(i) for i in str(n)]
for i in range(len(intArray)):
sup_Digit += intArray[i]
if sup_Digit and sup_Digit < 10:
return int(sup_Digit)
else:
super_digit(sup_Digit)
and here is my test case:
from unittest import TestCase
tc = TestCase()
tc.assertEqual(super_digit(5), 5)
tc.assertEqual(super_digit(30), 3)
tc.assertEqual(super_digit(9876), 3)
tc.assertEqual(super_digit(11111111111111), 5)
tc.assertEqual(super_digit(12345678901234567890), 9)
The return statement causes your function to exit and hand back a
value to its caller. The point of functions in general is to take in
inputs and return something.
Ok so by default 'return' gives None so in your case you are returning in the 'if' condition's first part when the condition is True but where is the return of if the condition goes to 'else' part?
def super_digit(n):
sup_Digit = 0
intArray = [int(i) for i in str(n)]
for i in range(len(intArray)):
sup_Digit += intArray[i]
if sup_Digit and sup_Digit < 10:
return int(sup_Digit)
else:
return super_digit(sup_Digit)
print(super_digit(9876))
output:
3
You forgot the return statement:
def super_digit(n):
sup_Digit = 0
intArray = [int(i) for i in str(n)]
for i in range(len(intArray)):
sup_Digit += intArray[i]
if sup_Digit and sup_Digit < 10:
return int(sup_Digit)
else:
return super_digit(sup_Digit)
Hey guys am new to python app development.i have came to learn about recrusive functions in python. The code which i have done
def something(a):
if a == 0:
return 0
else:
return 2 + something(a)
When i called the function like pythons(5) it calls me error like TypeError: something() missing 1 required positional argument: 'b'
I would like to do add two to a number like if i call something(5) it must return 5+2 = 7
I dunno why am getting this error.Hope you guys can help me out in solving this..I aplogise if this is a low grade question on so..
Any help would be appreciated ..Thanks in advance..
There are a couple of issues here. Firstly python code is whitespace sensitive when it comes to indentation (note that the question got edited, the formatting was not correct previously). Then secondly you need to match up the number of arguments provided to the functions:
def pythons(a,b):
if a == 0:
return a
else:
return a * pythons(a,b-1)
When you write def pythons(a,b): you are defining a function with the name pythons and you are specifying that it takes exactly 2 arguments, a and b. Previously you tried to call it with pythons(b-1), the error message tells you exactly what has gone wrong, you only provided 1 argument when the function expected to get 2 arguments.
That is because in your method, when you recursively call pythons, you are not passing two arguments. This is probably what you want:
def pythons(a,b):
if a == 0:
return a
else:
return a * (b-1)
or simply (with same results):
def pythons(a,b):
return a * (b-1)
If your aim is to add two numbers recursively then this would be a more appropriate recursive function.
def add(a, b):
if a == 0 and b ==0:
return 0
elif b == 0 and a>= 0:
return 1 +add(a-1, 0)
elif a ==0 and b>= 0:
return 1 + add(0, b-1)
else: return 2 + add(a-1, b-1)