Strict Standards: Declaration of Doku_Renderer_metadata::table_open() should be compatible with Doku_Renderer::table_open($maxcols = NULL, $numrows = NULL, $pos = NULL) in /home2/cp-wiki/htdocs/inc/parser/metadata.php on line 480

Strict Standards: Declaration of Doku_Renderer_metadata::table_close() should be compatible with Doku_Renderer::table_close($pos = NULL) in /home2/cp-wiki/htdocs/inc/parser/metadata.php on line 480

CS 190C LAB 3: Friday, January 30, 2009

Demo Problems

def main():
    f = open('actor2.txt', 'r')
    previous_year = ''
    for line in f:
        nomination = line.split(',')
        if nomination[0] != previous_year:
            print nomination[1]
        previous_year = nomination[0]
    f.close()
 
main()

List and string mutability

If something is mutable, it means that it can be changed. In Python, lists are mutable, but strings are not. Example:

>>> x = 'hello'
>>> x
'hello'
>>> x[0] = 'j'
Traceback (most recent call last):
  File "<pyshell#22>", line 1, in <module>
    x[0] = 'j'
TypeError: 'str' object does not support item assignment

If we convert x into a list of characters instead, we can indeed change the h to a j:

>>> x = list(x)
>>> x
['h', 'e', 'l', 'l', 'o']
>>> x[0] = 'j'
>>> x
['j', 'e', 'l', 'l', 'o']

The caveat is now our string is not a string. To convert it back:

>>> x = ''.join(x)
>>> x
'jello'

input() versus raw_input()

raw_input(), like input(), can be used to retrieve input. However, raw_input() behaves differently:

  • raw_input() always returns a string
  • the input to raw_input() does not have to be a legal Python expression
  • raw_input() is often used to obtain text input from a user. If you use input(), in order to get text input, you must surround your text with quotes.
>>> x = raw_input('input something: ')
input something: test
>>> x
'test'
>>> x = input('input something: ')
input something: 'test'
>>> x
'test'
>>> x = raw_input('input something: ')
input something: 0.5
>>> x
'0.5'
>>> x = input('input something: ')
input something: 0.5
>>> x
0.5

String comparison

Case-sensitive string comparison can be done in the same way you compare numbers:

>>> 'bob' == 'bob'
True
>>> 'bob' == 'BOB'
False

For today's problem, however, you need to deal with strings that may not be in the same case. If the user inputs “bob”, you still want it to be basically the same as “BOB”. One easy way is to compare the lowercase version of these strings:

>>> x = 'bob'
>>> y = 'bob'
>>> z = 'BOB'
>>> x.lower() == y.lower()
True
>>> x.lower() == z.lower()
True

In-Lab Problem

Download one of the files to use as your database:

actor2.txt actress2.txt

  1. Search your file of nominations and print out the nominations or awards that the actor received. You should clarify whether or not it is a nomination or an award. The first nomination of each year is the award for that year, since it isn't explicitly specified in the file. The search should not be case sensitive. For example, if the user types in “tom hanks”, it should still match nominations for “Tom Hanks”. Your output may look like this:
    Enter the actor: tom hanks
    Tom Hanks was nominated for an Oscar in 1988 for Big as Josh Baskin
    Tom Hanks won an Oscar in 1993 for Philadelphia as Andrew Beckett
    Tom Hanks won an Oscar in 1994 for Forrest Gump as Forrest Gump
    Tom Hanks was nominated for an Oscar in 1998 for Saving Private Ryan as Captain John H. Miller
    Tom Hanks was nominated for an Oscar in 2000 for Cast Away as Chuck Noland
  2. Lastly, since there is no reason why you need the entire list of Oscar nominees at once, you should process each line one at a time, rather than loading all of the entries before doing a search.

Solution

def main():
    f = open('actor2.txt', 'r')
    prev_year = 0
    actor = raw_input('Enter the actor: ')
    for line in f:
        nomination = line.split(',')
        nomination[0] = int(nomination[0])
        if nomination[1].lower() == actor.lower():
            if prev_year != nomination[0]:
                print nomination[1], 'won an Oscar in', nomination[0], 'for', nomination[2]
            else:
                print nomination[1], 'was nominated for an Oscar in', nomination[0], 'for', nomination[2]
        if nomination[0] != prev_year:
            prev_year = nomination[0]
 
    f.close()
main()
 
cs190c/lab3_09.txt · Last modified: 2009/02/05 02:34 by tang
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki