Purpose
The purpose of this request is to assess your understanding of
- Dictionary
- Tuple
- Set
- Random Module
Next week, we will discuss functions and the role of functions in programming (section 7.1) and how encapsulation is implemented (namespaces, local, global variables, section 7.2) and exceptions (section 7.3) and recursion, Chapter 10. You notice what we are skipping Chapters 8 and 9, some of that material you will see later in Java.
Submission
- Include your full name as a comment on the first line of your Python program.
- As we did in class, code all the problems in one Python file (.py). Each problems will consist of 1 or 2 modules (def). Identify each problem with comment like #Problem 1, Problem 2, etc.
- Submit (upload) to the dropbox one file labeled as YourName_HW6.py
Problam
Indexing problem
At the end of books, there usually is an index that lists the pages where a certain word appears. In this problem, you will create an index for a text but, instead of the page number, you will use the line numbers. You are to implement the function index (fname, letter) that takes as input the name of the text file and the first letter of the words for which you are to create the line number index. For each word that begins with ‘letter’, you are to print the corresponding line numbers. You should
- open and read the file only once; use readline()
- Remove all punctuation from the text
- For each word that begins with the specified letter, find all the line numbers in which the letter appears.
- Print the total number of lines in the text and the number of words that begin with the specified letter.
Use a dictionary to store results {word: list of line number(s)}. For each word, a line number should be listed only once, for example, ‘the’ appears 3 time on line 250; 250 should not be listed 3 times, only once. In the output, the line numbers should be separated by commas, but they are not printed as lists (no brackets). The words in the output should be in alphabetical order, i.e. a - z.
Random problem
Craps is a dice-based game played in many casinos. The game starts with the player throwing a pair of standard, six-sided dice. If the player rolls a total of 7 or 11, the player wins. If the player rolls a total of 2, 3 or 12, the player loses. For all other roll values, the player will repeatedly roll the pair of dice until either she rolls the initial value again (in which case she wins) or 7 (in which case she loses. Write a function craps() that takes no arguments, simulates one game of craps, and returns ‘I win’ if the player won and ‘I lost’ if the player lost. Consider implementing a second function if the player does not win or lose on the initial roll.
Dictionary problem
Implement a function STUDENT() which allows the user to enter the 7-digit student ID. The program will keep prompting the user for a last name and first name. If the student does not have a student ID on record, the program will then ask for the student ID, and store that information. If the student already has a student ID, the program will display it, and ask for confirmation whether a new studentID should be assigned (and, if so allows the new studentID to be entered). When the user hits the return key, the program prints a report listing all students with their studentID. Your report does not have to be in alphabetical order, however, if you do want to make it alphabetical, you can use sorted on the keys() of your dictionary.