lec 5 trace models
lec 5 trace models
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
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
amount = 100
while :
year = year + 1
amount = aplusi
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)
steps = steps + 1
return aPlusI
Pratik Patil
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-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
#P(k) ==> aplusi_k == amount_k + interes _k #P(k+1) ==> aplusi_k+1 == amount_(k+1) + interest_(k+1)
# time here is k
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
#return amount_k+1
P_k+1 ⇒ amount_k+1
Kinjal
number = n (where n is taken from the person)
prevnum = n-1
newnumber = number - 1
while n>=1:
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
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
steps = steps + 1
return accResult
Gurpreet def comp(p,y,r):
return accResult
def comp(p, r, y):
year = 1
Kinjal amount = p
year = year + 1
# 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