"""
========================================
Introduction to Computer Programming
========================================
------------------------------------------
Lab 1: ifs, whiles, pure functions
(starter file)
-----------------------------------------
"""
# ===========================================================================
# the following problems contain bugs ... fix them, then remove this comment
def change(nickels, dimes):
"""Returns the total penny eqivalent of specified number of
nickels and dimes.
>>> change(0, 0)
0
>>> change(1, 1)
15
>>> change(20, 10)
200
"""
return (0.05 * nickels) + (0.10 * dimes)
def three_five(m):
"""Returns 2 if m is divisible 3 and 5, returns 1 if divisible by 3 or 5
but not both, and returns 0 if divisible by neither 3 nor 5.
>>> three_five(45)
2
>>> three_five(25)
1
>>> three_five(27)
1
>>> three_five(14)
0
"""
p = 0
if m % 3 == 0:
p += 1
if m % 5 == 0:
p += 1
return p
def factorial(n):
"""Returns the factorial of n, i.e., the product 1 * 2 * ... n.
>>> factorial(0)
1
>>> factorial(4)
24
>>> factorial(20)
2432902008176640000
"""
product = 1
i = 1
while i >= n:
product *= i
i += 1
return product
def sum_odds(m):
"""Returns the sum of the first m odd numbers,
i.e., 1 + 3 + ... + (2m-1).
>>> sum_odds(0)
0
>>> sum_odds(3) # 1 + 3 + 5
9
>>> sum_odds(50) # 1 + 3 + ... + 99
2500
"""
sum = 0
odd = 1
i = 0
while i < m:
sum += odd
i += 1
return sum
def small_and_even(a, b):
"""Returns the smaller of the two numbers if they are both even, returns
whichever number is even if only one is even, and returns 0 if both
numbers are odd.
>>> small_and_even(5, 3)
0
>>> small_and_even(8, 6)
6
>>> small_and_even(8, 7)
8
"""
if a % 2 == 0:
if b % 2 == 0:
if a < b:
z = a
else:
z = a
else:
if b % 2 == 0:
z = b
return z
def categorize(n):
"""Returns a string indicating which of three categories integer n is
in: 'deficient', 'perfect', 'abundant'. Perfect numbers are the sum
of their factors (not including themselves: 1 + 2 + 3 = 6),
deficient numbers are greater than the sum of their factors (1 + 2 <
4), and abundant numbers are less than the sum of their factors (1 +
2 + 3 + 4 + 6 > 12).
>>> categorize(4)
'deficient'
>>> categorize(6)
'perfect'
>>> categorize(12)
'abundant'
"""
s = 0
d = 1
while d <= n:
if n % d == 0:
s += d
d += 1
if s == n:
cat = "perfect"
if s < n:
cat = "deficient"
else:
cat = "abundant"
return cat
# ==================================================================
# complete the following functions - remove "..." comments as you go
def diff_squares(a, b):
"""Returns the difference of the square of the first number minus the
square of the second number unless the first is smaller than second
in which case it returns 0.
>>> diff_squares(5, 3)
16
>>> diff_squares(3, 4)
0
>>> diff_squares(13, 5)
144
"""
c = 0
# ...
return c
def emphasize(s):
"""Returns a new string that is s surrounded by '*' on each side.
>>> emphasize('')
'**'
>>> emphasize('kidding')
'*kidding*'
"""
return '' # ...
def glue_short_long(a, b):
"""Returns a string formed from the two strings a and b such that the
longer string is appended to the shorter.
>>> glue_short_long('bye', 'hi')
'hibye'
>>> glue_short_long('six', 'seven')
'sixseven'
"""
return '' # ...
def first_digit(n):
"""Returns the leading (most significant) digit of n. Assumes
n is positive or 0.
>>> first_digit(0)
0
>>> first_digit(9)
9
>>> first_digit(537)
5
>>> first_digit(123456789)
1
"""
m = n
# ...
return m
def count_factors(n):
"""Returns the number of factors of n (including 1 and n
itself). Assumes n is positive.
>>> count_factors(1)
1
>>> count_factors(7)
2
>>> count_factors(12)
6
"""
count = 0
# ...
return count
def ever_more(m):
"""Returns the sum of the first m numbers of the sequence of integers,
starting at 1, where the gaps between each sucessive pair of number
in the sequence grow by 1. In other words the sequence looks like:
1 + 2 + 4 + 7 + 11 + 16 + ...
>>> ever_more(1)
1
>>> ever_more(2)
3
>>> ever_more(3)
7
>>> ever_more(4)
14
"""
s = 0
# ...
return s
def count_abundant(n):
"""Returns the number of numbers between 1 and n (inclusive) that
are abundant (numbers that are less than the sum of their factors).
>>> count_abundant(11)
0
>>> count_abundant(50)
9
"""
count = 0
m = 1
while m <= n:
# ...
m += 1
return count
# =================================================================
if __name__ == '__main__':
import doctest
# doctest.testmod() # uncomment this line when ready to test