15-110 Summer 2017

Programming Assignment 7


For this assignment, make a folder named pa7 and save your answers in it.

When you are finished, zip up and submit.

As always, you are responsible for protecting your solutions to these problems from being seen by other students, both physically (e.g., by looking over your shoulder) and electronically.

Part I

You will create a Python source file containing a Python function(s) implementing each of the problems described below. In implementing these functions, you may not use and built-in data conversion functions that convert between numbers and strings directly. You may, however, use subscripting with [ ] to access a character of a string, and the functions ord (to convert from a character to its ASCII value) and chr (to convert from an ASCII value to the corresponding character).

  1. [3 points] A positive integer \(n\) can be converted to a string of 1's and 0's holding that numbers binary represntation using the following algorithm:

    1. Let s be an empty string.
    2. While \(n > 0\) do the following:
      1. Let ch be "0" if \(n\) is even and "1" if \(n\) is odd.
      2. Add ch to the beginning of s. (Use the + concatenation operator.)
      3. Divide \(n\) by 2.
    3. Return s.

    Write a function convert_to_binary(n) (in convert_to_binary.py) which takes a non-negative integer n and returns the a string of 1's and 0's

    NOTE THAT: You will have to figure out how to adjust the algorithm to deal with the case where parameter n = 0.

    Example:

    
    >>> convert_to_binary(127)
    '1111111'
    >>> convert_to_binary(4)
    '100'
    >>> convert_to_binary(3)
    '11'
    >>> convert_to_binary(0)
    '0'
    
  2. [3 points] The value of a number represented with a binary string s can be found by the formula below:

    • The value of binary "1101" is 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20
    • The value of binary "11001" is 1 x 24 + 1 x 23 + 0 x 22 + 0 x 21 + 1 x 20

    Another way is iterating over the indices of the string and, for each index i, updating value to be the value of the binary number represented by s[0:i+1]. An example is shown in the table below:

    sis[i]value
    "11001" 0 "1" 1
    "11001"1"1"3
    "11001"2"0"6
    "11001"3"0"12
    "11001"4"1"25

    Write a function valueOfBinary(s) (in valueOfBinary.py) that takes a non-empty string of 1's and 0's and converts it into the corresponding integer value.
    HINT: For each character in the string (from left to right), if it is a "1", double the current value and add 1. If it is a "0", just double the current value.

    Example:

    
    >>> valueOfBinary("0")
    0
    >>> valueOfBinary("1")
    1
    >>> valueOfBinary("101")
    5
    >>> valueOfBinary("0011101110")
    238
    
  3. [4 points] All functions for this part should be in a file called characters.py.

    For this problem you will convert letters between upper and lower case. This is easy to do, because in the ASCII encoding, the code for each lower-case letter is obtained by adding 32 to the code for the corresponding upper-case letter. But you can't do arithmetic with characters in Python. Instead, you must get the character's code using the ord function, do the arithmetic, and convert the result back to a character using the chr function.

    Example:

    >>> ord('A')
    65
    >>> ord('a')
    97
    >>> ord('Z')
    90
    >>> ord('z')
    122
    >>> chr(65)
    'A'
    >>> chr(97)
    'a'
    >>> chr(90)
    'Z'
    >>> chr(122)
    'z'
    

    You are not allowed to use any built-in functions that convert between upper and lower case, or any that indicate whether a character is upper or lower case. Instead, use the character codes and numeric operations.

    1. Write a function upperCase(c) that takes a character stored in the parameter c and returns the upper-case version of this character. If the character is not a lower-case letter, just return it unchanged.

      Example:

      >>> upperCase('a')
      'A'
      >>> upperCase('m')
      'M'
      >>> upperCase('M')
      'M'
      >>> upperCase('*')
      '*'
      

    2. Write a function lowerCase(c) that takes a character stored in the parameter c and returns the lower-case version of this character. If the character is not an upper-case letter, just return it unchanged.

      Example:

      >>> lowerCase('A')
      'a'
      >>> lowerCase('M')
      'm'
      >>> lowerCase('m')
      'm'
      >>> lowerCase('*')
      '*'
      

    3. [HARDER] Write a function called capitalize(s). The parameter is a string s and the function returns another string that is the same as s except that all words are capitalized. To capitalize a word means to convert its first letter to upper case and all the other letters to lower case. A word is any sequence of letters (not including spaces). You may assume that the string s contains only letters and spaces. You must use the functions upperCase and lowerCase that you already defined.

      Example:

      >>> capitalize("the quick brown fox")
      'The Quick Brown Fox'
      >>> s = "PYTHON IS A SNAKE"
      >>> capitalize(s)
      'Python Is A Snake'
      >>> s
      'PYTHON IS A SNAKE'
      >>> capitalize("  abc   ")
      '  Abc   '
      >>> capitalize("abc")
      'Abc'
      

      HINT:

      Loop over each character of the string s. If it can be the start of a word, make it upper case. Otherwise, make the character lower case (this will not affect spaces). The big question is: how do you know when you're at the start of a word?

Submission Checklist

Your pa7.zip should contain

  1. convert_to_binary.py
  2. valueOfBinary.py
  3. characters.py
.

Submit your assignment using Gradescope.