Week 13

Review

Author:Christopher Haynes
Email:chaynes@indiana.edu
Affiliation:Indiana University
Course:BL CSCI A201
Date:2008-04-11
Copyright © 2008, Christopher Haynes—all rights reserved.

Syntax exercise 1

>>> 1 + 2
statement
.  expression
.  .  binary_operation
.  .  .  expression
.  .  .  .  literal
.  .  .  .  .  integer: 1
.  .  .  ____________________: +
.  .  .  ____________________
.  .  .  .  ____________________
.  .  .  .  .  integer: 2

Solution 1

>>> 1 + 2
statement
.  expression
.  .  binary_operation
.  .  .  expression
.  .  .  .  literal
.  .  .  .  .  integer: 1
.  .  .  binary_operator: +
.  .  .  expression
.  .  .  .  literal
.  .  .  .  .  integer: 2

Syntax exercise 2

>>> a = 'Spam' + 'alot'
statement
.  simple_variable_assignment
.  .  name: a
.  .  =
.  .  expression
.  .  .  ____________________
.  .  .  .  expression
.  .  .  .  .  literal
.  .  .  .  .  .  string: 'Spam'
.  .  .  .  binary_operator: +
.  .  .  .  expression
.  .  .  .  .  literal
.  .  .  .  .  .  string: 'alot'

Solution 2

>>> a = 'Spam' + 'alot'
statement
.  simple_variable_assignment
.  .  name: a
.  .  =
.  .  expression
.  .  .  binary_operation
.  .  .  .  expression
.  .  .  .  .  literal
.  .  .  .  .  .  string: 'Spam'
.  .  .  .  binary_operator: +
.  .  .  .  expression
.  .  .  .  .  literal
.  .  .  .  .  .  string: 'alot'

Syntax exercise 3

>>> a[1] = 2
statement
.  ____________________
.  .  indexing
.  .  .  expression
.  .  .  .  variable_reference
.  .  .  .  .  ____________________: a
.  .  .  [
.  .  .  ____________________
.  .  .  .  ____________________
.  .  .  .  .  integer: 1
.  .  .  ]
.  .  =
.  .  expression
.  .  .  literal
.  .  .  .  integer: 2

Solution

>>> a[1] = 2
statement
.  indexed_assignment
.  .  indexing
.  .  .  expression
.  .  .  .  variable_reference
.  .  .  .  .  name: a
.  .  .  [
.  .  .  expression
.  .  .  .  literal
.  .  .  .  .  integer: 1
.  .  .  ]
.  .  =
.  .  expression
.  .  .  literal
.  .  .  .  integer: 2

Syntax exercise

>>> print 1 + 2 * 3
____________________
.  print_statement
.  .  print
.  .  expression
.  .  .  ____________________
.  .  .  .  ____________________
.  .  .  .  .  literal
.  .  .  .  .  .  integer: 1
.  .  .  .  ____________________: +
.  .  .  .  expression
.  .  .  .  .  binary_operation
.  .  .  .  .  .  expression
.  .  .  .  .  .  .  literal
.  .  .  .  .  .  .  .  integer: 2
.  .  .  .  .  .  binary_operator: *
.  .  .  .  .  .  expression
.  .  .  .  .  .  .  literal
.  .  .  .  .  .  .  .  ____________________: 3

Solution

>>> print 1 + 2 * 3
statement
.  print_statement
.  .  print
.  .  expression
.  .  .  binary_operation
.  .  .  .  expression
.  .  .  .  .  literal
.  .  .  .  .  .  integer: 1
.  .  .  .  binary_operator: +
.  .  .  .  expression
.  .  .  .  .  binary_operation
.  .  .  .  .  .  expression
.  .  .  .  .  .  .  literal
.  .  .  .  .  .  .  .  integer: 2
.  .  .  .  .  .  binary_operator: *
.  .  .  .  .  .  expression
.  .  .  .  .  .  .  literal
.  .  .  .  .  .  .  .  integer: 3

Type exercise instructions

Under every expression, indexing, variable reference, and parameter write its type (LIST, INT, STR, CHAR (one character string), FUN (function), METHOD, etc). Write ANY if the type does not matter.

If there are subexpressions, their types go on a previous line.

