During session 6
Exercises
1. Filtering names
The text file names.txt contains some names of the Game of Thrones characters. Write a program that prompts the user for the surname of a character and outputs all the lines which contain the entered surname. The output lines must be enumerated. Furthermore, the last output line of the program has to show the total number of the printed out lines.
An example of the program execution:
Enter family name: Stark 1. Sansa Stark 2. Arya Stark 3. Catelyn Stark 4. Bran Stark 5. Robb Stark 6. Talisa Stark 7. Ned Stark All together: 7
Hint! Use method find() to find the last names of the characters.
2. Count the word
Download the file lyrics.txt . Write a program which counts how many times the word cha is used in the song.
Hint! Use the method split() to split and count the words in each line.
3. Compare
Why is it recommended to use the method find in the first task and method split in the second task?
4. Odd or even
Create a text file which contains integers - each integer on a separate line. Write a program that reads the numbers one by one from the file and displays the numbers with some additional information, whether they are even or odd.
5. Summing
Modify the previous program so that the program calculates the sum of the odd numbers, the sum of the even numbers and the sum of all the numbers.
6. Modified budget
Modify the exercise budget as follows.
The host has created a text file with a list of the invited people. He has added some remarks near each name:
? Anna + Peeter + Ursula ? Eva ? Juhan + Maria + Epp + Anu
where the '+' means that the person is coming and the '?' means that the person has not responded yet. Create a similar text file yourself!
Next write a program which prompts the user for the file name and calculates:
- the minimum budget for the party - take into account the people who have approved their participation
- the maximum budget for the party - take into account all guests regardless of their response.
Importantly, the party budget consists of the expenses on food (10 euros per person) and the room rent (a fixed price of 55 euros regardless of the number of people).
Hint: use your previous solution (including the function). Integrate the following functionalities into the program:
- the program prompts the user for the file name;
- the program reads information about the guests from the file;
- the program outputs the total number of the guest;
- the program outputs the number of people who have approved their participation in the party;
- the program uses the function to calculate the maximum budget for the party, and outputs the result;
- the program uses the function to calculate the minimum budget for the party, and outputs the result;
An example of the program output (assume that in the file guests.txt there is the list of guests shown above):
Please enter the file name: guests.txt The number of invited people: 8 The number of people attending: 5 Maximum budget: 135 EUR Minimum budget: 105 EUR
7. Peace and war
To demonstrate the power of Python in the field of text mining, download the novel 'Peace and War' by Leo Tolstoy. Then write a program which counts the lines in the novel. Also the program has to count how many times the words "peace" and "war" are used in the novel. Finally, the program has to output the results of the counting.
An example of the program output:
>>> In the file, there are 26808 lines. The word 'war' is used 166 times and the word 'peace' is used 59 times.