Institute of Computer Science
  1. Courses
  2. 2020/21 spring
  3. Computer Programming (LTAT.03.001)
ET
Log in

Computer Programming 2020/21 spring

  • Home

Homework for Week 7

Strings

Watch lecture videos:

Slides in English

Textbook in English

Quiz

Go to Moodle and solve the quiz on strings.

Examples

Strings are sequences of characters. They are useful in a large varietey of situations, where there is a need to process textual data.

Example 1. Personal identification code

Estonian personal identification code is a number that is unique for each citizen. The national ID-card, its associated certificates and digital signatures use personal identification codes. The code contains information about the gender and the date of birth of the person. Since the personal identification code is unique, it is possible to identify persons by their codes.

The code consists of 11 digits, generally written without whitespaces or other delimiters. It has the form GYYMMDDSSSC, where G shows the gender and birth century (odd numbers for male, even numbers for female; 1-2 for individuals born in the 19th century, 3-4 for the 20th century, and 5-6 for the 21st century). SSS is a serial number uniquely identifying people born on the same date, and C is the checksum digit.

The following program prompts the user for an identification code, finds the person's birthday date from the code and prints it out in the format dd.mm.yyyy:

code = input("Please enter a personal identification code: ")
#For example: 48007140350

date = code[5] + code[6]
month = code[3] + code[4]

if code[0] == "1" or code[0] == "2":
    century = "18"
elif code[0] == "3" or code[0] == "4":
    century = "19"
else:
    century = "20"

year = century + code[1] + code[2]

print("The birthday of the person with the code", code, "is", date+"."+month+"."+year)

Example 2. Room number from address

The following program prompts for an address. The program finds and prints the room number in the address, assuming that the address is entered in the form of street_name building_number-room_number, town_name; e.g. Narva mnt 18-2039, Tartu.

address = input("Enter address: ")

fpos = address.find("-")
lpos = address.find(",", fpos)

room = address[fpos+1:lpos]

print("Room number is", room)

Exercises

1. Name and grades

Write a program that asks the user for his/her first name and grades. The grades are entered as a continuous string of letters. The program outputs exactly four lines of text:

  • Name and all grades. Regardless of how the data are entered, in the output the name should begin with capital letter, followed by lowercase letters, and the grades should be printed in all uppercase.
  • Total number of grades
  • The last grade in the input
  • Total number of A's and B's, counted together

Example of program output:

 Enter first name: eLiIsE
 Enter grades: aaAbBcDaD
 Hello Eliise, your grades are AAABBCDAD
 You have 9 grades
 Last grade is D
 The number of A's and B's is 6

2. Password

The following flowchart shows an algorithm for a web page registration form. Write a program that implements the process described in the flowchart.

Reversing the password means writing it backwards: drowssap.

Make sure that the program corresponds exactly to the flowchart: the program must first ask the user for three inputs, as shown in the flowchart, even if in some cases the error in the password can be detected already after entering the password for the first time. After informing the user about the mistake, the program needs to prompt only for the password (twice).

Submit your solutions

Submit your solutions of exercises 1 and 2 in Moodle under the names home1.py and home2.py, respectively.

  • 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