Created
May 16, 2012 09:29
-
-
Save advait/2709056 to your computer and use it in GitHub Desktop.
Solutions to ICP1 Homework 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Introdcution to Computer Programming 1 | |
Homework 2 Solutions | |
Advait Shinde | |
Note: I tried to keep my solutions well documented. Notice the | |
triple quote documentation in each function body. This is pretty standard | |
in the Python world - a great way to show what your implementation does | |
and how somebody else can use it! | |
""" | |
# Problem 1 | |
def isPalindrome(s): | |
"""Is s a palindrome? | |
Args: | |
s String to test | |
Returns: | |
True iff s is a palindrome. False otherwise. | |
""" | |
return s == s[::-1] # Super slicing :) | |
# Problem 1 Test Cases | |
# assert is a great way to make sure your code works properly | |
# If you try to assert a boolean false value, you will see an error! | |
# We'll go over this more in class! | |
assert isPalindrome("") | |
assert isPalindrome("9") | |
assert isPalindrome("racecar") | |
assert isPalindrome("not ton") | |
assert not isPalindrome("was it a rat i saw") | |
assert not isPalindrome("Malayalam") | |
# Problem 2 | |
def isMultiple(i): | |
"""Returns True iff i is a multiple of both 3 and 4. | |
Args: | |
i Integer to test | |
Returns: | |
True iff i is a multiple of both 3 and 4. False otherwise. | |
""" | |
return (i % 3 == 0) and (i % 4 == 0) | |
def sumOfMultiples(i): | |
"""Returns the sum of all numbers less than i that are multiples of | |
both three and four. | |
Args: | |
i Integer the upper limit to sum | |
Returns: | |
Integer sum | |
""" | |
return sum([n for n in range(i) if isMultiple(n)]) | |
# Problem 2 Test Cases | |
assert isMultiple(0) | |
assert isMultiple(12) | |
assert isMultiple(24) | |
assert not isMultiple(13) | |
assert sumOfMultiples(0) == 0 | |
assert sumOfMultiples(1) == 0 | |
assert sumOfMultiples(13) == 12 | |
assert sumOfMultiples(30) == 36 | |
# Problem 3 | |
def fib(n): | |
"""returns the nth fibonacci number | |
Args: | |
n Integer zero-based index of the fibonacci list | |
Returns: | |
Integer the nth fibonacci number | |
""" | |
a = 1 # Temporary variables to hold last two fib numbers | |
b = 1 | |
for temp in range(n): | |
# We don't care about the value of temp. | |
# We just want to loop n times | |
(a, b) = (b, a+b) # Tuple unpacking :) | |
return b | |
def sumFib(n): | |
"""Returns the sum of the first n fibonacci numbers. | |
Args: | |
n Integer the number of fibs to sum up | |
Returns: | |
Integer sum | |
""" | |
s = 0 | |
a = 1 | |
b = 1 | |
for temp in range(n): | |
# Note that this solution doesn't use fib(). | |
# Why is this method more efficient? | |
s += b | |
(a, b) = (b, a+b) | |
return s | |
# Problem 3 Test Cases | |
assert fib(0) == 1 | |
assert fib(1) == 2 | |
assert fib(2) == 3 | |
assert fib(3) == 5 | |
assert fib(4) == 8 | |
assert sumFib(0) == 0 | |
assert sumFib(1) == 1 | |
assert sumFib(2) == 3 | |
assert sumFib(3) == 6 | |
assert sumFib(4) == 11 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment