Question: On PHTHON!! I have to run those two programs but I still can't deal with them. # Program 1: Complete the two functions power and
On PHTHON!! I have to run those two programs but I still can't deal with them.
# Program 1: Complete the two functions power and _power
class Complex: def __init__ (self, r, i ): self.real = r self.imag = i
def add(self, c): self.real += c.real self.imag += c.imag
def _multiply(self, c1, c2): c = Complex(0,0) c.real = c1.real * c2.real -c1.imag*c2.imag c.imag = c1.real * c2.imag + c1.imag * c2.real return c
def _power(self, c, n): # c: Complex, n: positive integer # It's a functional method returning c^n # This must be a RECURSIVE FUNCTION RUNNING in O( log n ) time if n==1: return c else: c1 = self._power(c, n//2) # Do not change any of the above lines in _power # Your code must use this c1 # You code must not change the value of c1, # and not use: # - the math module (including math.arctan) # - a for or while loop # Start code here.
def power(self, n): # This is a procedural method # Change self into self to the power n # Code using _power
# Test Code c = Complex(8, -6) c.power(5) print((c.real, c.imag)) c = Complex(2, -3) c.power(9) print((c.real, c.imag)) # Should print #(-99712, 7584) # (-86158, -56403)
# Program 2: Code the function binarySearch(x, intList) # that returns True if x is in intList, False otherwise, # where x is an integer, intList is a sorted list of integers # The function must be recursive running in O( log n ) time # where n = len(intList)
def binarySearch(x, intList): if len(intList)==1: # Code the base case
else: n1 = len(intList) // 2 # Your inductive step must use n1, # must call binarySearch recursively, # and must not use a loop # Do not change anything between "else" and this line # Code the inductive step
intList = [ 3, 10, 11, 15, 23, 25, 38, 45, 49, 69, 81] xList=[45, 13, 4, 60, 23] out=[] for x in xList: out.append([x, binarySearch(x, intList)]) print(out) # This should show # [[45, True], [13, False], [4, False], [60, False], [23, True]]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