Use a dash to indicate the span of long expressions.

Example:

a =   1 * 2.4
FLOAT INT FLOAT
      FLOAT--

Type exercise

def every_other(a):

    """
    Return a list consisting of every other value in lst, starting with the
    first.
    >>> every_other(['a', 1, 'b', 2, 'c'])
    ['a', 'b', 'c']
    >>>
    """
    b = []

    c = 0

    while c < len(a):


        b.append(a     [c])



        c = c + 2


    return b

Type exercise solution

def every_other(a):
                LIST
    """
    Return a list consisting of every other value in lst, starting with the
    first.
    >>> every_other(['a', 1, 'b', 2, 'c'])
    ['a', 'b', 'c']
    >>>
    """
    b =  []
    LIST LIST
    c = 0
    INT INT
    while c < len(a):
          INT FUN LIST
              INT---
          BOOL------
        b.append(a    [c])
        LIST     LIST INT
        METHOD-- ANY-----
        NONETYPE-----
        c = c + 2
        INT INT INT
            INT----
    return b
           LIST

Type exercise

def lower(a):

    """Return a string that is like the given string, but with all
    upper-case letters converted to lower-case.
    >>> lower("The Knights of Ni")
    'the knights of ni'
    >>>
    """
    b = 0

    c   =  ''

    while b < len(a):




        d  = a      [b]


        if 'A' <= d and d <= 'Z':



            d =  chr(ord(d) + UPPER_CASE_SHIFT)




        c = c + d


        b = b + 1


    return c

Type exercise solution

def lower(a):
          STR
    """Return a string that is like the given string, but with all
    upper-case letters converted to lower-case.
    >>> lower("The Knights of Ni")
    'the knights of ni'
    >>>
    """
    b = 0
    INT INT
    c   =  ''
    STR STR
    while b < len(a):
          INT FUN STRING
              INT---
          BOOL------
        d  = a      [b]
        CHAR STRING INT
             CHAR------
        if 'A' <= d and d <= 'Z':
           CHAR   CHAR  CHAR CHAR
           BOOL----     BOOL----
           BOOL-----------------
            d =  chr(ord(d) + UPPER_CASE_SHIFT)
            CHAR FUN FUN CHAR  INT
                    INT--
                    INT----------------------
                 CHAR------------------------
        c = c + d
        STR STR CHAR
            STR--
        b = b + 1
        INT INT INT
            INT--
    return c
           STR

FYI: static typing

clicker If the documentation of a function says that it returns something (other than None), then its body

  1. must have at least one return statement
  2. may or may not have a return statement, depending on whether it uses print
  3. needs a return statement only if it needs to exit from somewhere other than the end of the function body

Answer: A

clicker If the documentation of a function says that it does something with "corresponding" elements of two or more sequences, and you are not using recursion, you know that its implementation requires

  1. a while loop
  2. a for loop
  3. an indexed loop
  4. a while True loop
  5. none of the above

Answer: C

clicker Which of the following code lines is either wrong or uses very misleading variable names?

  1. for count in range(n):
  2. for value in range(len(lst)):
  3. for index in range(len(lst)):
  4. for index in range(lst):
  5. all of the above
  6. B and D
  7. none of the above

Answer: F

clicker For me the syntax exercises were

  1. very helpful
  2. somewhat helpful
  3. maybe helpful
  4. not at all helpful

clicker For me the types exercises were

  1. very helpful
  2. somewhat helpful
  3. maybe helpful
  4. not at all helpful

CSV file exercise

Write a function csv_column that takes the name of a CSV file and a column number (one-based indexing) and prints the elements in the column.

Recall: list(csv.reader(csv_file_object)) returns table representing the contents of the CSV file.

CSV file solution

import csv

def csv_column(csv_file_name, column_number):
    """Print the strings in the indicated column (one-based indexing) in
    the named CSV file, excluding empty strings.
    """
    # Hint: list(csv.reader(<file object>))
    csv_file = file(csv_file_name, 'r')
    table = list(csv.reader(csv_file))
    csv_file.close()
    column_number = column_number - 1
    for index in range(len(table)):
        row = table[index]
        value = row[column_number]
        if value.strip() != '':
            print value