AlgoBazaAlgoBaza
Python for kids ยท Module 2

Lesson 30

Contest tasks with conditions

After this lesson: You'll solve real contest tasks: list all the cases and test every road of the program.

Video

?

The video for this lesson is coming soon.

Explanation

You already know everything you need for real contest tasks with conditions - like the ones at OIJ or Logia! ๐Ÿ† Today you'll learn the method: how to read a task, list all the cases and miss nothing.

Bajtek's plan for a task ๐ŸŽฌ

Click Next - four steps that work for every task with conditions.

?Bajtek
๐Ÿชจ๐Ÿ“„โœ‚๏ธ
1

Step 1: read carefully

The task: Two players play rock, paper, scissors. Print 1 if the first one wins, 2 if the second one does, and REMIS for a draw. Bajtek underlines: what is read, what is printed and what the rules are.

Step 1 / 4

A table of cases

For tasks with many possibilities draw a little table - then no case gets away:

first \ secondrock ๐Ÿชจpaper ๐Ÿ“„scissors โœ‚๏ธ
rock ๐ŸชจREMIS21
paper ๐Ÿ“„1REMIS2
scissors โœ‚๏ธ21REMIS

Rock, paper, scissors ๐Ÿชจ๐Ÿ“„โœ‚๏ธ

Type the first player's move ๐Ÿชจ and the second player's move โœ‚๏ธ (kamien, papier or nozyce - rock, paper, scissors in Polish), Enter โŽ after each.

โœŠ1first = input()
2second = input()
if first == second
4print("A draw ๐Ÿค")
๐Ÿค
elif first == "kamien" and second == "nozyce" or first == "nozyce" and second == "papier" or first == "papier" and second == "kamien"
6print("Player 1 wins")
1๏ธโƒฃ
elsein every other case
8print("Player 2 wins")
2๏ธโƒฃ

Click the arrow - I'll walk the program's path, line by line.

Code
1first = input()
2second = input()
3if first == second:
4 print("A draw ๐Ÿค")
5elif first == "kamien" and second == "nozyce" or first == "nozyce" and second == "papier" or first == "papier" and second == "kamien":
6 print("Player 1 wins")
7else:
8 print("Player 2 wins")
Screen
โ–
1/2 ยท line 1 runs next

Tickets for a trip ๐ŸŽฌ

Click Next - a task where you have to pick the cheapest option.

?Bajtek
๐ŸŽŸ๏ธ10 zloty๐Ÿ“ฆ10 pcs = 80 zloty
1

Two kinds of tickets

13 people are going to the museum. A single ticket costs 10 zloty, and a pack of 10 tickets - 80 zloty. What's the cheapest way to buy them?

Step 1 / 3

The cheapest tickets on the podium

The three ideas are three bars - the crown goes on the lowest.

?
๐ŸŽŸ๏ธ
?
๐Ÿ“ฆ๐ŸŽŸ๏ธ
?
๐Ÿ“ฆ๐Ÿ“ฆ
๐Ÿ“ฆ singles๐Ÿ“ฆ mixed๐Ÿ“ฆ packs
?

Click the arrow - the numbers will stand on the podium as bars.

Code
1people = 13
2singles = people * 10
3mixed = people // 10 * 80 + people % 10 * 10
4packs = (people // 10 + 1) * 80
5cheapest = singles
6if mixed < cheapest:
7 cheapest = mixed
8if packs < cheapest:
9 cheapest = packs
10print(cheapest)
Screen
โ–
1/10 ยท line 1 runs next

Work out the tickets for your class ๐ŸŽŸ๏ธ

Type a number of people ๐Ÿง’ (e.g. 13, 19 or 27) and press Enter โŽ.

?
๐ŸŽŸ๏ธ
?
๐Ÿ“ฆ๐ŸŽŸ๏ธ
?
๐Ÿ“ฆ๐Ÿ“ฆ
๐Ÿ“ฆ singles๐Ÿ“ฆ mixed๐Ÿ“ฆ packs
?

Click the arrow - the numbers will stand on the podium as bars.

Code
1people_str = input()
2people = int(people_str)
3singles = people * 10
4mixed = people // 10 * 80 + people % 10 * 10
5packs = (people // 10 + 1) * 80
6cheapest = singles
7if mixed < cheapest:
8 cheapest = mixed
9if packs < cheapest:
10 cheapest = packs
11print(cheapest)
Screen
โ–
1/2 ยท line 1 runs next

The kind of triangle

A contest classic: three sides a, b, c can make an equilateral triangle (all sides equal), an isosceles one (two equal) or a scalene one (none equal). But first a guard: is it a triangle at all? (from lesson 25: any two sides together are longer than the third).

The order matters: an equilateral triangle also has two equal sides - so we check it before isosceles.

What kind of triangle is it?

Sides 5, 5 and 8: the guard lets them through, equilateral - no, two equal - yes!

๐Ÿ“1a = 5
2b = 5
3c = 8
if a + b <= c or a + c <= b or b + c <= a
5print("Not a triangle")
๐Ÿšซ
elif a == b and b == c
7print("Equilateral")
๐Ÿ”บ
elif a == b or b == c or a == c
9print("Isosceles")
๐Ÿ”ป
elsein every other case
11print("Scalene")
๐Ÿ“

Click the arrow - I'll walk the program's path, line by line.

Code
1a = 5
2b = 5
3c = 8
4if a + b <= c or a + c <= b or b + c <= a:
5 print("Not a triangle")
6elif a == b and b == c:
7 print("Equilateral")
8elif a == b or b == c or a == c:
9 print("Isosceles")
10else:
11 print("Scalene")
Screen
โ–
1/8 ยท line 1 runs next
?

How many situations are there in rock-paper-scissors for two players?

?

Which triangle should be checked first?

?

Which tests do you pick for a task with three roads (if, elif, else)?

Remember

  • The plan: read (what's read, what's printed) โžก๏ธ list the cases โžก๏ธ write the ifs โžก๏ธ test every road.
  • With many possibilities draw a table of cases.
  • First the guard (does the data make sense), then the most demanding cases, the else at the end.
  • When looking for the cheapest / best option - work out all of them and hold a tournament of ifs.

Time for real contest tasks! Remember Bajtek's plan: read, list the cases, write, test.

Tasks from the video

We solve them together in the video - pause it and try first on your own.

Practice tasks

Try to solve them without the video. Stuck? Every task has hints and an editorial.

Sign in to save your progress.