Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python HELP!! Hello, I\'m taking CS and learning python now and I was doing HW,

ID: 3590992 • Letter: P

Question

Python HELP!!

Hello, I'm taking CS and learning python now and I was doing HW, but they are hard this time.

Please HELP!!!!

# 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]]

Explanation / Answer

# 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.
if n%2 == 0:
return self._multiply(c1, c1)
else:
return self._multiply(c, self._multiply(c1, c1))
  
  
def power(self, n):
# This is a procedural method
# Change self into self to the power n
# Code using _power
c = self._power(self, n)
self.real = c.real
self.imag = c.imag

  
# 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)

# code link: https://paste.ee/p/aY0Ku

'''
Sample output
(-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)

# 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
return x == intList
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
if intList[n1] == x:
return True
elif intList[n1] > x:
return binarySearch(x, intList[: n1])
else:
return binarySearch(x, intList[n1+1:])


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]]

# code link: https://paste.ee/p/PbiWA

'''

Sample output

'''