CSC111 Homework 9 Solution

From CSclasswiki
Jump to: navigation, search

--Thiebaut 23:02, 18 April 2009 (UTC)


There were several very good programs submitted for HW #9. This one is taken for its clear style and simplicity.

# -*- coding: utf-8 -*-
# hw9b.py
# Sarah Morse (edited by D. Thiebaut)
# April 13, 2009
# CSC 111, HW9b
#
# This program takes a list of 80 movies from the imdb database (www.imdb.com) 
# with their ratings and the years they were made, and puts them into two separate lists. 
# The first list contains the movies listed that appeared in  2000 or later, sorted by rating.  
# The second list is the top ten oldest movies sorted alphabetically. 
# This version of the program is robust and attempts to gather movie information from
# lines that may be corrupted.  If the program does not find expected information, it skips
# the corrupted line.


def getList():

    # processes list by splitting it by line and then by words
    # creates ordered lists for later use
    text="""
1.	9.1	The Shawshank Redemption (1994)	417,427
2.	9.1	The Godfather (1972)	347,404
3.	9.0	The Godfather: Part II (1974)	201,520
4.	8.9	Buono, il brutto, il cattivo., Il (1966)	122,618
5.	8.9	Pulp Fiction (1994)	342,899
6.	8.9	The Dark Knight (2008)	359,778
7.	8.8	Schindler's List (1993)	227,442
8.	8.8	One Flew Over the Cuckoo's Nest (1975)	174,583
9.	8.8	12 Angry Men (1957)	88,588
10.	8.8	Star Wars: Episode V - The Empire Strikes Back (1980)	235,660
11.	8.8	Casablanca (1942)	143,348
12.	8.8	Star Wars (1977)	277,431
13.	8.8	Shichinin no samurai (1954)	81,491
14.	8.8	The Lord of the Rings: The Return of the King (2003)	302,350
15.	8.7	Goodfellas (1990)	188,381
16.	8.7	Rear Window (1954)	98,431
17.	8.7	Cidade de Deus (2002)	130,423
18.	8.7	Raiders of the Lost Ark (1981)	209,849
19.	8.7	C'era una volta il West (1968)	58,226
20.	8.7	The Lord of the Rings: The Fellowship of the Ring (2001)	330,321
21.	8.7	Fight Club (1999)	310,927
22.	8.7	The Usual Suspects (1995)	227,103
23.	8.7	Psycho (1960)	119,151
24.	8.6	The Silence of the Lambs (1991)	204,638
25.	8.6	Sunset Blvd. (1950)	43,907
26.	8.6	Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb (1964)	134,407
27.	8.6	Memento (2000)	223,870
28.	8.6	North by Northwest (1959)	78,221
29.	8.6	The Matrix (1999)	319,235
30.	8.6	It's a Wonderful Life (1946)	86,397
31.	8.6	Citizen Kane (1941)	120,280
32.	8.6	Se7en (1995)	227,549
33.	8.6	The Lord of the Rings: The Two Towers (2002)	275,929
34.	8.6	Léon (1994)	160,004
35.	8.6	Apocalypse Now (1979)	146,618
36.	8.5	American Beauty (1999)	248,258
37.	8.5	Taxi Driver (1976)	126,952
38.	8.5	American History X (1998)	181,671
39.	8.5	Lawrence of Arabia (1962)	67,953
40.	8.5	Vertigo (1958)	76,366
41.	8.5	WALL·E (2008)	126,539
42.	8.5	Forrest Gump (1994)	243,212
43.	8.5	Paths of Glory (1957)	36,503
44.	8.5	Fabuleux destin d'Amélie Poulain, Le (2001)	148,831
45.	8.5	M (1931)	32,579
46.	8.5	Double Indemnity (1944)	29,983
47.	8.5	Slumdog Millionaire (2008)	107,219
48.	8.5	To Kill a Mockingbird (1962)	70,011
49.	8.5	Alien (1979)	148,931
50.	8.5	A Clockwork Orange (1971)	160,929
51.	8.5	The Treasure of the Sierra Madre (1948)	27,133
52.	8.5	The Departed (2006)	209,970
53.	8.5	Das Leben der Anderen (2006)	53,927
54.	8.5	The Shining (1980)	144,423
55.	8.5	The Third Man (1949)	42,601
56.	8.4	The Pianist (2002)	104,918
57.	8.4	Chinatown (1974)	64,876
58.	8.4	Saving Private Ryan (1998)	227,130
59.	8.4	Eternal Sunshine of the Spotless Mind (2004)	178,991
60.	8.4	Sen to Chihiro no kamikakushi (2001)	79,807
61.	8.4	Terminator 2: Judgment Day (1991)	189,600
62.	8.4	Aliens (1986)	141,368
63.	8.4	Requiem for a Dream (2000)	149,090
64.	8.4	L.A. Confidential (1997)	142,194
65.	8.4	Monty Python and the Holy Grail (1975)	134,036
66.	8.4	Das Boot (1981)	60,070
67.	8.4	City Lights (1931)	21,284
68.	8.4	The Bridge on the River Kwai (1957)	49,726
69.	8.4	Reservoir Dogs (1992)	175,494
70.	8.4	Laberinto del fauno, El (2006)	120,903
71.	8.4	Rashômon (1950)	31,753
72.	8.4	The Wrestler (2008)	44,400
73.	8.4	Raging Bull (1980)	74,394
74.	8.4	The Maltese Falcon (1941)	43,535
75.	8.4	All About Eve (1950)	29,532
76.	8.4	Modern Times (1936)	28,481
77.	8.3	Singin' in the Rain (1952)	46,328
78.	8.3	Gran Torino (2008)	53,632
79.	8.3	Der Untergang (2004)	64,126
80.	8.3	Metropolis (1927)	31,952
        8.3     Some Like It Hot (1959) 56,171
81.     8.3     Rebecca (1940) 29,139
83.     8.3     Amadeus  [1984] 81 316         
84.     8.3     The Prestige               (2006)       156,204
85.     8.3     The Elephant Man (1980)  47,003
86.     8.3     Sin City (2005)   210,757

"""
   
    # split the long string at the ends of lines
    lines = text.split( "\n" )

    # create two empty lists
    list1 = []
    list2 = []
    for line in lines:
        words = line.split()  # split at spaces

        # if we don't have words, it's an ill-formed sentence, skip it
        if len( words ) == 0:
            continue

        # if the word at index one is more than 3 digits/letters, skip it
        if len(words[1])>3:
            continue

        # find title and score using the indexes of the split words
        title = " ".join( words[2:-2] )
        score = float(words[1])

        # find year using parentheses
        parenIndex  = line.find( '(' )
        parenIndex2 = line.find( ')' , parenIndex )

        # if there are no parentheses to mark a date, skip it
        if parenIndex==-1 or parenIndex2==-1:
            continue

        year = int( line[ parenIndex+1: parenIndex2 ] )

        #if there is no comma in the number, skip it
        if line.find(',')==-1:
            continue

        # add split words in different orders to make it easier to sort
        list1.append(  [score, title, year]  )
        list2.append( [year, title, score] )
 
    return list1, list2

def newMovie(list, newYear):
    # input list and the year you would like to print top movies since that year
    print "\nTop Movies from 2000-2008:"
    print "----------------------------------\n"
    for (score, title, year) in list:
        if year >= newYear:
            print title, score, year

def oldMovie(list):
    # input list, make a new list of the top ten oldest movies
    print "\nTop Ten Oldest Movies:"
    list.sort()
    newList=[]
    for i in range( 10 ):
        (score, title, year) = list[i]
        newList.append([title, score, year])
    return newList

def oldMovieSort(list):
    # input list, sort the top ten oldest movies alphabetically
    print "\nTen oldest movies in the list"
    print "---------------------------------\n"
    list.sort()
    for (title, year, score) in list:
        print title, score, year


def main():
    # get the two lists
    list1, list2=getList()

    # input list1 and year to get new movie list
    newMovie(list1, 2000)
 
    # input list2 to get top ten oldest movies
    list3=oldMovie(list2)
   
    # use list3 from above to arrange top ten movies alphabetically
    oldMovieSort(list3)

main()