Introduction to Computer Programming: Lab 3

Strings

Instructions


Exercises

Debugging exercises Each of the first few problems are syntactically valid but due not work as advertised. Your job is to fix them one by one. (Make sure your fixed versions pass the supplied doctests.)

  1. Fix thrice(x) so that it returns three times x. As in:

     >>> thrice(0)
     0
     >>> thrice(7)
     21
  2. Fix has_upper(s) so that it returns True if s has at least one uppercase letter in it. Examples:

     >>> has_upper('')
     False
     >>> has_upper('X')
     True
     >>> has_upper('abc')
     False
     >>> has_upper('ABCs')
     True
     >>> has_upper('the language is Python')
     True
  3. Fix right_trim(s) so that it returns a string the same as s, but with all trailing spaces removed. Examples:

     >>> right_trim('')
     ''
     >>> right_trim('   ')
     ''
     >>> right_trim('now is the time!   ')
     'now is the time!'
     >>> right_trim('now is the time!')
     'now is the time!'
  4. Fix squash(s) so that it returns a string which is the same as s, but such that any consecutive spaces are “squashed” into one space. Examples:

     >>> squash('')
     ''
     >>> squash('NoSpaces')
     'NoSpaces'
     >>> squash('   ')
     ' '
     >>> squash('  There   are  too     many spaces here!   ')
     ' There are too many spaces here! '

Complete the following functions using only the bare minimum of string operations: len, +, chr, ord, ==, and character subscripting ([]). Use for loops over strings where appropriate.

  1. Complete is_low(ch) following the supplied model of is_up so that it returns True if character ch is a lowercase letter.

  2. Complete print_character_codes(start, stop) so that it displays code numbers and corresponding Unicode characters, one per line from start (inclusive) through stop (exclusive). For this problem, you can and should use the format method. Example:

     In []: print_character_codes(33, 37)
     33: !
     34: "
     35: #
     36: $
  3. Complete to_up(s) so that it returns uppercase version of s. Equivalent to s.upper(). Example:

     >>> to_up('When will there be good news?')
     'WHEN WILL THERE BE GOOD NEWS?'
  4. Complete char_less(c, d) so that it returns True if character c is lexicographically less than d. Equivalent to c < d.

  5. Complete find_common_prefix_length(s, t) so that it returns the number of consecutive characters strings s and t have in common starting from their beginnings. Examples:

     >>> find_common_prefix_length('hello', 'hell on wheels')
     4
     >>> find_common_prefix_length('CASE sense', 'CASE Sense')
     5
     >>> find_common_prefix_length('there', 'the')
     3
     >>> find_common_prefix_length('nothing', 'in common')
     0
  6. Complete string_less(s, t) so that it returns True if string s is lexicographically less than string t. Equivalent to built-in string comparison s < t. Examples:

     >>> string_less('', 'anything')
     True
     >>> string_less('hello', 'hell on wheels')
     False
     >>> string_less('Apple', 'apple')
     True
     >>> string_less('the', 'there')
     True
     >>> string_less('nothing', 'in common')
     False
  7. Complete slice_from(s, start) so that it returns s[start:] following the model of the supplied slice_to. Assumes start >= 0. Examples:

     >>> slice_from('whatever', 3)
     'tever'
     >>> slice_from('whatever', 0)
     'whatever'
     >>> slice_from('whatever', 8)
     ''
     >>> slice_from('whatever', 4063)
     ''
  8. Complete slice(s, start, stop) so that it returns s[start:stop]. Assumes start and stop both >= 0. Examples:

     >>> slice('whatever', 2, 5)
     'ate'
     >>> slice('whatever', 0, 8)
     'whatever'
     >>> slice('whatever', 5, 5)
     ''
     >>> slice('whatever', 4, 4063)
     'ever'

    Complete the following functions using Python’s built-in string slicing.

  9. 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 above 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!'
  10. Complete 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!'
  11. Complete parse_first_word(s) so that it returns the first word in string s. 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'