Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Loop Invariants This second python programming assignment is about loop invariants. You will write a function eExp(left,right) that computes exponentials left ** right in

Programming Loop Invariants

This second python programming assignment 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 the loop invariants lecture.

The starter code contains some skeleton code. Study egyptian multiplication in the lecture slides. The program logic in egyptian_multiplication, and thus the loop invariant is based on the fact that

image text in transcribed

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. Be sure to use the invariant function, as we will call this directly with different values to check that it is correct. A correct implementation of eExp:

 image text in transcribed

Given code:

import sys

def invariant(n, k, e, left, right): return True

def eExp(left, right): # precondition: left>0 AND right>0 if left

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)

Picture:

image text in transcribed

a + b = if odd (a): b = (a//2)* (b*2) else: (a//2)* (b*2) 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 1 2 import sys 3 4 def invariant(n, k, e, left, right): 5 return True 6 7 def eExp(left, right): 8 # precondition: left> AND right> 9 if left AND right> 9 if left

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Finance for Non Financial Managers

Authors: Pierre Bergeron

7th edition

176530835, 978-0176530839

More Books

Students also viewed these Finance questions

Question

Describe the process for deleting a record from a table.

Answered: 1 week ago