Computers and Technology

This second python programming assignment, PA2, is about loop invariants. You will write a function eExp(left, right) that computes exponentials left ** right in a similar fashion as the egyptian_multiplication function computes products, as discussed in lecture 8: loop invariants. eExp. txt contains some skeleton code. Download it and rename it eExp. py. Study egyptian multiplication in the lecture. The program logic in egyptian_multiplication, and thus the loop invariant is based on the fact that

a * b = if odd(a): b + (a//2)*(b*2)
else: (a//2)*(b*2)

and that p stepwise gathers the product.

For your exponentiation code, the program logic, and thus the loop invariant, is based on the fact that

n ** k = if odd(k): n * (n*n)**(k//2)
else (n*n)**(k//2)

and that e stepwise gathers the exponential.

Your job is to complete the code **INCLUDING** the correct assert statements to check the loop invariant, loop test and their combination, as indicated in the skeleton code. Leave the print statements in place. A correct implementation of eExp:

python3 eExp. py 2 11

produces

program: eExp. py 2 11
n: 2 k: 11 e: 1
n: 4 k: 5 e: 2
n: 16 k: 2 e: 8
n: 256 k: 1 e: 8
k: 0 e: 2048
2 ** 11 = 2048

exp. txt
mport sys

def eExp(left, right):
# precondition: left>0 AND right>0
if left <= 0 or right <= 0 : raise "eExp: invalid inputs!"
n=left; k=right; e=1 #e: the exponent
assert True # fill in the proper loop invariant
while (False) : # the loop test
assert True # fill in the proper loop test and loop invariant
print(" n:",n,"k:",k,"e:",e)
# body
assert True # fill in the proper loop invariant
print("k:",k,"e:",e)
assert True # fill in proper not(loop test) and loop invariant
return e

if __name__ == "__main__":
print("program:", sys. argv[0], sys. argv[1], sys. argv[2])
n = int(sys. argv[1])
k = int(sys. argv[2])
e = eExp(n, k)
print(n,"**",k,"=",e)

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 09:00, jeremytheprimerapper
Which best compares appointments and events in outlook 2010appointments have a subject man, and events do notappointments have a specific date or range of dates, and events do notappointments have a start and end time of day, and events do notappointments have a location option, and events do not
Answers: 2
image
Computers and Technology, 23.06.2019 13:00, torresnoemi899
Which of the following statements is false? a. a class can directly inherit from class object. b. if the class you're inheriting from declares instance variables as private, the inherited class can access those instance variables directly. c. a class's instance variables are normally declared private to enforce good software engineering. d. it's often much more efficient to create a class by inheriting from a similar class than to create the class by writing every line of code the new class requires.
Answers: 3
image
Computers and Technology, 24.06.2019 14:30, danielweldon1234
When workers demonstrate patience, are able to manage there emotions, and get along with other employees, which skills are being displayed?
Answers: 1
image
Computers and Technology, 24.06.2019 16:00, ramirez5571
"gps interaction and cognitive process" respond to the following: imagine that you have been asked to design a gps product that will have voice recognition and bluetooth capability. discuss and conceptualize a user interaction model. predict two (2) problems that may arise in gps products with voice recognition and bluetooth capability. recommend solutions for each of these issues. attention is one of the six cognition processes. when attention is applied to a design, the goal is to make it easier for the end user to quickly locate where he / she should type information on the computer or mobile device screen. compare and contrast how the google search engine and the microsoft bing search engine employ the attention cognition process. provide your opinion as to which search engine better employs the attention cognition process and explain why.
Answers: 3
Do you know the correct answer?
This second python programming assignment, PA2, is about loop invariants. You will write a function...

Questions in other subjects:

Konu
Business, 26.02.2022 17:00