0% found this document useful (0 votes)
6 views

lec 5 trace models

Uploaded by

ricksonyonah24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

lec 5 trace models

Uploaded by

ricksonyonah24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Trace Semantics of Programs,

or Programs as Sequences
Prof Sane
● Write a python function to calculate factorial of n
● For this course, we want programs are easily translatable between a
spreadsheet view and the program statements
○ This means we dont want recursive functions etc.
● One point of the course is to internalize the view of programs as sequences of
histories or traces or sequences of variable values
● No IO inside functions, no print, no input etc.
● Only thing we have is functions which take some input parameters and return
results
● Printing if any will occur in some supporting function if needed
● Only python allowed is functions, if/else, while, i = i + 1, Lists and Tuples
● P(1) before the while loop, P(k) => P(k+1) is the while loop body
● NEVER MODIFY PARAMETERS
Desired version of code: clear link to induction and spreadsheet
def fact(n):
steps = 1
inputSoFar = 1
accResult = 1 # P(1) is true here
while steps < n:
# P(k) holds here
inputSoFar = inputSoFar + 1
accResult = accResult * inputSoFar
# P(k+1) is true here
steps = steps + 1
return accResult
Prof Sane: The design of while loops
● A loop is about the inductive property of sequences of variable values
● What is P(1) -- what is the “setup” of a loop
○ Just after the setup and before the loop, you must establish property P for the first step
○ amount = initial_amount*(1 + r)
● This ensures that just after the test of the while loop, and just before the body
P(1) holds the first time
● If the body gets you to P(2) then P(2) holds before the next iteration
● AND SO ON -- “and so on” is the fundamental principle of loops
● The test tells you when to stop, often depending on parameters
○ Here the key question is things like is the test “<” , or “<=”
○ The test depends on expected values
● In your head, you have to create a spreadsheet and mimic in the code
● 9 times of out 10, you should not have to debug your loop
Prof Sane def comp(p, r, y):
def comp(p, r, y): year = 1
year = 1 amount = p
year = 1
amount = 100 interest = amount * r/100
amount = 100
interest = amount * r/100 aplusi = amount + interest
interest = amount * r/100
aplusi = amount + interest while year < y:
aplusi = amount + interest
while year < y: year = year + 1
while :
year = year + 1 amount = aplusi
year = year + 1
amount = aplusi interest = amount * r/100
amount = aplusi
interest = amount * r/100 aplusi = amount + interest
interest = amount * r/100
aplusi = amount + interest return amount
aplusi = amount + interest

return amount
Prof Sane
def comp(p, r, y): # P(p, r, y) == p*(1+r)^y
compound amount has a closed form
year = 1 formula
amount = p
n! → 1*2*.. n
interest = amount * r/100

aplusi = amount + interest

while year < y:

year = year + 1 # year_k+1 == year_k + 1

amount = aplusi # amount_k+1 == aplusi_k

interest = amount * r/100 # interest_k+1 == amount_k+1*r/100

aplusi = amount + interest # aplus_k+1 == amount_k+1 + interest_k+1

# we have establish P(k+1)

# whatever we return will be the result of P(k+1)

return amount # → you will actually get amount_k over here rather k+1
Problem: timestamped values
# P(k) ==> accResult_(k) == inputSoFar_(k)*(inputSoFar_(k) + 1)/2

Put P(k), # time here is k


P(k+1) and
inputSoFar = inputSoFar + 1
timestamped
variables for # inputSoFar_k+1 = inputSoFar_k + 1
the comp()
function just accResult = accResult + inputSoFar
like we did for
# accResult_(k+1) = accResult_(k) + inputSoFar_(k)+1
the sumt
function in the steps = steps + 1
4th lecture last
time. # P(k+1) ==> accResult_(k+1) == inputSoFar_(k+1)*(inputSoFar_(k+1) + 1)/2
year = 1

amount = 100

interest = amount * r/100

aplusi = amount + interest

while :

year = year + 1

amount = aplusi

interest = amount * r/100

aplusi = amount + interest


Prajas Naik def comp(p, r, y):

steps = 1

aPlusI = p
def factorial(n):

i=1 years = 0
fact = 1
while (years < y):
while (i <= n):
years = years + 1
fact = fact * i

i += 1 amount = aPlusI
return fact
aPlusI = aPlusI * (1 + r)

interest = aPlusI - amount

steps = steps + 1

return aPlusI
Pratik Patil
def factorial(n):

if n == 0:

return 1

else:

return n * factorial(n-1)

n = int(input("Enter a number: "))

print("The factorial of",n,"is",factorial(n))


