Introduction to Computer Programming: Lab 3

Strings and Tuples

Instructions


Exercises

  1. Python style. Consider this example. You can run it directly in IDLE; it should seem familiar - we considered a very similar example (illustrating the use of pairs) in class. The code achieves its goal, but its style is lacking (in consistency and readability). First, try to identify what could be better about the code's layout. Make changes as you see fit, but be sure to make sure the code still runs. Then try analyzing the code using this Python style checker. Try to eliminate all the style errors while still preserving the code's functionality. You can use the official Style Guide for Python Code as a reference.


  2. Debugging exercises. Each of the functions in this file have bugs. Your job is to fix them one by one, so that when the file is loaded into IDLE it passes all the supplied doctests. (To help you test the debugging exercises, "doctesting" is included at the bottom of the file, but commented out.)


    For the remainder of the lab, work in the main starter file.

  3. Complete left_trim(s) so that it returns a string that is the same as s but with all leading spaces removed. (Hint: this should be quite similar to right_trim from the debugging exercises.) Examples:

    >>> left_trim('')
    ''
    >>> left_trim('   ')
    ''
    >>> left_trim('now is the time!   ')
    'now is the time!   '
    >>> left_trim('   now is the time!')
    'now is the time!'
  4. Comlete trim(s) so that it returns a string that is the same as s but with all leading and trailing spaces removed. (Hint: this can be done in one line using solutions from elsewhere in the lab.) Examples:

    >>> trim('')
    ''
    >>> trim('  ')
    ''
    >>> trim('Nothing to see here.')
    'Nothing to see here.'
    >>> trim('   Much to see here!   ')
    'Much to see here!'

  5. Complete halves(s) so that it returns a pair of strings that are the first half and second half of s. If s has an odd length, then the first half should be one character longer than the second. Use slicing. Examples:

    >>> halves('')
    ('', '')
    >>> halves('I')
    ('I', '')
    >>> halves('I think')
    ('I th', 'ink')
    >>> halves('headache')
    ('head', 'ache')
  6. Generalize the previous function by completing slices(s, n) so that it returns a tuple consisting of n equally sized slices from s. If any of s remains, it is returned as the final item in the tuple. Assumes n > 0. (Hint: use a definite loop.) Examples:

    >>> slices('abcdefg', 3)
    ('ab', 'cd', 'ef', 'g')
    >>> slices('', 5)
    ('', '', '', '', '')
    >>> slices('hello', 1)
    ('hello',)
    >>> slices('0123456789', 2)
    ('01234', '56789')
    >>> slices('abcdefghijklm', 4)
    ('abc', 'def', 'ghi', 'jkl', 'm')
    >>> slices('abcdefghijklm', 5)
    ('ab', 'cd', 'ef', 'gh', 'ij', 'klm')

  7. Complete glue_simple(tup) so that it returns single string consisting of each string in tuple tup concatenated together. Examples:

    >>> glue_simple(())
    ''
    >>> glue_simple(('hello',))
    'hello'
    >>> glue_simple(('hello',))
    'hello'
    >>> glue_simple(('abc', 'def', 'ghi', 'jkl', 'm'))
    'abcdefghijklm'
  8. Modify glue(tup, sep) so that it returns a single string consisting of each string in tuple tup concatenated together with string sep between each item. sep should be an optional parameter; if it is not specified it should default to be a single space. Examples:

    >>> glue(())
    ''
    >>> glue(('hello',))
    'hello'
    >>> glue(('hello',), 'WHATEVER')
    'hello'
    >>> glue(('abc', 'def', 'ghi', 'jkl', 'm'))
    'abc def ghi jkl m'
    >>> glue(('abc', 'def', 'ghi', 'jkl', 'm'), '::')
    'abc::def::ghi::jkl::m'

  9. Complete rand_tuple(count_low, count_high, value_low, value_high) so that it returns a tuple of random length at least count_low items but fewer than count_high items, where each item in the tuple is a random number at least as large as value_low but less than value_high. Examples:

    >>> rand_tuple(3, 6, 10, 20)
    (13, 11, 7)
    >>> rand_tuple(3, 6, 10, 20)
    (19, 10, 15, 10, 13)

  10. Complete parse_first_word(s) so that it returns a pair (tuple) consisting of the the first word in string s and the rest of the string. The word is indicated by a space or the end of the string. Examples:

    >>> parse_first_word('')
    ('', '')
    >>> parse_first_word('unity')
    ('unity', '')
    >>> parse_first_word('now is the time')
    ('now', 'is the time')
  11. Complete parse_words(s) so that it returns a tuple corresponding to the space-separated words in string s. Make use of other functions from the labe such as: squash, trim, and parse_first_word. Examples:

    >>> parse_words('unity')
    ('unity',)
    >>> parse_words('now is the time')
    ('now', 'is', 'the', 'time')
    >>> parse_words('  ignores leading,  trailing,  extra spaces!   ')
    ('ignores', 'leading,', 'trailing,', 'extra', 'spaces!')

  12. Complete add_points(p, q) so that it returns new point that is the sum of the first and second parts of pairs p and q. Examples:

    >>> add_points((1, 2), (3, 4))
    (4, 6)
    >>> add_points((-5, 7), (20, -3))
    (15, 4)
  13. Modify turtle_repl so that it lets the user move a turtle using simple E/W/N/S commands.