Skip to content

Instantly share code, notes, and snippets.

@eecsmap
Last active May 5, 2022 19:07
Show Gist options
  • Save eecsmap/e88914cece030dab4b882a6cbca30d63 to your computer and use it in GitHub Desktop.
Save eecsmap/e88914cece030dab4b882a6cbca30d63 to your computer and use it in GitHub Desktop.
cs61a
class VirFib():
"""A Virahanka Fibonacci number.
>>> start = VirFib()
>>> start
VirFib object, value 0
>>> start.next()
VirFib object, value 1
>>> start.next().next()
VirFib object, value 1
>>> start.next().next().next()
VirFib object, value 2
>>> start.next().next().next().next()
VirFib object, value 3
>>> start.next().next().next().next().next()
VirFib object, value 5
>>> start.next().next().next().next().next().next()
VirFib object, value 8
>>> start.next().next().next().next().next().next() # Ensure start isn't changed
VirFib object, value 8
"""
def __init__(self, value=0):
self.value = value
def next(self):
"*** YOUR CODE HERE ***"
ans = VirFib(int(not self.value) or self.value + self.prev)
ans.prev = self.value
return ans
def __repr__(self):
return "VirFib object, value " + str(self.value)
import doctest; doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment