subject

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)

ansver
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 00:20, mmmmaaarriiieeee
The open systems interconnection (osi) reference model: defines standards for many aspects of computing and communications within a network. is a generic description for how computers use multiple layers of protocol rules to communicate across a network. defines standards for wireless local area network (wlan) communication protocols. details the advantages and disadvantages of various basic network cabling options.
Answers: 1
image
Computers and Technology, 23.06.2019 10:00, karissanichole18
Install and use wireshark program ( send back screen shots and other vital information) case project 3-2: decode a tcp segment in a wireshark capture in this chapter, you walked through tcp segment to interpret the data included in its header. in this project, you use wireshark to capture your own http messafes, examine the tcp headers, and practice interpreting the data you'll find there. 1. open wireshark and snap the window to one side of your screen. open a browser and snap that window to the other side of your screen so you can see both windows.
Answers: 2
image
Computers and Technology, 23.06.2019 11:30, talyku7131
Me dangers of social media and the internetexplain what each means: 1) social media and phones have become an addiction.2) outside people have access to you all the time.3) cyberstalking4) cyberbullying5) catphishing6) viruses7) identity theft8) credit card fraud9) hacking10) money schemes
Answers: 1
image
Computers and Technology, 23.06.2019 18:30, DSUDLER5555
Write a program that prints the day number of the year, given the date in the form month-day-year. for example, if the input is 1-1-2006, the day number is 1; if the input is 12-25-2006, the day number is 359. the program should check for a leap year. a year is a leap year if it is divisible by 4, but not divisible by 100. for example, 1992 and 2008 are divisible by 4, but not by 100. a year that is divisible by 100 is a leap year if it is also divisible by 400. for example, 1600 and 2000 are divisible by 400. however, 1800 is not a leap year because 1800 is not divisible by 400.
Answers: 3
You know the right answer?
This second python programming assignment, PA2, is about loop invariants. You will write a function...

Questions in other subjects: