CSC 111: Intro to Computer Science through Programming

Lab 7

Due: Friday, Mar. 31 at 11:59pm on Moodle

The goal of this lab is practice working with sets and dictionaries, and we will also be revisiting reading files. At the end, there will be some optional practice with while loops.

For this lab, first find your randomly assigned partner. Introduce yourselves - the person with the first name that comes last alphabetically should begin as the "driver", with the other partner as the "navigator". The driver will have the code open, and the navigator will have these instructions open.

At the end of the lab, email the code (finished or not) and transcript to the person who started as the navigator. If you do not finish during lab you have two options:

(1) Arrange to meet before Friday and finish the lab together.

(2) Continue the code separately and denote the part you did on your own with a comment.

Note: it is not an option for one person to complete the code on their own and then send the finished code to their partner to submit. Any code that you submit should be either written by you, or written by you and your partner while you were pair programming. Both partners should submit their code on Moodle.

Part A: Sets

To start out, type the following statements in the shell. Before running each statement, predict what the result will be, and make sure you understand what each step is doing.

code

Part B: Dictionaries

In this part of the lab, you'll be building a dictionary from a file, and then using your dictionary to decode a hidden message. The translation from this secret code to English is in the file dictionary.txt. The first string in each line is the binary (made of up 0's and 1's) code, and the second string is the English letter or symbol that it corresponds to. A portion of the dictionary file is shown below:

code

Download both the dictionary file, and the file encoded.txt which contains a message that has been encoded using this system. Your task is to decode this message. From the dictionary file, notice that each code is 6 digits long, so that we can read 6 digits at a time and get an English letter/symbol back. Here is an example:

code

To start out, create a new file called lab7.py in the same directory as your dictionary and encoded file. Then follow the steps below to decode this message.

  1. First, create a helper function that will take in a file name, and return a dictionary with they keys as the binary codes (in the form of strings), and the values as the English alphabet and associate symbols. Here is what the function's signature should look like. The single parameter should be the file name as a string, and the return value should be of type dictionary.

    lines

    As a reminder, here is an example of how to create a dictionary and add key/value pairs:

    lines

    In this example, they keys are the 99 numbers, and the values are student's names (these were my first two research students at Smith, but not their real 99 numbers!)

    As you are creating this helper function, make sure to print frequently to make sure everything is working the way you want it to. For reading the file, use a for loop over the lines of the file.

    Switch Driver and Navigator here!

  2. You may have noticed that in the dictionary file there is the word "space" instead of a space (" "). In your helper function from the previous step, fix this issue as a special case (using an if-statement to see if the second string is the word "space"). When you reach this case, have the dictionary value be a space character (" ") instead.

  3. Next, in the main method, we will decode the encoded file to obtain the message. This file is all on one line, so it is okay to read the entire file using .read(). Read this file in as a single string, and then think about how to read each set of 6 digits at a time so you can look them up in your dictionary. At the end, you should be able to print out the encoded message. (Also, make sure to call your helper function so you can obtain the dictionary for translating.)

    Switch Driver and Navigator here!

You do not need to submit a transcript, but save the decoded message as a plain text file called decode.txt.

Part C: While Loops (optional)

If you finish early, here is an exercise about while loops. The goal in this question is to convert a decimal number to binary (which is what I did to create the binary codes above). We are used to working in decimal (base 10), but numbers can be expressed in other bases as well. For example, take the number x=13. We can express x as the sum of powers of 10, i.e. 1 x 101 + 3 x 100 = 13. So the "tens" digit is 1, and the "ones" digit is 3. An analogous idea exists for binary (base 2). We can express 13 as the sum of powers of 2:

lines

For this question, we will write a function to convert any number in base 10 to binary. Here is an algorithm for doing so, which uses the concept of a while loop. The idea is to iteratively divide a number by 2 (until we get to 0), seeing what the remainder is each time. Here are the steps for 13:

  • 13 divided by 2 = 6 remainder 1
  • 6 divided by 2 = 3 remainder 0
  • 3 divided by 2 = 1 remainder 1
  • 1 divided by 2 = 0 remainder 1
So if we read the remainders backwards, we get 13 in binary: 1101. Try this in base 10 as well to convince yourself this process works! The challenge now is to implement this algorithm in a function, using a while loop to see how many times we should divide the number by 2. An example of how your function should work is shown below:

lines

I've encoded the binary numbers as strings here so that they can be "padded" with 0's to create a fixed-length code (you don't need to do that in this step though). If you finish this part, send me the code for your function. Thank you!

Submit

You do not have to submit a transcript for this lab. Make sure that both you and your partner have a copy of all the code written during the lab period, and decoded file. Both partners should submit the files:

  • lab7.py

  • decode.txt

on Moodle. If you did not finish, you can either meet to finish or finish separately. All labs must be submitted by Friday night. If you finish early, I encourage you to start on Homework 7, but you are also welcome to depart.