Assignment 7 Due by Friday, May 5th, midnight
Transcription
Assignment 7 Due by Friday, May 5th, midnight
Assignment 7 Due by Friday, May 5th, midnight Instructions This is your The assignment will be graded out of 100 points. Create a text document for your answers. Please use Python 3 (3.4 is the current version) Once you type your answers using your preferred word processor, generate a TEXT (.txt) file which has to have the named as name [yourname]_assignment7. The .txt has to include the following information on the first page: your first, last name and your UTA ID. The code submitted in the questions that require code to be written, should be exactly the same with the code you wrote in your python environment (i.e. IDLE). We will be copying-pasting your code from the text document to our python installation. Upload your file via the Blackboard. Task 1 (5 pts.) This is similar to our in-class example. Write a function count_letters (in_file, out_file) that: Takes two filenames (in_file and out_file) as arguments: Opens and reads the input file specified by in_file, and counts the number of occurrences of each letter (in a case-insensitive manner). Writes the result in the output file specified by out_file. Each line of the output file should be a letter, followed by a space, followed by the number of occurrences for that letter. Letters should appear in alphabetical order (the program should NOT separate upper-case from lower-case letters). For example: a 12 b5 c 21 ... Task 2 (15 pts.) Below is a list of albums that are in Rolling Stone Magazine's list of top 100 albums of all time. Each line contains the name of a band, the name of an album, and the date the album was released. Save this list in a file, and save it as albums.txt. Design a function read_albums(filename) that: Takes the filename as an argument. Reads the file and counts, for each band, how many albums of that band are listed in the file. Prints in descending order the number of albums, a line for each band. Each line should contain the name of the band, followed by a colon and space, and then the number of albums for that band. band1: number1 band2: number2 band3: number3 … START OF FILE Beatles - Revolver (1966) Nirvana - Nevermind (1991) Beatles - Sgt Pepper's Lonely Hearts Club Band (1967) U2 - The Joshua Tree (1987) Beatles - The Beatles (1968) Beatles - Abbey Road (1969) Guns N' Roses - Appetite For Destruction (1987) Radiohead - Ok Computer (1997) Led Zeppelin - Led Zeppelin 4 (1971) U2 - Achtung Baby (1991) Pink Floyd - Dark Side Of The Moon (1973) Michael Jackson -Thriller (1982) Rolling Stones - Exile On Main Street (1972) Clash - London Calling (1979) U2 - All That You Can't Leave Behind (2000) Weezer - Pinkerton (1996) Radiohead - The Bends (1995) Smashing Pumpkins - Mellon Collie And The Infinite Sadness (1995) Pearl Jam - Ten (1991) Beach Boys - Pet Sounds (1966) Weezer - Weezer (1994) Nirvana - In Utero (1993) Beatles - Rubber Soul (1965) Eminem - The Eminem Show (2002) R.E.M. - Automatic For The People (1992) Radiohead - Kid A (2000) Tool - Aenima (1996) Smashing Pumpkins - Siamese Dream (1993) Madonna - Ray Of Light (1998) Rolling Stones - Sticky Fingers (1971) Pink Floyd - The Wall (1979) Bruce Springsteen - Born To Run (1975) Oasis - What's The Story Morning Glory? (1995) Bob Dylan - Blonde On Blonde (1966) Red Hot Chili Peppers - Blood Sugar Sex Magik (1991) Who - Who's Next (1971) Eminem - The Marshall Mathers Lp (2000) Green Day - Dookie (1994) Bob Dylan - Blood On The Tracks (1975) Jeff Buckley - Grace (1994) Oasis - Definitely Maybe (1994) Metallica - Metallica (1991) Fleetwood Mac - Rumours (1977) Jimi Hendrix - Are You Experienced? (1967) Red Hot Chili Peppers - Californication (1999) Guns N' Roses - Use Your Illusion 1 & 2 (1991) Alanis Morissette - Jagged Little Pill (1995) Bob Dylan - Highway 61 Revisited (1965) U2 - War (1983) Pearl Jam - Pearl Jam Vs. Pearl Jam (1993) Led Zeppelin - Led Zeppelin 2 (1969) Madonna - Music (2000) U2 - The Unforgettable Fire (1984) Dave Matthews Band - Crash (1996) Nirvana - Unplugged In New York (1994) David Bowie - The Rise And Fall Of Ziggy Stardust (1972) Strokes - Is This It (2001) Linkin Park - Hybrid Theory (2001) Black Sabbath - Paranoid (1970) Ac/Dc - Back In Black (1980) Miles Davis - Kind Of Blue (1959) Madonna - Like A Prayer (1989) Bruce Springsteen - Darkness On The Edge Of Town (1978) Bruce Springsteen - Born In The Usa (1984) Who - Tommy (1969) Prince - Purple Rain (1984) Rage Against The Machine - Rage Against The Machine (1992) Rolling Stones - Let It Bleed (1969) U2 - Zooropa (1993) Dave Matthews Band - Under The Table And Dreaming (1994) System Of A Down - Toxicity (2001) Michael Jackson - Off The Wall (1979) Sex Pistols - Never Mind The Bollocks (1977) Counting Crows - August And Everything After (1993) Marvin Gaye - What's Going On (1971) Pixies - Doolittle (1990) No Doubt - Tragic Kingdom (1995) Velvet Underground + Nico - Velvet Underground + Nico (1967) Soundgarden - Superunknown (1994) Depeche Mode - 101 (1989) Pearl Jam - Vitalogy (1994) Queen - A Night At The Opera (1975) Led Zeppelin - Houses Of The Holy (1973) Van Morrison - Astral Weeks (1968) Bon Jovi - Slippery When Wet (1986) Smiths - The Queen Is Dead (1986) Metallica - Master Of Puppets (1986) Bob Dylan - Bringing It All Back Home (1965) Who - Quadrophenia (1973) Weezer - Maladroit (2002) Garbage - Version 2.0 (1998) Bob Marley - Legend (1984) Def Leppard - Hysteria (1987) Moby - Play (1999) Stevie Wonder - Songs In The Key Of Life (1976) Beck - Odelay (1996) Jimi Hendrix - Electric Ladyland (1968) Madonna - The Immaculate Collection (1990) Pink Floyd - Wish You Were Here (1975) END OF FILE Task 3 (10 pts.) Mr. Style is needing a program to help him get dressed in the morning. He needs a function, dress_me, that: Receives 3 lists as arguments: o One list of shirt colors o One list of tie colors o One list of suit colors Returns a list of all possible combinations of shirts, ties, and suits For example, if he ran: shirts = ['white', 'blue'] ties = ['purple', 'yellow'] suits = ['grey', 'blue'] combinations = dress_me(shirts, ties, suits) for combo in combinations: print combo It would print something like: ('white', 'purple', 'grey') ('white', 'purple', 'blue') ('white', 'yellow', 'grey') ('white', 'yellow', 'blue') ('blue', 'purple', 'grey') ('blue', 'purple', 'blue') ('blue', 'yellow', 'grey') ('blue', 'yellow', 'blue') Write a function that meets Mr. Style's requirements. The lists can be arbitrarily long (i.e., they can have more than 3 elements). Hint: one approach is to use nested for loops. Task 4 (15 pts.) The file system on a computer can be represented as a tree. The directories are connected with branches and the files are the leaves on the tree. Consider the following directory structure: ___________[C:]___________________ | | | __[dir1]____________ file6 [dir5] | | | | file1 [dir2] __[dir4]_____ file7 | | | | [dir3] file3 file4 file5 | file2 This directory structure can be represented by nested lists in which each directory is a list containing files and/or other directories (also represented as lists). For example, the above directory structure could be represented as: C = [ ['file1', [ ['file2'] ], ['file3', 'file4', 'file5'] ], 'file6', ['file7'] ] # | | | |_______| | |_________________________| | |_______| | # | | | dir3 | dir4 | dir5 | # | | |___________| | | # | | dir2 | | # | |____________________________________________________| | # | dir1 | # |____________________________________________________________________________| C: The C directory is the external list. Inside the C list is another list representing dir1, file6, and another list representing dir5. Each of those directories contains either more lists representing subdirectories, files, or both. Write a function that traverses a tree structure represented by such nested lists and prints the filenames one per line. The tree can be arbitrarily deep and each directory may or may not contain files or other directories. Here are a few example lists and their respective output: # Input 1 C = [['file1', [['file2']], ['file3', 'file4', 'file5']], 'file6', ['file7']] # Output 1 file1 file2 file3 file4 file5 file6 file7 # Input 2 C = [[['answers.txt'], 'task12.py', [['hello.txt'], 'pagefile.sys']], 'explorer.exe'] # Output 2 answers.txt task12.py hello.txt pagefile.sys explorer.exe # Input 3 C = ['a.txt', 'file.txt', [[[[['hello.txt', 'bye.txt'], 'b.txt']]], 'last.txt']] # Output 3 a.txt file.txt hello.txt bye.txt b.txt last.txt # Input 4 C = [[[[[[[[[[[[[[[[[[['my_file.txt']]]]]]]]]]]]]]]]]]] # Output 4 my_file.txt Task 5a (15 pts.) Design and program the game, Tic-Tac-Toe! Tic-Tac-Toe is a two-player game played on a 3x3 grid in which the players alternate turns and select a grid square in which to place one of their pieces. One player uses the letter X for pieces, and the other uses the letter O. A player wins the game by placing three of his pieces in a row, either vertically, diagonally or horizontally. For example, X wins the following game: X X X O O X X O O You are free to make any design decisions you see fit. For example, which data structure you choose to represent the playing board. This is what you must complete (anything additional is up to you): Present the user a menu that contains the following choices: o 1: Play human vs. human o 2: Play human vs. computer o 3: Quit Receive the user's choice and verify that it is a valid choice. If it is invalid (i.e. anything other than 1, 2, or 3), keep displaying the menu and asking for another choice until the user enters a valid choice. Implement a human vs. human mode that prompts each player in turn for a move until the game is won or ends in a tie. How you want the human to enter a move at the prompt is a design decision you will make. Implement a human vs. computer play mode that lets the human play first (by prompting him for a move) and alternates between players until the game is won or ends in a tie. How you want the human to enter a move at the prompt is a design decision you will make. You should display the game board before each turn. o The computer player does not have to be intelligent. It can randomly choose an open square to play a piece. The program must detect a win, print the winning player, and then end the game. It should be stressed that you will receive partial credit if you design and implement some useful functions for the game, even if you can't finish everything. Task 5b Describe the design of your program. What functions are you creating, and what are their arguments and return values? What does each function do, in English? What functions you deem necessary is entirely up to you. Just be sure to discuss each one you create. The goal in this document is to clearly describe WHAT useful thing each function computes. Task 5c Also, please state explicitly which function is the top-level function (i.e., the function that a user needs to call to play a game). Task 6 (15 pts.) Write a function check_day(text), that takes one argument called text and satisfies the following specs: if text is not a string, it returns False. If text is one of the seven days of the week, it returns True. Otherwise it returns False. It should not matter whether letters in text are uppercase or lowercase. For example, the function should return True if text is "sunday", or "MONday", or "tueSDay". If your solution is correct, the output should match EXACTLY what you see below. Result for ['Sunday'] : False Result for Sunday : True Result for mondaY : True Result for tUeSdAy : True Result for WEDneSDay : True Result for thursday : True Result for FRIDAY : True Result for sATurDAy : True Result for June : False exiting Task 7 (15 pts.) Write a function named random_element(s) that: Takes one argument, a string s. Returns a random letter from string s. For example, if I pass it the string "hello!", it may return "h", or "e", or any other of the letters in "hello", but it should not return "m" or "n", since those letters are not in "hello!". Your function will return random things each time you pass it the same string. To have Python choose a random integer from n to m, including both n and m, you can use the following code: # Place this line at the beginning of your file. from random import randint # This line generates a random integer from values n, n+1, ..., m, and stores it in random_index. random_index = randint(n, m) Task 8 (10 pts.) Write a function named list_powers(n1, n2) that satisfies the following specs: It takes two arguments, n1, n2. If any argument is not a integer, the function should return None. If n2 a negative integer (less than 0), the function should return None. If arguments n1, n2 are integers greater than or equal to 0, the function returns the list [n1**0, n1**1, n**2, ..., n1**n2]. For example: list_powers(15, -1) should return None. list_powers('hello', 1) should return None. list_powers(2, 0) should return [1]. list_powers(3, 1) should return [1, 3]. list_powers(3, 4) should return [1, 3, 9, 27, 81].