Sneh Pahuja
def factorial(n): def calccompound (years, pAmount, r):
steps = 1 time = 1
inputSoFar = 1 rate = r /100
accResult = 1 interest = pAmount * rate
while steps <= n: amount = pAmount + interest
inputSoFar = inputSoFar + 1 while time < years:
accResult = accResult * inputSoFar time = time + 1
return accResult # time(k) = time(k-1) + 1
interest = amount * rate
# interest(k + 1) = amount(k -1) * rate (k - 1)
amount = amount + interest
# amount (k + 2) = amount ( k - 1) + interest( k + 1)
return interest
# return interest(k + 1)
Sneh Pahuja (updated)
def factorial(n):
steps = 0
inputSoFar = 1
accResult = 1
while steps <= n:
inputSoFar = inputSoFar + 1
accResult = accResult * inputSoFar
steps = steps + 1
return accResult
Nidhi
def factorial(n) : def comp(p,r,y)
Steps =1
inputsofar=1 year = 1
accResult=1 amount = 100
while steps < n : interest = 10
accResult = accResult * inputsofar aplusi = 110
inputsofar = inputsofar + 1
steps = steps + 1 while year < y
return accResult amount = aplusi
interest = amount * 0.1
aplusi = amount + interest
year = year + 1

return aplusi
Nidhi interest = amount * r/100
def comp(p, r, y):
#interst_k+1 = amount_(k+1) * r/100
year = 1

amount = p
aplusi = amount + interest
interest = amount * r/100
#aplusi_k+1 = amount_(k+1) + interest_(k+1)
aplusi = amount + interest

while year < y:

#P(k) ==> aplusi_k == amount_k + interes _k #P(k+1) ==> aplusi_k+1 == amount_(k+1) + interest_(k+1)

# time here is k

year = year + 1 return aplusi


# year_k+1 == year_k + 1

amount = aplusi

# amount_k+1 == aplusi_(k)
Rishi
def factorial(n): def compound_interest(p,r, t):

i=1
if n == 0:
while i <= t:
return 1
p = p * (1 + r / 100)
else:
i += 1
return n * factorial(n-1) return principal
Deevankumar
def factorial (x):

if x == 0:

return 1

else:

return(x)*factorial(x-1
)
def comp(p, r, y):

year = 1

Ayesha amount = p

interest = amount * r/100

aplusi = amount + interest


N = int(input("Enter a number."))
fact = 1 P(k) ⇒ amount_k

print (range(1,N)) while year < y:


print(range(1, N+1))
year = year + 1
for i in range(1,N+1):
fact = fact*i #year_k+1 = year_k + 1
print("N! is: ", fact)
amount = aplusi

def compound(n): #amount_k = aplusi_k


amount = 100
interest = amount * r/100
year = 1
interest = 10
# interest_k = amount_k *r/100

while year <= n: aplusi = amount + interest


aplusi = amount + interest
interest = interest + 1 #aplusi_k+1 = #amount_k+1 + interest_k+1
amount = aplusi
return aplusi return amount

#return amount_k+1

P_k+1 ⇒ amount_k+1
Kinjal
number = n (where n is taken from the person)

prevnum = n-1

factorial = number * prevnum

newnumber = number - 1

while n>=1:

factorial = newnumber * prevnum

newnumber = number - 1
Diya Gambhir
num = int(input("Enter a number: "))
factorial = 1

if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

def comp(p,r,y):

year = 1
amount = 100
interest = amount*r/100
aplusi = amount+interest

while year>y :
year = year + 1
amount = aplusi
interest = amount*r/100
aplusi = amount+interest

return amount
while year < y:
Aadil
year = year + 1

x=1 # year(k-1) = year(k)

amount = 100 amount = aplusi


interest = 0.1
# amount(k) = aplusi(k+1)
aplusi = amount+(amount*interest)
interest = amount * r/100
years = 9

aplusi = amount + interest


while x < years:

aplusi = aplusi+(aplusi*interest)
return aplusi
print(f"aplusi = {aplusi} for Year {x}")

x = x+1
Bhargavi
def factorial(n):

fact = n

while fact>=1:

factorial = 1 * fact

fact = fact - 1

Return factorial
Bhargavi
def comp(p,r,n):

steps = 1

accResult = p

while steps <=n :

accResult = accResult * (1+r)

steps = steps + 1

return accResult
Gurpreet def comp(p,y,r):

amount = p def comp(p,y,r):


interest = amount * r/100 years = 1
def fact(n): aplusi = amount + interest
amount = p
steps = 1
interest = amount * r/100
inputSoFar = 1 while years < y:

accResult = 1 amount = aplusi aplusi = amount + interest


interest = amount * r/100

while steps < n: aplusi = amount + interest


while years < y:
inputSoFar = years = years + 1
inputSoFar + 1 amount = aplusi
return amount

interest = amount * r/100


accResult =
accResult * inputSoFar
aplusi = amount + interest
years = years + 1
steps =
steps + 1
return amount

return accResult
def comp(p, r, y):

year = 1
Kinjal amount = p

interest = amount * r/100


amount = n (taken from person)
aplusi = amount + interest
years = m (taken from person)
while year < y:

year = year + 1

while n <= m: amount = aplusi

# at time k=0
interest = amount * 0.1
interest = amount * r/100
aplusi = amount + interest
# at time k=0
amount = aplusi
aplusi = amount + interest

# at time k=1

# up to time k=y-1

return amount

You might also like