Introduction to Computer Programming: Lab 1

Ifs, Whiles, and Pure Functions

Instructions


  1. Fix the function change(nickels, dimes) so that it correctly returns the total penny equivalent of the specified number of nickels and dimes. Examples:

    >>> change(0, 0)
    0
    >>> change(1, 1)
    15
    >>> change(20, 10)
    200
  2. Fix three_five(m) so that it returns 2 if m is divisible by 3 and 5, returns 1 if m is divisible by 3 or 5 but not both, and returns 0 if m is divisible by neither 3 nor 5. Examples:

    >>> three_five(45)
    2
    >>> three_five(25)
    1
    >>> three_five(27)
    1
    >>> three_five(14)
    0
  3. Fix factorial(n) so that it correctly returns the factorial of n, i.e., the product 1 * 2 * ... n. Examples:

    >>> factorial(0)
    1
    >>> factorial(4)
    24
    >>> factorial(20)
    2432902008176640000
  4. Fix sum_odds(m) so that it correctly returns the sum of the first m odd numbers, i.e., 1 + 3 + ... + (2m-1). Examples:

    >>> sum_odds(0)   # nothing to sum, so result is 0
    0
    >>> sum_odds(3)   # 1 + 3 + 5
    9
    >>> sum_odds(50)  # 1 + 3 + ... + 99
    2500
  5. Fix small_and_even(a, b) so that it 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. Examples:

    >>> small_and_even(5, 3)
    0
    >>> small_and_even(8, 6)
    6
    >>> small_and_even(8, 7)
    8
  6. Fix categorize(n) so that it 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). Examples:

    >>> categorize(4)
    'deficient'
    >>> categorize(6)
    'perfect'
    >>> categorize(12)
    'abundant'

  7. Complete diff_squares(a, b) so that it 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. Examples:

    >>> diff_squares(5, 3)
    16
    >>> diff_squares(3, 4)
    0
    >>> diff_squares(13, 5)
    144
  8. Complete emphasize(s) so that it returns a new string that is s surrounded by * on each side. Examples:

    >>> emphasize('')
    '**'
    >>> emphasize('kidding')
    '*kidding*'
  9. Complete glue_short_long(a, b) so that it returns a string formed from the two strings a and b such that the longer string is appended to the shorter. Examples:

    >>> glue_short_long('bye', 'hi')
    'hibye'
    >>> glue_short_long('six', 'seven')
    'sixseven'
  10. Complete first_digit(n) so that it returns the leading (most significant) digit of n. Assumes n is positive or 0. Examples:

    >>> first_digit(0)
    0
    >>> first_digit(9)
    9
    >>> first_digit(537)
    5
    >>> first_digit(123456789)
    1
  11. Complete count_factors(n) so that it returns the number of factors of n (including 1 and n itself). Assumes n is positive. Examples:

    >>> count_factors(1)
    1
    >>> count_factors(7)
    2
    >>> count_factors(12)
    6
  12. Complete ever_more(m) so that it 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 + ...

    Examples:

    >>> ever_more(1)
    1
    >>> ever_more(2)
    3
    >>> ever_more(3)
    7
    >>> ever_more(4)
    14
  13. Complete count_abundant(n) so that it returns the number of numbers between 1 and n (inclusive) that are abundant (numbers that are less than the sum of their factors). Examples:

    >>> count_abundant(11)
    0
    >>> count_abundant(50)
    9