Implement the next method of the Fib class. For this class, the value attribute is a Fibonacci number. The next method returns a Fib instance whose value is the next Fibonacci number. The next method should take only constant time.
Note that in the doctests, nothing is being printed out. Rather, each call to .next() returns a Fib instance, which is represented in the interpreter as the value of that instance (see the __repr__ method).
Hint: Keep track of the previous number by setting a new instance attribute inside next.
defnext(self): "*** YOUR CODE HERE ***" if self.value == 0: result = Fib(1) else: result = Fib(self.value + self.previous) result.previous = self.value return result def__repr__(self): returnstr(self.value)
Remember that next must return a Fibonacci object! With this in mind, our first goal is to calculate the next Fibonacci object and return it. One approach is to figure out the base case (self.value == 0) and then decide what information is needed for the following call to next.
You might also note that storing the current value makes the solution look very similar to the iterative version of the fib problem.
Create a class called VendingMachine that represents a vending machine for some product. A VendingMachine object returns strings describing its interactions. See the doctest below for examples:
classVendingMachine: """A vending machine that vends some product for some price. >>> v = VendingMachine('candy', 10) >>> v.vend() 'Machine is out of stock.' >>> v.deposit(15) 'Machine is out of stock. Here is your $15.' >>> v.restock(2) 'Current candy stock: 2' >>> v.vend() 'You must deposit $10 more.' >>> v.deposit(7) 'Current balance: $7' >>> v.vend() 'You must deposit $3 more.' >>> v.deposit(5) 'Current balance: $12' >>> v.vend() 'Here is your candy and $2 change.' >>> v.deposit(10) 'Current balance: $10' >>> v.vend() 'Here is your candy.' >>> v.deposit(15) 'Machine is out of stock. Here is your $15.' >>> w = VendingMachine('soda', 2) >>> w.restock(3) 'Current soda stock: 3' >>> w.restock(3) 'Current soda stock: 6' >>> w.deposit(2) 'Current balance: $2' >>> w.vend() 'Here is your soda.' """ def__init__(self, product, price): self.product = product self.price = price self.stock = 0 self.balance = 0
defrestock(self, n): self.stock += n return'Current {0} stock: {1}'.format(self.product, self.stock)
defdeposit(self, n): if self.stock == 0: return'Machine is out of stock. Here is your ${0}.'.format(n) self.balance += n return'Current balance: ${0}'.format(self.balance)
defvend(self): if self.stock == 0: return'Machine is out of stock.' difference = self.price - self.balance if difference > 0: return'You must deposit ${0} more.'.format(difference) message = 'Here is your {0}'.format(self.product) if difference != 0: message += ' and ${0} change'.format(-difference) self.balance = 0 self.stock -= 1 return message + '.'
You may find Python string formatting syntax useful. A quick example:
1 2 3
>>> ten, twenty, thirty = 10, 'twenty', [30] >>> '{0} plus {1} is {2}'.format(ten, twenty, thirty) '10 plus twenty is [30]'