Problem Wk.2.3.1: 1. a1 2. b2 3. c1
Problem Wk2.3.2: class AccountDollars: def __init__(self,value): self.balance = value def depositDollars(self,deposit): self.interestFees() self.balance = self.balance + deposit return self.balance def interestFees(self): self.balance = self.balance * 1.1 self.balance = self.balance - 0.5 class AccountPounds(AccountDollars): def __init__(self,pounds): self.balance = 2*pounds def depositPounds(self,deposit): self.interestFees() self.balance = self.balance + deposit*2 return self.balance/2.0
Problem Wk.2.3.3: import lib601.sm as sm class CountMod5(CountingStateMachine): def getCurOutput(self,state,inp): #print "state", state return state%5 class CountingStateMachine(sm.SM): def __init__(self): self.startState = 0 def getCurOutput(): pass def getNextValues(self,state,inp): output = self.getCurOutput(state,inp) #print "out :", output return (state + 1, output) print "test", CountMod5().transduce([0,1,2,3,4,5,6,7,8,9,10],verbose=True)
Problem Wk.2.3.4 def add(a, b): if b == 0: return a else: return add(a, b-1) + 1 # False - a and b can be any number # False - a can be any number, b must be integer # False - # True - # False # False # True # True # False # add(5,1) # add(5,0) # None def sub(a,b): if b == 0: return a else: return sub(a, b-1) - 1
# Problem Wk.2.3.5 def slowMod(a, b): if a < b: return a else: return slowMod(a - b, b)
Join our real-time social learning platform and learn together with your friends!