1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
snow_tiger [21]
3 years ago
5

This second python programming assignment, PA2, is about loop invariants. You will write a function eExp(left,right) that comput

es 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)
Computers and Technology
1 answer:
Andru [333]3 years ago
8 0
Describe a way you could model seafloor spreading I don’t know the answer I need points sorrynxjdidurkek dndirieo eemail eis and furious
You might be interested in
What is the answer ??​
Anna11 [10]

Answer:

option 1 is correct go with 1s .

3 0
3 years ago
Consider rolling a 6-sided die. The outcome of interest is the number of dots that appears on the topside when the die stops rol
damaskus [11]

Answer:

s

Explanation:

5 0
3 years ago
In PC relative addressing mode, the PC and constant need to be added to
vlabodo [156]

l think is the right answer is (a)

6 0
3 years ago
WILL GIVE BRAINLIEST IF DONE CORRECT
Juliette [100K]

Answer:

import sys

#account balance

account_balance = float(500.25)

##prints current account balance

def printbalance():

  print('Your current balance: %2g'% account_balance)

#for deposits

def deposit():

 #user inputs amount to deposit

 deposit_amount = float(input())

 #sum of current balance plus deposit

 balance = account_balance + deposit_amount

 # prints customer deposit amount and balance afterwards

 print('Deposit was $%.2f, current balance is $%2g' %(deposit_amount,

balance))

#for withdrawals

def withdraw():

 #user inputs amount to withdraw

 withdraw_amount = float(input())

 #message to display if user attempts to withdraw more than they have

 if(withdraw_amount > account_balance):

   print('$%.2f is greater than your account balance of $%.2f\n' %

(withdraw_amount, account_balance))

 else:

   #current balance minus withdrawal amount

   balance = account_balance - withdraw_amount

   # prints customer withdrawal amount and balance afterwards

   print('Withdrawal amount was $%.2f, current balance is $%.2f' %

(withdraw_amount, balance))

#system prompt asking the user what they would like to do

userchoice = input ('What would you like to do? D for Deposit, W for

Withdraw, B for Balance\n')

if (userchoice == 'D'): #deposit

 print('How much would you like to deposit today?')

 deposit()

elif userchoice == 'W': #withdraw

 print ('How much would you like to withdraw today?')

elif userchoice == 'B': #balance

 printbalance()

else:

 print('Thank you for banking with us.')

 sys.exit()

6 0
3 years ago
Read 2 more answers
What is the main difference between a struct and a class?
Kay [80]

Explanation:

A class encapsulates methods, while a struct cannot.

4 0
3 years ago
Other questions:
  • Which one of these is a mem?
    8·1 answer
  • Give the 16-bit 2's complement form of the following 8-bit 2's complement numbers: (a) OX94 (b) OXFF (c) OX23 (d) OXBCWhich of t
    7·1 answer
  • Consider that an individual threat agent, like a hacker, can be a factor in more than one threat category. If a hacker breaks in
    5·1 answer
  • What must be done before using ArrayLists?<br><br> (This is Java Programming btw)
    9·1 answer
  • After running a Google Search Ads campaign for several months, Meredith notices sales of her advertised products are starting to
    8·1 answer
  • Time
    6·1 answer
  • Describe how being a global citizen in the world of advanced technology can be beneficial to your success in meeting your person
    13·1 answer
  • USB 3.1 offers an improvement over its predecessors by enabling a maximum data transfer rate of up to:
    8·1 answer
  • ……………………is one of the most popular cloud storage facilities.​
    11·1 answer
  • Use the drop-down menus to complete the steps to convert a macro to VBA.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!