In Class: Week 10, list algorithms

Create a week10 subdirectory in your cs21/class directory and copy over my starting point files:

    $ cd 
    $ cd cs21/class
    $ pwd

    $ mkdir week10        
    $ cd week10
    $ pwd

    $ cp ~newhall/public/cs21/week10/* .
After class, you can copy over my files by doing:
$ cp ~newhall/public/cs21/week10/tia* .

  1. We are going to continue to look at algorithms for lists of values and add some code to

  2. ascii: ascii is a numeric encoding of characters and is how characters are internally represented in the conputer. To see the ascii encodings, type the following at the unix prompt:
    $ man ascii
    Use the arrow keys to navigate, and 'q' to quit.

    You should never memorize the ascii encoding, but it is important to know that the encodings for 'a' to 'z' are contiguous, as are 'A' to 'Z' and '0' to '9'. Internallly, python uses the ascii encoding of characters to compare strings. For example, for the following expression:

    "ant" == "any"
    Python first comapares the ascii values the first character in each string (97 for 'a' to 97 for 'a'), since they are equal, python then compares the ascii value of the second character in each string (110 for 'n' to 110 for 'n'), since they are equal, python then compares the ascii value of the third character in each string (116 for 't' to 121 for 'y'), since the ascii values are not equal, python evaluates the expression to False.

    The ascii encoding also explains why the following relational expressions evaluate to True:

    "Zoo" < "ant"
    "zoo" > "ant"
    because the ascii encoding for 'Z' is less than that for 'a' (90 vs. 97) the string "Zoo" is less than the string "ant", and because the ascii value for 'z' is greater than for 'a' (122 vs. 97) the string "zoo" is "Zoo" < "ant" greater than the string "ant".

    You can use the ord function to get the ascii value for any character, and the chr function to get the character string for any numeric ascii value:

    print ord('a')   # prints 97
    print chr(99)    # prints the string c

    Let's try out a few things using what we know about ascii:

    1. Together let's write a program,, that asks the user to enter an int and then print out the ascii character encoded with the int value. So if the user enters the value 66, our program will print out the character B.

    2. Next, lets change the program so that the user enters a string value, and we print out all the characters and their ascii values in the string. Here is what a run of your program might look like:
      $ python
      Enter a phrase: hello there
      h : 104
      e : 101
      l : 108
      l : 108
      o : 111
        : 32
      t : 116
      h : 104
      e : 101
      r : 114
      e : 101

    3. We are going to write a new program,, that reads in a string and a shift value from the user, and encodes all lower case characters in the string by shifting their ascii value over by the shift value. So, if the the shift value is 3 and the string is "hello", your program will create a new string "khoor". We will ignore the case of negative input values for now.

      Remember you can create a list of all the characters in the string by calling the list constructor with the string as an argument:

      s = "hello"
      l = list(s)
      print l
      ['h', 'e', 'l', 'l', 'o']
      Since list elements can be modified, you can change elements in the list:
      l[2] = 't'
      And, you can create a string from a list of strings using the join method of the string class:
      new_s = "".join(l)
      print new_s
    4. Next, we are going to add a cyclic shift of alphabetic encodings. In this case, if the shift value is 3 and the string is "zoo", the encoded version is "crr" because the encoding for 'z' shifted by 3 with wrap-around goes to 'c'