Why both codes below will give the same result if we pass the variables (x,y) to the function and without passing it
First Code
def gen():
yield (x + y)
yield (x - y)
x = int(input("X = "))
y = int(input("Y = "))
g = gen()
while True:
try:
print(next(g))
except:
print("END!")
break
Second Code
def gen(x,y):
yield (x + y)
yield (x - y)
x = int(input("X = "))
y = int(input("Y = "))
g = gen(x,y)
while True:
try:
print(next(g))
except:
print("END!")
break
Related
I am looking for a reduced way of this code. I had to do the division separately because of the multiplicative inverse condition.
"""This code calculates the multiplication Elliptic curves over Zp"""
#Initial values for testing
yq = 3
yp = 3
xq = 8
xp = 8
a = 1
p = 11
#Calculate the Euclid greatest common divisor
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return g, x - (b // a) * y, y
#Calculate the multiplicate inverse
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise Exception('Mod inverse does not exist')
else:
return x % m
#veces = Number of times for the multiplication
veces = 7
print(f"The next results are the multiplication of {veces}*({xp},{yp})")
z = 1
while z <= (veces - 1):
if xp == xq and yp == yq:
numerador = (3 * pow(xp, 2) + a) % p
denominador = ((2 * yp) % p)
inver = modinv(denominador, p)
landa = (inver * numerador) % p
else:
numerador = (yq - yp) % p
denominador = (xq - xp) % p
inver = modinv(denominador, p)
landa = (inver * numerador) % p
xr = (pow(landa, 2) - xp - xq) % p
yr = (landa * (xp - xr) - yp) % p
z += 1
xp, yp = xr, yr
print(f"The result is ({xp},{yp})")
#Any way to simplify this code? I had to do the division separately but I think we can a reducted code for the division.
I'm trying to print only products of 3 and 5 if x < 1000. I'm getting a lot of repeats. How do I make sure that I don't have any repeats?
for x in range(1000):
y = 3
z = 5
a = x % y
b = x % z
if a == 0:
print (x)
if b == 0:
print (y)
You need to combine the two conditions with disjunction (or):
for x in range(1000):
y = 3
z = 5
a = x % y
b = x % z
if a == 0 or b == 0:
print (x)
x = random.randint(1, 100)
y = random.randint(1, 100)
myList = [x, y]
newNumber = random.randint(x, y)
but when...
myList = [(nsmallest(2, guesses, key=lambda x: abs(x-number)))]
I don't know what the numbers are, and how to use them as arguments (x, y) to determine the newNumber
"Unpacking Argument Lists"
random.randint(*myList)
Hi community when I run my script I receive the recursion error. I don't know how to go about it since my program is due in less than an hour. If someone can give me some insight that would be great! It is a two part script.
My code
import math
import random
from random import randrange, uniform
def add(u, v):
sum1 = [x + y for x, y in zip(u, v)]
return sum1
def negate(u):
myng = [x - 2*y for x, y in zip(u, u)]
return myng
def sub(u,v):
d = [x - y for x, y in zip(u, v)]
return d
def scalarMult(c, u):
m = [(x+y-y)*c for x, y in zip(u,u)]
return m
def zip(u,v):
mt = [x * y for x, y in zip(u, v)]
return mt
def dot(u, v):
l = zp(u,v)
a = sum(l)
return a
def length(u):
d = dot(u,u)
x = math.sqrt(d)
return x
def unit(v):
l = length(v)
m = [(x+y-y)/l for x,y in zip(v,v)]
return m
def randVector(n, a, b):
l = [] * n
i = 0
while(i<n):
l.append(random.uniform(a,b))
i=i+1
return l
#------------------------------------------------------------------------------
# VectorTest.py
#------------------------------------------------------------------------------
import Vector
A = [-3, -4, 7]
B = [6, -2, 2]
print(A)
print(B)
print(Vector.add(A,B))
print(Vector.negate(B))
print(Vector.sub(A,B))
print(Vector.scalarMult(2.5,A))
print(Vector.scalarMult(-3.5,B))
print(Vector.zip(A,B))
print(Vector.dot(A,B))
print(Vector.length(A))
print(Vector.length(B))
print(Vector.unit(A))
print(Vector.unit(B))
print(Vector.angle(A,B))
C = Vector.randVector(3,-10,10)
print(Vector.sub(C,C))
Your zip function infinitely calls itself, generating an infinite recursion.
def zip(u, v):
mt = [x * y for x, y in zip(u, v)]
return mt
When you call it, it has to call itself to get its value. When it's called this way, it calls itself again and again, never completing.
The "main" code works to calculate every single binomial coefficient, unless (n = k + 1). It is mind boggling - it says there is a division by zero error but I cannot see why. the error occurs in the main function ( d = n2 / c ). Any idea why?
def getInteger( prompt ):
while True:
try:
num = int( input(prompt))
except ValueError:
print( "That is not an integer -- please try again")
continue
return num
def factorial(f):
f = f
q = (f - 1)
fac = (f * q)
while (q) > 1:
q -= 1
fac = (fac * q)
return (fac)
def main():
n = getInteger("enter a factor:")
k = getInteger("enter a factor:")
while n >= k :
n2 = factorial(n)
k2 = factorial(k)
a = n - k
b = factorial(a)
c = b * k2
d = n2 / c
print("n2 = {} k2 = {} a = {} b = {} c = {} d = {}" .format(n2, k2, a, b, c, d) )
return
else:
print("n must be larger than k")
if __name__ == '__main__':
main()
main()
Note that I need to implement the calculations myself so I cannot use libraries.
Your factorial function is not correct for the inputs 0,1. It returns 0 for both of them, while it should return 1 for both of them.