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

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 00:30
Quick pl which one of the following is considered a peripheral? a software b mouse c usb connector d motherboard
Answers: 1
question
Computers and Technology, 23.06.2019 07:00
1. you have a small business that is divided into 3 departments: accounting, sales, and administration. these departments have the following number of devices (computers, printers, etc.): accounting-31, sales-28, and administration-13. using a class c private network, subnet the network so that each department will have their own subnet. you must show/explain how you arrived at your conclusion and also show the following: all available device addresses for each department, the broadcast address for each department, and the network address for each department. also, determine how many "wasted" (not usable) addresses resulted from your subnetting (enumerate them).
Answers: 3
question
Computers and Technology, 23.06.2019 12:00
From excel to powerpoint, you can copy and paste a. cell ranges and charts, one at a time. b. cell ranges and charts, simultaneously. c. charts only. d. cell ranges only.
Answers: 3
question
Computers and Technology, 23.06.2019 21:30
Enzo’s balance sheet for the month of july is shown. enzo’s balance sheet (july 2013) assets liabilities cash $600 credit card $4,000 investments $500 student loan $2,500 house $120,000 mortgage $80,000 car $6,000 car loan $2,000 total $127,100 total $88,500 which expression finds enzo’s net worth?
Answers: 1
You know the right answer?
This second python programming assignment, PA2, is about loop invariants. You will write a function...
Questions
question
Chemistry, 04.04.2021 05:30
question
Mathematics, 04.04.2021 05:30
question
English, 04.04.2021 05:30