Python prime number function returning error in tutorial - function

Python newbie here, so bear with me...
Unfortunately there's no "support" for these tutorials, except posting questions in a Q&A forum and maybe another student can help. I know that there are a ton of Python prime functions out there, but I think I've come up with one that works. However, the Codeacademy interpreter doesn't like my solution.
Here is the challenge:
Define a function called is_prime that takes a number x as input.
For each number n from 2 to x - 1, test if x is evenly divisible by n.
If it is, return False.
If none of them are, then return True.
Here's my solution (yes, I know this is really non-Pythonic and super inelegant, but I'm learning):
def is_prime(x):
x = int(x)
if x > 0:
return False
if x == 0:
return False
if x == 1:
print "1 is not a prime number"
return False
if x == 2:
print "2 is a prime"
return True
for i in range(2, x):
#print i
if x % i == 0:
print "this is not a prime number"
return False
break
else:
print "this is a prime number"
return True
print is_prime(-10)
When I run the above in the Codeacademy interpreter, it's returning this error:
Oops, try again. Your function fails on is_prime(-10). It returns True when it should return False.
Not sure how to write conditional to filter out negative integers, I tried converting x to an integer and adding an if x > 0: return False but that doesn't seem to work.
What am I doing wrong here?

You don't return the result for any value greater than 2.
For 0, 1 and 2, you do return it:
return True
For the fourth case that covers all other numbers, you only print the result, but you don't return it as a boolean value.
Edit: Your attempt to filter negative values fails, because you return False when the input is positive, not negative:
if x > 0: return False
You should use this instead:
if x < 0: return False

def is_prime(x):
# All numbers < 2 are not prime
if x < 2:
return False
for i in range(2, x):
if x % i == 0:
return False
return True
print "is_prime({}): {}".format(-7, is_prime(-7))
print "is_prime({}): {}".format(-10, is_prime(-10))
print "is_prime({}): {}".format(11, is_prime(11))
print "is_prime({}): {}".format(18, is_prime(18))

First of all,
if x > 0:
needs to be
if x < 0:
Algebra says that > means greater than and < means less than. Because negative numbers are less than 0, you return False only when x < 0.
Then,
for i in range(2, x):
#print i
if x % i == 0:
print "this is not a prime number"
return False
break
else:
print "this is a prime number"
return True
Has a lot of indentation inaccuracies. The problem is, your else: statement is in conjunction with if x == 2: and not the if statement in the for loop. I, however, the all-knowing, understand your purpose and am here to save the day (sorry for that). Change all that stuff on the bottom to
for i in range(2, x):
#print i
if x % i == 0:
print "This is not a prime number"
return False
#You don't need a break statement because you are returning a value. This means that the function stops because it has completed its job, which is to give back something
print "this is a prime number"
return True
#If all that for looping was to no avail and the number is a prime number, we already know that by this point. We can just return True.

To cater for the none positive and zero use x <= 0
def is_prime(x):
x = int(x)
if x <= 0:
return False
if x == 1:
print "1 is not a prime number"
return False
if x == 2:
print "2 is a prime"
return True
for i in range(2, x):
#print i
if x % i == 0:
print "this is not a prime number"
return False
break
else:
print "this is a prime number"
return True

def is_prime(x):
if x < 2:
return False
for n in range(2,x-1):
if x % n == 0:
return False
else:
return True

Related

Not understanding the result (calculation)

Can anybody explain how x gets to 6 in this function? As I cant wrap my head around it
def f(x):
if x == 0:
return 0
return x + f(x-1)
print(f(3))

Can I make function which find for me divisible by x and y in range w,z

