I have a five part problem that builds on each part. I have to: I have already d
ID: 3805409 • Letter: I
Question
I have a five part problem that builds on each part. I have to:
I have already done the first to parts, writing the egcd and multinv functions, I just need help with the other three functions.
# Part 1: Extended Euclidean Algorithm
def egcd(a, b):
if b == 0:
return (1, 0)
else:
# Calculate q
q = a // b
# Calculate r
r = a % b
# Calculate (s, t) by calling egcd(b, r)
(s,t) = egcd(b, r)
return (t, s-q*t)
# Part 2: Multiplicative Inverse
def multinv(a, n):
g = egcd(a, n)
return g[0] % n
# Part 3: Decrypt a single value
def decrypt(c, m, k):
...
# Part 4: Decrypt a byte string into an array of ints
def decryptstring(ciphertext, m, k):
...
# Part 5: Decrypt a byte string, returning a byte string
def lineardecipher(ciphertext, m, k):
...
Explanation / Answer
Here is the python code for above questions. Comments are along with code. Please do rate the answer if it helped. Thanks.
def productOfOdds(tup):
prod = 1
if tup[0]%2 == 1: # if th efist element in tuple is odd
prod = tup[0]
if len(tup) > 1: #if more than 1 element is present
prod = prod * productOfOdds(tup[1:]) #call recursively with rest of elements
return prod;
#------------------------------
def printStarBucks(n):
star = '*' #define the star character
dollar = '$' #deffine the dollar character
if n > 0: #make sure n is +ve
print star*n,dollar*n # the * operator is used for repeatition
return;
#------------------------------
def factorial(n):
#recursive function to calculate factorial of number n
if n > 1: # for number n which is greater than 1, it is n * factorial of previous
fact = n * factorial (n-1)
else:
fact = 1
return fact;
#------------------------------
def oddevenfact(n):
fact = 1 #1 will be returned when n is 0 or less
if n > 1:
# for odd numbers recursive function will stop at 1 and and for even numbers
# last recursive call will be with n = 0 which will return 1
fact = n * oddevenfact (n-2)
return fact;
#------------------------------
def equals(x,y):
#A function to check if x and y are equal using recursion
if x < 0 or y < 0: #when one of them becomes negative also function terminates
value = False
else:
if x==0 and y==0: #the recursive function call terminates at this condition
value = True
else:
value = equals(x-1, y-1) #call recursively by decreasing both numbers
return value;
#------------------------------
def printStars(n):
#function to print * symbol n times
star='*'
print star*n
return;
#------------------------------
def printTriangle(n):
#prints * in triangle form with last line having n *s
for i in range(n+1): #range(n+1) gives numbers from 1 to n
printStars(i)
return;
#------------------------------
#testing productOfOdds
t=(5,2,7) #first element odd and total 2 odd elements
val=productOfOdds(t)
print 'productOfOdds' ,t ,'=' , val
t=(3,) #only one odd number
val=productOfOdds(t)
print 'productOfOdds' ,t ,'=' , val
t=(3,5) #all odd elements
val=productOfOdds(t)
print 'productOfOdds' ,t ,'=' , val
#testing printStarBucks
print 'calling printStartBucks(3)'
printStarBucks(3) # prints ***$$$
print 'calling printStarBucks(0)'
printStarBucks(0) # prints nothing
#testing factorial
print 'factorial(1)=' ,factorial(1 ) #should print 1
print 'factorial(3)=' ,factorial(3) #should print 6
print 'factorial(5)=' ,factorial(5) #should print 120
#test oddevenfact
print 'oddevenfact(6)=' ,oddevenfact(6) #should print 6*4*2 ie. 48
print 'oddevenfact(7)=' ,oddevenfact(7) #should print 7*5*3*1 i.e 105
#testing equals
print 'equals(5,5)=' , equals(5,5) #should print true
print 'equals(3,4)=' , equals(3,4) #should print false
#testing printTriangle
printTriangle(3)
printTriangle(5)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.