CS8 F17
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h10: Perkovic 6.3-6.4 (Encodings, random)

ready? assigned due points
true Tue 11/07 08:00AM Tue 11/14 08:00AM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

There is NO MAKEUP for missed assignments, and you may not submit work in advance, or on behalf of another person.
In place of that, we drop the four lowest scores (if you have zeros, those are the four lowest scores.)


Please read Perkovic 6.3-6.4 (Encodings, random). Then complete these problems and turn in your completed homework during your registered lab section..

  1. ( pts) Please fill in the information at the top of this homework sheet, as usual. WRITE DARK, and remember, if you MUST submit it on multiple sheets, JUST write your name at the top of both sheets and turn in both sheets UNCONNECTED. No staples, paper clips, fold/tear etc or anything that would jam up the scanner.

  2. Section 6.3 discusses character encodings, a topic that usually arises in the context of dealing with strings, and files. Two encodings are discussed: ASCII and Unicode. The text also discusses various ways of representing Unicode characters including utf-8, utf-16, and utf-32.

    1. (10 pts) The original 7-bit ASCII character set has 128 different characters, numbered 0 through 127, but not all of them are “printable” characters. According to our text, how many of the ASCII characters with codes 0 through 127 are considered “printable”?

    2. (10 pts) Fill in the blanks in the following sequence of Python code so that the code prints out all of the lowercase characters ‘a’ through ‘z’:

      a b c d e f g h i j k l m n o p q r s t u v w x y z 

      Your answer should only involve filling in the blanks; not modifying the rest of the code in anyway. As a reminder, end=' ' on a function call to print makes the print NOT go to a new line; instead, it just prints a space after eachcall to print (See Section 4.2, p. 99 in the textbook.)

      for i in range ( ________ , ________ ) :
         print(chr(i),end=' ')
  3. Indicate what the output of the code will be:

    for c in "UCSB":
    (10 pts)
    print ('\u0048\u0049\u0021')
    (10 pts)
  4. (10 pts) Write a Python expression that returns an random integer between 5 and 10.

  5. Suppose the python variable ucList is defined as follows:

    ucList=[ "UCB", "UCD", "UCI", "UCLA", "UCM", "UCR", "UCSB", "UCSC", "UCSD" ]
    1. (10 pts) Write a Python expression that returns the name of a random UC from this list (as a string). For full credit, use the most straightforward way (that way doesn’t involve calculating the length of the list.)

    2. (10 pts) Write a Python expression that returns a pair of random UCs from this list (i.e. a list of length 2 that has two randomly selected UCs from the list. Do it in the most straightforward way possible. (If you read the whole section carefully, you’ll discover this can be done in one line of code.)

    3. (10 pts) Section 6.3 explains the idea of lexicograpic order. As it turns out, the list ucList is already in lexicographic order, though if it were not, we could put it in lexicographic order by writing ucList.sort().

      What is lexicographic order?

    4. (20 pts) Suppose that someone proposed that instead of putting the UCs in alphabetical order, to randomize the order (so that “UCB” wouldn’t always be first, and “UCSD” wouldn’t always be last.)

      Write some Python code that would print the UC abbreviations, one per line, in a random order. Use the most straightforward way of doing it.