Hello World - Spring 2007

Homework 9

Attempt by: Monday, April 23
Due by: Thursday, April 26

You may work with a partner on this assignment if you wish.

  1. Review the Python examples from class this week, and spend some time reading (or re-reading) through the Python tutorial How to Think Like a Computer Scientist, focusing mainly on the first 8 chapters. There's much more information here than we need for our purposes, so just read the first few sections of each chapter and feel free to skip around.

  2. Write a Python function binary(number) that takes a number in binary as input and returns the equivalent decimal value. Here is a pseudocode outline:

    def binary(number):
       convert number to a string called digits
       set sum to 0
       set i to the leftmost position of digits
       set exponent to one less than the length of digits
       while i has not reached the end of digits do
          set d to the ith character of digits
          add 2exponent times the value of d to sum
          subtract 1 from exponent
          add 1 to i
       (end loop)
       return sum
    

    Here are some examples of how your function should work:

    >>> binary(10)
    2
    >>> binary(1001)
    9
    >>> binary(11111111)
    255
    >>> binary(1000111000111)
    4551
    >>> 
    
  3. Write a function digitvalue(digit) that takes as input a string consisting of a single hexadecimal digit in the range '0' up to 'F', and returns the equivalent decimal value. Hint: one way to do this is to use a series of if/elif/else statements (also known as a chained conditional). Your function should use a return statement (instead of print) to output the final result. Here is how your function should work (notice the quote marks around the input digit):

    >>> digitvalue('0')
    0
    >>> digitvalue('5')
    5
    >>> digitvalue('A')
    10
    >>> digitvalue('F')
    15
    >>> 
    
  4. Write a function hex(digits) that takes a hexadecimal number as input, and returns the equivalent decimal value. Since a hexadecimal number can contain the digits A, B, C, D, E, or F, the input will be represented as a string instead of an integer. Hint: you can use your digitvalue function as a "building block" in your hex function, much like we used the ulam function as a building block in ulamtable (see the ulam.py example from class). Here is how your function should work:

    >>> hex('10')
    16
    >>> hex('FFF')
    4095
    >>> hex('123ABC')
    1194684
    >>> hex('420')
    1056
    >>> 
    
  5. Write a function convertfrom(base, digits) that takes two inputs: a base value up to 16 and a number in that base represented as a string, and returns the equivalent decimal value. You should use your digitvalue function as a building block. Here is how convertfrom should work:

    >>> convertfrom(8, '175')
    125
    >>> convertfrom(16, 'ABBA')
    43962
    >>> convertfrom(2, '1000111')
    71
    >>> convertfrom(12, 'ABBA')
    19006
    >>> convertfrom(10, '542')
    542
    >>> convertfrom(6, '123450')
    11190
    >>> 
    
  6. Write a function makedigit(value) that takes as input a value from 0 to 15 and returns an equivalent hexadecimal digit represented as a one-character string. Here is how your function should work (notice the quote marks around the output):

    >>> makedigit(5)
    '5'
    >>> makedigit(10)
    'A'
    >>> makedigit(15)
    'F'
    >>> 
    
  7. Write a function convertto(base, number) that takes two inputs: a base up to 16 and a decimal number, and converts the decimal number to the specified base. You should use your makedigit function as a building block. Hint: remember that the % operator in Python can be used to find the remainder of one number divided by another. Here is how your function should work:

    >>> convertto(2, 14)
    '1110'
    >>> convertto(2, 71)
    '1000111'
    >>> convertto(16, 256)
    '100'
    >>> convertto(16, 19006)
    '4A3E'
    >>> convertto(3, 19006)
    '222001221'
    >>> convertto(12, 19006)
    'ABBA'
    >>> 
    
  8. Write a function vowelpos(word) that takes a word, represented as a string, as input and returns the position number (starting from 0) of the first vowel in the string, where a vowel is defined to be one of the letters a, e, i, o, or u. If there is no vowel in the string, -1 should be returned. Here is how your function should work:

    >>> vowelpos('apple')
    0
    >>> vowelpos('trumpet')
    2
    >>> vowelpos('chrome')
    3
    >>> vowelpos('rhythm')
    -1
    >>>
    

Turning in Your Homework

For this homework, you will submit both hardcopy and electronic versions of your programs. I'll use the electronic versions for testing purposes. IMPORTANT: If you work with a partner, please only submit one copy of your assignment, but make sure to put both of your names on your printout so that you both receive credit.

  1. Put all of your Python function definitions in a single file called homework9.py and submit this file electronically using the homework upload web page.

  2. Print out your file, write your name(s) on the printout, and turn this in during class. You will lose points on this assignment if I don't get a printout from you.

If you have questions about anything, just ask!