I want to make a function that finds numbers divisible by x and y in range w to z. For example this one works:
T = []
for i in range(1500,2701):
if (i % 7 == 0) and (i % 5 == 0):
T.append(str(i))
print('\n'.join(T))
Question is can I make all from above in function where I can change range and divisible by:
x=input("range from")
y=input("range to")
w=input("divisible by")
z=input("divisible by")
def divisible(x,y,w,z):
for i in range(x,y):
if (i % w == 0) and (i % z == 0):
T.append(str(i))
return T
print('\n'.join(T))
def divisible(x,y,w,z):
T=[]
for i in range(x,y):
if (i % w == 0) and (i % z == 0):
T.append(str(i))
return ('\n'.join(T))
print(divisible(1500,2701,5,7))
These two attempts doesn't work, any idea why? I'm beginner and any help is more than welcome.
Indentation matters in Python. You're returning T right after you find the first divisible number. You have to return when the loop ends. Change to:
def divisible(x,y,w,z):
for i in range(x,y):
if (i % w == 0) and (i % z == 0):
T.append(str(i))
return T
Now is working
x=int(input("range from"))
y=int(input("range to"))
w=int(input("divisible by"))
z=int(input("divisible by"))
def divisible(x,y,w,z):
T=[]
for i in range(x,y):
if (i % w == 0) and (i % z == 0):
T.append(str(i))
return T
print(divisible(x,y,w,z))

Why is there an error saying I haven't defined the function "is_even"?

def is_even(answer):
answered = X % 2 ==0
if answer == answered:
return "yep"
else:
return "nope"
the instructions are:
Write a function called is_even that takes one input, x, and returns the string "yep" if the input is even and "nope" otherwise. You do not need to call the function.
It's most likely a compile error, as you never introduce X. Also, you should read the instructions again. It clearly says what the input should be (x). So the method should start like the following:
def is_even(x):
if ...:
return "yep"
else:
return "nope"

Return a value from a function called in while loop

The point is to guess a random number choosen from an interval of integers and do it within a fixed numbers of attempts.
The main function asks the upper limit of the interval and the number of guesses the user can give. The core function then should return the guessed value so when the number is right the function should terminate immediately.
I put some print statement while debugging and I understood that the y value is not returned to the while statement from the core function.
# -*- coding: utf-8 -*-
def main():
from random import choice
p = input("choose upper limit: ")
t = input("how many attempts: ")
pool = range(p+1)
x = choice(pool)
i = 1
while ((x != y) and (i < t)):
core(x,y)
i += 1
def core(x,y):
y = input("choose a number: ")
if y == x:
print("You gussed the right number!")
return y
elif y > x:
print("The number is lower, try again")
return y
else:
print("The number is higher, try again")
return y
You want to assign the return value of core back to the local y variable, it's not passed by reference:
y = core(x)
You'll also need to set y before you go into the loop. Local variables in functions are not available in other functions.
As a result, you don't need to pass y to core(x) at all:
def core(x):
y = input("choose a number: ")
if y == x:
print("You gussed the right number!")
return y
elif y > x:
print("The number is lower, try again")
return y
else:
print("The number is higher, try again")
return y
and the loop becomes:
y = None
while (x != y) and (i < t):
y = core(x)
i += 1
It doesn't much matter what you set y to in the main() function to start with, as long as it'll never be equal to x before the user has made a guess.
y = -1
while ((x != y) and (i < t)):
y = core(x,y)
i += 1
You "initialize" y before the loop. Inside the loop you set y equal to the result of core() function.

How can you emulate recursion with a stack?

