| Author: | Christopher Haynes |
|---|---|
| Email: | chaynes@indiana.edu |
| Affiliation: | Indiana University |
| Course: | BL CSCI A201 |
| Date: | 2008-04-11 |
Contents
>>> 1 + 2 statement . expression . . binary_operation . . . expression . . . . literal . . . . . integer: 1 . . . ____________________: + . . . ____________________ . . . . ____________________ . . . . . integer: 2
>>> 1 + 2 statement . expression . . binary_operation . . . expression . . . . literal . . . . . integer: 1 . . . binary_operator: + . . . expression . . . . literal . . . . . integer: 2
>>> a = 'Spam' + 'alot' statement . simple_variable_assignment . . name: a . . = . . expression . . . ____________________ . . . . expression . . . . . literal . . . . . . string: 'Spam' . . . . binary_operator: + . . . . expression . . . . . literal . . . . . . string: 'alot'
>>> a = 'Spam' + 'alot' statement . simple_variable_assignment . . name: a . . = . . expression . . . binary_operation . . . . expression . . . . . literal . . . . . . string: 'Spam' . . . . binary_operator: + . . . . expression . . . . . literal . . . . . . string: 'alot'
>>> a[1] = 2 statement . ____________________ . . indexing . . . expression . . . . variable_reference . . . . . ____________________: a . . . [ . . . ____________________ . . . . ____________________ . . . . . integer: 1 . . . ] . . = . . expression . . . literal . . . . integer: 2
>>> a[1] = 2 statement . indexed_assignment . . indexing . . . expression . . . . variable_reference . . . . . name: a . . . [ . . . expression . . . . literal . . . . . integer: 1 . . . ] . . = . . expression . . . literal . . . . integer: 2
>>> print 1 + 2 * 3 ____________________ . print_statement . . print . . expression . . . ____________________ . . . . ____________________ . . . . . literal . . . . . . integer: 1 . . . . ____________________: + . . . . expression . . . . . binary_operation . . . . . . expression . . . . . . . literal . . . . . . . . integer: 2 . . . . . . binary_operator: * . . . . . . expression . . . . . . . literal . . . . . . . . ____________________: 3
>>> 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
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--
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
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
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
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
If the documentation of a function says that it returns something (other than None), then its bodyAnswer: A
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 requiresAnswer: C
Which of the following code lines is either wrong or uses very misleading variable names?Answer: F
For me the syntax exercises were
For me the types exercises wereWrite 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.
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