Introduction to Computer Programming: Lab 4

Bitstrings and Files



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 reverse(s) so it returns a new string which is s in reverse order. Example:

     >>> reverse('Hello')
  2. Fix count_lines_from_file() so that it repeatedly prompts the user for a filename until a valid one is given. It then counts prints the number of lines in the file. Example:

     >>> count_lines_from_file()
     enter filename: what
     "what" is not a valid file.
     enter filename: no-way
     "no-way" is not a valid file.
     enter filename: sample.txt
     "sample.txt" has 4 lines.
  3. Fix find_next(s, start, ch) so that it returns index of the next occurrence of character ch in string s starting from index start. If there are no further instances of ch from that point on, returns the length of s. Examples:

     >>> find_next('Now is the time.', 0, ' ')
     >>> find_next('Now is the time.', 4, ' ')
     >>> find_next('Now is the time.', 7, ' ')
     >>> find_next('Now is the time.', 11, ' ')
  4. Fix show_bits(n) so that it prints n as a binary number, each bit on its own line, from least significant to most significant. Assumes n is a nonnegative integer. Example:

     In [] show_bits(53)

    Main exercises

  5. Complete is_bits(s) so that it returns True if s is nonempty and consists only of the characters 1 and 0. Examples:

     >>> is_bits('')
     >>> is_bits('10011')
     >>> is_bits('0011')   # leading 0s are allowed
     >>> is_bits('1a')
     >>> is_bits('1 0 1 1')   # no spaces (or punctuation)
  6. Complete from_bits(bits) so that it returns the integer corresponding to the given bitstring bits. Assumes bits is non-empty and consists only of ‘1’ and ‘0’. Examples:

     >>> from_bits('0')
     >>> from_bits('11101')
  7. Complete to_bits(n) so that it returns a bitstring representation of the nonnegative integer n. Examples:

     >>> to_bits(0)
     >>> to_bits(23)
  8. Complete read_binary_number_from_file(filename) so that it returns the integer corresponding to the bitstring stored in the file filename. Returns None (and prints an appropriate error message) if either the file is not a valid text file or if its contents are not entirely ones and zeroes. Example:

     In []: write_bits(53, 'some-bits')
     In []: read_binary_number_from_file('some-bits')
     Out[]: 53
  9. Complete find_next_word(s, start) so that it returns the index of the next occurrence of a nonwhitespace character in string s starting from index start. If no more exists, returns length of s. Use Python’s .isspace method for strings. Examples:

     >>> find_next_word('Now\nis   the  \n time', 0)
     >>> find_next_word('Now\nis   the  \n time', 4)
     >>> find_next_word('Now\nis   the  \n time', 9)
     >>> find_next_word('Now\nis   the  \n time', 16)
  10. Write count_words() from scratch. It should ask the user for a filename, use read to read the entire contents of a file into a single string, and use find_next_word repeatedly to get and display the number of words in the files.

  11. Complete increment(bits) so that it returns the bitstring corresponding to the binary number that immediately follows (is one greater than) the binary number represented by bits. Use reverse. Do not use from_bits and to_bits.