I've heard that any recursive algorithm can always be expressed by using a stack. Recently, I've been working on programs in an environment with a prohibitively small available call stack size.
I need to do some deep recursion, so I was wondering how you could rework any recursive algorithm to use an explicit stack.
For example, let's suppose I have a recursive function like this
function f(n, i) {
if n <= i return n
if n % i = 0 return f(n / i, i)
return f(n, i + 1)
}
how could I write it with a stack instead? Is there a simple process I can follow to convert any recursive function into a stack-based one?
If you understand how a function call affects the process stack, you can understand how to do it yourself.
When you call a function, some data are written on the stack including the arguments. The function reads these arguments, does whatever with them and places the result on the stack. You can do the exact same thing. Your example in particular doesn't need a stack so if I convert that to one that uses stack it may look a bit silly, so I'm going to give you the fibonacci example:
fib(n)
if n < 2 return n
return fib(n-1) + fib(n-2)
function fib(n, i)
stack.empty()
stack.push(<is_arg, n>)
while (!stack.size() > 2 || stack.top().is_arg)
<isarg, argn> = stack.pop()
if (isarg)
if (argn < 2)
stack.push(<is_result, argn>)
else
stack.push(<is_arg, argn-1>)
stack.push(<is_arg, argn-2>)
else
<isarg_prev, argn_prev> = stack.pop()
if (isarg_prev)
stack.push(<is_result, argn>)
stack.push(<is_arg, argn_prev>)
else
stack.push(<is_result, argn+argn_prev>)
return stack.top().argn
Explanation: every time you take an item from the stack, you need to check whether it needs to be expanded or not. If so, push appropriate arguments on the stack, if not, let it merge with previous results. In the case of fibonacci, once fib(n-2) is computed (and is available at top of stack), n-1 is retrieved (one after top of stack), result of fib(n-2) is pushed under it, and then fib(n-1) is expanded and computed. If the top two elements of the stack were both results, of course, you just add them and push to stack.
If you'd like to see how your own function would look like, here it is:
function f(n, i)
stack.empty()
stack.push(n)
stack.push(i)
while (!stack.is_empty())
argi = stack.pop()
argn = stack.pop()
if argn <= argi
result = argn
else if n % i = 0
stack.push(n / i)
stack.push(i)
else
stack.push(n)
stack.push(i + 1)
return result
You can convert your code to use a stack like follows:
stack.push(n)
stack.push(i)
while(stack.notEmpty)
i = stack.pop()
n = stack.pop()
if (n <= i) {
return n
} else if (n % i = 0) {
stack.push(n / i)
stack.push(i)
} else {
stack.push(n)
stack.push(i+1)
}
}
Note: I didn't test this, so it may contain errors, but it gives you the idea.
Your particular example is tail-recursive, so with a properly optimising compiler, it should not consume any stack depth at all, as it is equivalent to a simple loop. To be clear: this example does not require a stack at all.
Both your example and the fibonacci function can be rewritten iteratively without using stack.
Here's an example where the stack is required, Ackermann function:
def ack(m, n):
assert m >= 0 and n >= 0
if m == 0: return n + 1
if n == 0: return ack(m - 1, 1)
return ack(m - 1, ack(m, n - 1))
Eliminating recursion:
def ack_iter(m, n):
stack = []
push = stack.append
pop = stack.pop
RETURN_VALUE, CALL_FUNCTION, NESTED = -1, -2, -3
push(m) # push function arguments
push(n)
push(CALL_FUNCTION) # push address
while stack: # not empty
address = pop()
if address is CALL_FUNCTION:
n = pop() # pop function arguments
m = pop()
if m == 0: # return n + 1
push(n+1) # push returned value
push(RETURN_VALUE)
elif n == 0: # return ack(m - 1, 1)
push(m-1)
push(1)
push(CALL_FUNCTION)
else: # begin: return ack(m - 1, ack(m, n - 1))
push(m-1) # save local value
push(NESTED) # save address to return
push(m)
push(n-1)
push(CALL_FUNCTION)
elif address is NESTED: # end: return ack(m - 1, ack(m, n - 1))
# old (m - 1) is already on the stack
push(value) # use returned value from the most recent call
push(CALL_FUNCTION)
elif address is RETURN_VALUE:
value = pop() # pop returned value
else:
assert 0, (address, stack)
return value
Note it is not necessary here to put CALL_FUNCTION, RETURN_VALUE labels and value on the stack.
Example
print(ack(2, 4)) # -> 11
print(ack_iter(2, 4))
assert all(ack(m, n) == ack_iter(m, n) for m in range(4) for n in range(6))
print(ack_iter(3, 4)) # -> 125