During session 7
Exercises
1. Using list operations
There are two lists a and b:
a = [2, 3, 1, 5] b = [6, 4]
Using a, b and the operations applicable to lists, create a new list c which contains the following elements: [1, 2, 3, 4, 5, 6].
2. What is the name of a month?
We had an exercise where you wrote a function called month_as_name that took the number of a month as an argument and returned the name of the month (Session 3). You probably used if-statements in the function. This time rewrite the function so that if-statements are not used.
In the program, test your function with different inputs.
3. Date
Write a function date that takes a date in the string format (for example "10.09.1991") as an argument and returns the date in the form monthname date, year (for example September 10, 1991). Then add a piece of code which asks the user for a date and the program outputs the corresponding date as a string in the form monthname date, year.
Hint: you can and should reuse the function you defined in exercise 2.
4. All words
Write a program which prompts the user for a file name, finds all different words in this file, sorts and prints unique words in the alphabetical order.
Hint
Open the file and read it line by line. For each line, split the line into a list of words using the split function.
For each word, check if the word is already in the list. If the word is not in the list, add it to the list.
When reading the file is completed, sort and print unique words in the alphabetical order.
If the content of the file romeo.txt is as follows:
But soft what light through yonder window breaks It is the east and Juliet is the sun Arise fair sun and kill the envious moon Who is already sick and pale with greif
then the program output is the following:
Enter file name: romeo.txt ['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']
5. Processing records
Create a file (in Excel or any other similar program) that contains few lines of data, for example like this:
Jon | Snow | 36807043445 |
Mary | Lamb | 45302142344 |
Susan | Smith | 48892113324 |
... | ... | ... |
Save (or export) the table into the CSV format (http://en.wikipedia.org/wiki/Comma-separated_values). Open the file in any text editor (for example, in Notepad) to see how the data are saved.
If you don't have Excel, use this file instead.
Write a program that reads the data from the file line by line and outputs each line in the following way:
First name: Jon Last name: Snow ID: 36807043445 --------------------------- First name: Mary Last name: Lamb ID: 45302142344 --------------------------- …
Bonus. Add a filter to your program: the program asks the user if he wants to see the information about men or women; then the program outputs the corresponding data (hint: if the first digit of the ID is even, it is a woman, otherwise it is a man).