Arvutiteaduse instituut
  1. Kursused
  2. 2022/23 sügis
  3. Programmeerimise alused (MTAT.03.236)
EN
Logi sisse

Programmeerimise alused 2022/23 sügis

  • 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 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 the list in with all the cents as a parameter, 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 the euro cents and the sum of unknown cents.
  4. Write a function red_coin that takes in the list with all the 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.
  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Tartu Ülikooli arvutiteaduse instituudi kursuste läbiviimist toetavad järgmised programmid:
euroopa sotsiaalfondi logo