If you have not already done so, please complete the on-line course evaluation for your classes by visiting my.wsu.edu.

Image goes here
Homework 1
CptS 355 - Programming Language Design
Washington State University

Overview

Due Date: Friday, Sept. 18 by 11:59PM
In this homework you will explore use of some of the features of Python.

Turning in your assignment

Note the following directions carefully. Points will be deducted for incorrectly named files and functions.

Put your answers in a file named hwk1.txt, hwk1.doc, or hwk1.pdf having the appropriate type of content -- plain text, MS Word document, or Adobe PDF. Turn in the file at the turn-in page. You may turn in your assignment as many times as you like. Only the last one submitted before the deadline will be graded.

The work you turn in is to be your own personal work. You may not work together with another student, copy code from the web, or anything else that lets you avoid solving the problems for yourself. If you are stuck contact the TA or me, not other students.

Grading

The assignment will be marked for correct answers and appropriate use of Python language features.

Problems

In the following problems first try to figure out the answer, then use the documentation to figure out the answer, and only then use the Python interpreter to confirm that you have a correct answer.
  1. Assume that L has been assigned a value as follows:
       L = [1, 2, 3, 4, 5, 6]
    
    What are the values of
    1. L[0]
    2. L[-1]
    3. L[1:2]
    4. L[3:]
    5. L[:3]
    6. L[:]
    7. L[::-1]
  2. What is different between L[1:2] and L[1]?
  3. Slice assignment: assuming L is initialized as above, what is the value of L after executing
       L[2:4] = []
    
    Does slice assignment work for strings? Why not?
  4. What is the difference between
       L.append(7)
    
    and
       L = L + [7]
    
    Hint: try assigning L2 = L and see what happens to L2 in the two cases.
  5. Again assume that L is initialized as in problem 1. What is enumerate(L)? What is list(enumerate(L))?
  6. Define a function listPrint(L) that prints the elements of a list, one per line. (Remember my admonition to prefer for loops to while loops.)
  7. Define a function listPrint2(L) that prints the elements of a list all on one line separated by a single space. Recall that if the last value passed to a print statement is followed by a comma no newline character is printed.
  8. Assume that D has been assigned a value as follows:
       D = { 't': 2, 'e': 1, 's': 1, }
    
    (Note: the trailing comma is allowed and putting it in makes subsequent editing easier.) What is D['t']? What is D['a']?
  9. D['a'] produced an error. The get method on dicionaries allows you to specify a default value to return instead of an error. So D.get('a', 0) returns 0. Define a function charCount(s) that counts the number of occurrences of each different character in a string, returning a dictionary that maps each character to its count. For example, the dictionary D defined above is returned by charCount('test'). Your function should contain no if statements -- use the dictionary get method.
(c) 2003 Curtis Dyreson, (c) 2004-2006 Carl H. Hauser           E-mail questions or comments to Prof. Carl Hauser