Introduction to Computer Programming: Lab 4

Bitstrings and Files

Instructions

• Work on this lab with your partner (or on your own if you have not been assigned one).

• Work from the supplied starter file.

• It may be helpful to download and save this sample text file (`sample.txt`) into your working folder.

• Unless otherwise indicated, use only the Python operations and commands we have described thus far in class.

• Show your work for a given problem to the instructor or one of the lab assistants before moving on to the next problem, but do not get too bogged down on any one problem.

• In the common case, you will not complete the exercises during our scheduled lab. It is then up to you to complete the lab on your own time.

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

`````` >>> reverse('Hello')
'olleH'``````
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, ' ')
3
>>> find_next('Now is the time.', 4, ' ')
6
>>> find_next('Now is the time.', 7, ' ')
10
>>> find_next('Now is the time.', 11, ' ')
16``````
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)
1
0
1
0
1
1``````

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('')
False
>>> is_bits('10011')
True
>>> is_bits('0011')   # leading 0s are allowed
True
>>> is_bits('1a')
False
>>> is_bits('1 0 1 1')   # no spaces (or punctuation)
False``````
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')
0
>>> from_bits('11101')
29``````
7. Complete `to_bits(n)` so that it returns a bitstring representation of the nonnegative integer `n`. Examples:

`````` >>> to_bits(0)
'0'
>>> to_bits(23)
'10111'``````
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')

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)
4
>>> find_next_word('Now\nis   the  \n time', 4)
9
>>> find_next_word('Now\nis   the  \n time', 9)
16
>>> find_next_word('Now\nis   the  \n time', 16)
20``````
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`.