Institute of Computer Science
  1. Courses
  2. 2018/19 fall
  3. Computer Programming (Narva College) (SVNC.00.054)
ET
Log in

Computer Programming (Narva College) 2018/19 fall

  • Home
  • Grading
  • Links

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:

  1. 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).
  2. 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).
  3. Use the function eurocents in the program to output the sum of euro cents and the sum of unknown cents.
  4. 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.
  5. Use the function red_coin in the program to output the smallest red coin (1, 2, 5) or report about their absence.
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment