CSSE 120 -- Intro. to Software Development

Homework 6 — Comments and Hints

You will face a number of technical challenges in this problem. Here are some comments/hints.

  1. The encode function that we gave you uses the input function to get the secret message from the user (i.e., the message to encode).
  2. Re getting the input from the user in DECODE: You get it as a string, because that is how input works. You probably want it to be converted to a list, stripping away whatever separator you use between the numbers in the input. For example, if your input is the following (i.e., you chose to ask the user to input a comma-separated list of numbers):
       184, 181, 188, 188, 191, 112, 196, 184, 181, 194, 181
    
    then you probably want this input to be stored in a variable in your program as a list of numbers, like this:
       [184, 181, 188, 188, 191, 112, 196, 184, 181, 194, 181]
    
    There is no simple SINGLE line to accomplish this transformation from string-of-numbers to list-of-numbers. However, there is a simple TWO-step approach.
    1. Apply the strip method to the input string, to get the corresponding list with the separator characters stripped away. In the above example, that would produce:
         ['184', '181', '188', '188', '191', '112', '196', '184', '181', '194', '181']
      
    2. Note that the list elements are (alas) strings that represent numbers, not numbers themselves. So:
    3. When you loop through this list, remember to use int to convert the item from a string representing a number to a number itself.
    Here is some sample code and output from it to help you understand the string and int functions. Just copy-and-pasting this code into your DECODE will NOT work. You must APPLY the ideas and examples.
        stringOfNumbers = "120, 150, 75"
        listOfNumbersAsStrings = stringOfNumbers.split(",")
        firstNumberInListAsAString = listOfNumbersAsStrings[0]
        firstNumberInListAsANumber = int(firstNumberInListAsAString)
    
        print(stringOfNumbers)
        print(listOfNumbersAsStrings)
        print(firstNumberInListAsANumber + 1)
        print(firstNumberInListAsAString + 1) # WRONG - can't add 1 to a string, causes an error message
    
            the above produces as output: 
        
        
        120, 150, 75
        ['120', ' 150', ' 75']
        121
        
        
        Traceback (most recent call last):
           ...
        TypeError: Can't convert 'int' object to str implicitly
        
    
  3. The inverse of the ord function is the chr function. Here's an example:
        print("The Unicode number for 'p' is:", ord('p'))
        print("The character for the Unicode number 112 is:", chr(112))
        print("The character for the Unicode number 63 is:", chr(63))
        print("The Unicode number for '?' is:", ord('?'))
    
           the above produces as output: 
    
        
        The Unicode number for 'p' is: 112
        The character for the Unicode number 112 is: p
        The character for the Unicode number 63 is: ?
        The Unicode number for '?' is: 63
        
    
  4. Depending on how you implement the decode function, your decoded message might be a string (good!) or a list of characters (less good!). But converting the latter to the former is easy, using the join function that is the inverse of the split function. Here is an example of its use, to get you started:
        listOfCharacters = ['h', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e']
        sameThingAsAString = "".join(listOfCharacters)
        
        print(listOfCharacters)
        print(sameThingAsAString)
    
            the above produces as output: 
    
        
        ['h', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e']
        hello there
        
    
    This example shows only a little of the power of join. Please feel free to ask your instructor or csse120-staff@rose-hulman.edu if you want a complete explanation of join.