The faculty of two numbers python 3 - python

def choose (x, y):
if y > x:
print ("False")
elif y == 0 or y == x:
return 1
elif y == 1:
return x
else:
if (x-y) > y:
biggest = x-y
smallest = y
else:
biggest = y
smallest = x-y
resultatet = x * choose (x-1, biggest)
res = resultatet // smallest
return res
My function is working perfectly with whatever x input I insert but with bigger Y inputs like 8000 for example I'm getting
File "/home/nazel607/labb3b_2.py", line 20, in choose
resultatet = x * choose (x-1, biggest)
File "/home/nazel607/labb3b_2.py", line 3, in choose
if y > x:
RuntimeError: maximum recursion depth exceeded in comparison
Is there a way I can overcome this problem or is it impossible in python due to its limits? Is there another way than increasing the limit?

It seems that you can get rid of the recursion:
def choose2(x, y):
if y > x:
raise ValueError()
if y == 0 or y == x:
return 1
if y == 1:
return x
result = 1
while y != x:
big, small = max(x-y, y), min(x-y, y)
result *= x // small
x -= 1
y = big
return result
I've tested it over few examples:
for x, y in [
(4, 2),
(17, 9),
(125, 79),
(8005, 13),
(9005, 13),
# (19005, 7004) # exceeds max recursion depth on my machine
]:
assert choose(x, y) == choose2(x, y)
and seems to work fine.

You are not exiting the program ...
def choose (x, y):
if y > x:
print ("False")
return
# ...rest of your program

Related

python changing location using for loop

I want to change location by putting command but got trouble in for loop
All I want is put R R R U D D and get (3,4) location
here is my code
x,y = first start point
N= size of map
N=5
x,y = 1,1
I define left(-1,0), right(1,0), upper(0,-1), down(0,1)
def L(x,y):
if x>1 and x<N and y>1 and y<N:
x=x
y=y-1
return(x,y)
else:
return(x,y)
def R(x,y):
if x<N and y<N:
x=x
y=y+1
return(x,y)
else:
return(x,y)
def U(x,y):
if x>1 and x<N and y>1 and y<N:
x=x-1
y=y
return(x,y)
else:
return(x,y)
def D(x,y):
if x<N and y<N:
x=x+1
y=y
return(x,y)
else:
return(x,y)
input command
move_type=L(x,y),R(x,y),U(x,y),D(x,y)
num = [*map(int, input().split())]
put num [1 1]
change location - this is the point where I got trouble
for i in num:
x,y = move_type[i]
print(x,y)
**result come like this
1 2
1 2
I expect (1,2)
(1,3)
what's wrong with my code
help me plz**
Run Method like that
num = [*map(int, input().split())]
x, y = num
# move_type=L(x,y),R(x,y),U(x,y),D(x,y)
# above tuple has the return value of function because you call them with the x and y as 1.
move_type = L, R, U, D # But here I only provide the function name to
# tuple and later I execute them with the new x and y values
for i in num:
x, y = move_type[i](x, y) # calling the function with the new value of x and y
print(x, y)
One suggestion change your all functions
def L(x, y):
if x > 1 and x < N and y > 1 and y < N: # if this if execute then the x and y modify and return at the end
x = x
y = y-1
# but if if not execute the x and y values return to the same x and y values
return (x, y) # this will return the x, y
def R(x, y):
if x < N and y < N:
x = x
y = y+1
return (x, y)
def U(x, y):
if x > 1 and x < N and y > 1 and y < N:
x = x-1
y = y
return (x, y)
def D(x, y):
if x < N and y < N:
x = x+1
y = y
return (x, y)

How to add all even numbers in a given range recursively?

