Introduction to Computer Programming

Program 1: ifs, whiles, pure functions

Due date: Thursday, September 27 at 5pm


Instructions


  1. Complete the function tri_num(m) so that it returns half of the product of m and one more than m. Examples:

    In  []: tri_num(0)
    Out []: 0
    In  []: tri_num(3)
    Out []: 6
    In  []: tri_num(6)
    Out []: 21
  2. Complete zero_off(n) so that it returns the string 'off' if n is zero and 'on' if n is nonzero. Examples:

    In  []: zero_off(0)
    Out []: 'off'
    In  []: zero_off(4103)
    Out []: 'on'
  3. Complete pad_bang(s, n) so that it returns a new string of (at least) length n that is the same as s but with as many '!' characters appended as necessary to make the length at least n. Examples:

    In  []: pad_bang('hi', 2)
    Out []: 'hi'
    In  []: pad_bang('bye', 2)
    Out []: 'bye'
    In  []: pad_bang('cool', 9)
    Out []: 'cool!!!!!'
  4. Complete append_digit(n, d) so that it returns an integer that is like n but with digit d appended at its end. Assume d is a nonnegative integer less than 10; otherwise it should just return n. Examples:

    In  []: append_digit(410, 3)
    Out []: 4103
    In  []: append_digit(61, 0)
    Out []: 610
    In  []: append_digit(32767, 49)
    Out []: 32767
  5. Complete pow5(n) so that it returns 5 raised to the n power. Use a while loop; do not use the exponentiation operator (**). Examples:

    In  []: pow5(0)
    Out []: 1
    In  []: pow5(2)
    Out []: 25
    In  []: pow5(4)
    Out []: 625
    In  []: pow5(37)
    Out []: 72759576141834259033203125
  6. Complete sum_digits(n) so that it returns the sum of the digits in n. Examples:

    In  []: sum_digits(0)
    Out []: 0
    In  []: sum_digits(41)
    Out []: 5
    In  []: sum_digits(9876)
    Out []: 30
  7. Complete unary_digits(n) so that it returns a string representation of n where each digit of n is expressed in unary - i.e., as the corresponding number of '*' characters repeated. Digit places should be separated by the '|' character. You are encouraged to take advantage of Python's string multiplication operator (*). Examples:

    In  []: unary_digits(0)
    Out []: ''
    In  []: unary_digits(7)
    Out []: '*******'
    In  []: unary_digits(4103)
    Out []: '****|*||***'
  8. Complete collatz_count(m) so that it returns the number of iterations of the Collatz (Ulam) calculation required to get from m down to 1. For example, to go from 3 requires 7 steps:

    3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1

    Assume m is positive. Examples:

    In  []: collatz_count(1)
    Out []: 0
    In  []: collatz_count(4)
    Out []: 2
    In  []: collatz_count(3)
    Out []: 7
  9. Complete threes(m) so that it returns the number of times that m can be repeatedly divided by 3. Assume m is positive. Examples:

    In  []: threes(3)
    Out []: 1
    In  []: threes(7)
    Out []: 0
    In  []: threes(45)
    Out []: 2
    In  []: threes(1134)
    Out []: 4
  10. Complete count_primes(n) so that it returns the number of prime numbers less than or equal to n. Examples:

    In  []: count_primes(2)
    Out []: 1
    In  []: count_primes(10)
    Out []: 4
    In  []: count_primes(100)
    Out []: 25