Example programming exercise
Write a program that reads a file that contains values of euro cents in each line and finds the sum of the euro cents and the value of smallest red coin (red coins are 1, 2 and 5 coins). Cents should be saved in the file cents.txt, where each line represents a coin (make the file by yourself). Find the sum of the euro cents (valid euro cent coins are 1, 2, 5, 10, 20 and 50) and the sum of unknown cents (the remaining numbers). Find the value of smallest red coin (1, 2, 5) or report about their absence.
For example, if the content of the file cents.txt is as follows:
5 10 15 20 10
then the program output should be as follows:
Sum of euro cents is 45 Sum of unknown cents is 15 Smallest red coin is 5
But if the content of the file cents.txt is as follows:
10 15 4 20 25 50
then the program output should be as follows:
Sum of euro cents is 80 Sum of unknown cents is 44 No red coins
You should follow these steps when writing your program:
- Read data in from the file cents.txt and store the cents into a list. Program should not crash if the file does not exist (use try-except).
- Write a function eurocents that takes in the list with all cents as a parameter, and finds and returns the sum of euro cents (1, 2, 5, 10, 20 and 50).
- Use the function eurocents in the program to output the sum of euro cents and the sum of unknown cents.
- Write a function red_coin that takes in the list with all cents as a parameter, finds the smallest red coin and returns its value. If there are no red coins, then the function should return 0.
- Use the function red_coin in the program to output the smallest red coin (1, 2, 5) or report about their absence.