I have to find the sum of all even numbers between a given range x and y.
I have tried:
def sum_even(x, y):
if x == y:
return x
else:
if x % 2 == 0:
return x + sum_even(x+2, y)
If my given range is (5, 11) then my output should be 24.
Try this:
def sum_even(x, y):
if x >= y:
return 0
if x % 2 == 0:
return x + sum_even(x + 1, y)
return sum_even(x + 1, y)
Examples:
>>> sum_even(5, 11)
24
>>> sum_even(2, 3)
2
>>> sum_even(2, 4)
2
>>> sum_even(2, 5)
6
My code does not include the upper bound y in the sum (see the examples). It might or might not be the desired behavior, but that's depend on your case. If you want to include the upper bound use this code:
def sum_even(x, y):
if x > y:
return 0
if x % 2 == 0:
return x + sum_even(x + 1, y)
return sum_even(x + 1, y)
Examples:
>>> sum_even(5, 11)
24
>>> sum_even(2, 3)
2
>>> sum_even(2, 4)
6
>>> sum_even(2, 5)
6
You might even solve this problem with a O(1) solution. Indeed, you don't need to actually look at ALL the numbers in your range. All you need is a simple mathematical formula. For example, in order to sum all the numbers from 0 to 10^10, you can either do s = sum(x for x in range(10**10 + 1)) (i.e., s = sum(range(10**10 + 1))) or you can use the famous formula s = n * (n + 1) // 2 (i.e., s = 10**10 * (10**10 + 1) // 2). The second approach is much much faster, because you don't need to go through all the numbers in the range.
Similarly in this case you can do this:
def sum_even(x, y):
n1 = y // 2
n2 = (x - 1) // 2
return n1 * (n1 + 1) - n2 * (n2 + 1)
Examples:
>>> sum_even(5, 11)
24
>>> sum_even(2, 3)
2
>>> sum_even(2, 4)
6
>>> sum_even(2, 5)
6
So you don't need recursion and you don't need iteration to solve this problem. Just some math :)
This is your solution patched, I think it could be correct:
def sum_even(x, y):
if x == y or x == y-1:
return x
else:
if x % 2 == 0:
return x + sum_even(x+2, y)
else:
return sum_even(x+1, y)
I assume you want to include x and y (if they're even). For instance, sum_even(2,6) would return 2+4+6.
If you want to exclude the upper bound instead, the code snippets below need a small correction.
# Using python builtin functions `sum` and `range`:
def f1(x, y):
return sum(range(x+x%2, y+1, 2))
# Using arithmetic:
def f2(x, y):
start, end = (x+1)//2, y//2
return end*(end+1) - start*(start-1)
# Using a `for`-loop:
def f3(x, y):
result = 0
for z in range(x, y+1):
if z % 2 == 0:
result += z
return result
# Using recursion:
def f4(x, y):
if x > y:
return 0
elif x % 2 == 1:
return f4(x+1, y)
else:
return x + f4(x+2, y)
# Testing:
print(' x, y --> f1,f2,f3,f4')
for (x, y) in [(5,11), (0,10), (2,6), (12, 17)]:
results = [f(x,y) for f in (f1,f2,f3,f4)]
print('{:2d},{:2d} --> {:2d},{:2d},{:2d},{:2d}'.format(x,y,*results))
# x, y --> f1,f2,f3,f4
# 5,11 --> 24,24,24,24
# 0,10 --> 30,30,30,30
# 2, 6 --> 12,12,12,12
# 12,17 --> 42,42,42,42

Need to find the wrong output in the following python code

def max3bad(x,y,z):
maximum = 0
if x >= y:
if x >= z:
maximum = x
elif y >= z:
maximum = y
else:
maximum = z
return(maximum)
wrong output for what input?
get an input for which u get wrong output
for case: x = 2, y = 1, z = 3
the code output is 0 not 3.
It's better to get max number by :
max(x, y, z)
or you fix bug in your code:
def max3bad(x,y,z):
maximum = 0
if x >= y:
if x >= z:
maximum = x
else:
maximum = z
elif y >= z:
maximum = y
else:
maximum = z
return maximum

if assertion in z3py

i'm a new user for z3py. I need to write a program that check satisfaction of some rule like
IF room.temp < 18 THEN room.fireplace = on
IF room.temp > 24 THEN room.fireplace = off
IF room.CO > 180 THEN room.fireplace = off
IF room.temp > 28 THEN house.hvac = off
IF house.hvac == on THEN room.fireplace = off
also how to write this
bedroom.occupancy == true and bedroom.env_brightness <= 31.5 and bedroom.light.switch = on
thanks
You need a Z3 If-then-else which can be defined using If in z3.
>>> x = Int('x')
>>> y = Int('y')
>>> max = If(x>y, x, y)
>>> max
If(x > y, x, y)
For defining multiple constraints you can use And and Or
>>> max = If(And(x>y, x!=0), x, y)
>>> max
If(And(x > y, x != 0), x, y)
>>> simplify(max)
If(And(Not(x <= y), Not(x == 0)), x, y)
Hope this helps.
This is a great resource to start with z3py in general.

Returning the middle value when giving the function 3 integer arguments

This code is restarting the Python shell, and I cannot work out the errors of my code.
def middle(x,y,z):
if x > y and x < y:
return x
elif y > x and y < z:
return y
elif z > x and z < y:
return z
else:
return False
#Main Routine
middle(1,11,111)
Note that Python can chain comparisons for you (see the docs), and you are missing several cases:
def middle(x, y, z):
"""Return the middle of the three input values."""
if y < x < z or z < x < y: # or min(y, z) < x < max(y, z)
return x
elif x < y < z or z < y < z:
return y
elif x < z < y or y < z < x:
return z
return False
In use:
>>> middle(1, 11, 111)
11
If you want to see results when running the script directly, you will have to be explicit about this; as Martijn suggested in the comments, you could print middle(1, 11, 111). Otherwise the result will be evaluated, but not actually shown on-screen.
You can also simplify by sorting the inputs:
def middle(x, y, z):
"""Return the middle of the three input values."""
x, y, z = sorted((x, y, z))
return y if x < y < z else False
I am not sure what you want to do with this but your function looks weird to me
For example:
if x > y and x < y:
Will never be true
Also, if this function aims at returning the median of the three value it does not do this.
it would be more like
def middle (x, y, z):
t = [x, y, z]
t.sort()
return t[1]
Hope this helped

Categories

Resources