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.
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.
A table of cases
For tasks with many possibilities draw a little table - then no case gets away:
| first \ second | rock ๐ชจ | paper ๐ | scissors โ๏ธ |
|---|---|---|---|
| rock ๐ชจ | REMIS | 2 | 1 |
| paper ๐ | 1 | REMIS | 2 |
| scissors โ๏ธ | 2 | 1 | REMIS |
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.
first = input()second = input()print("A draw ๐ค")print("Player 1 wins")print("Player 2 wins")Click the arrow - I'll walk the program's path, line by line.
1first = input()2second = input()3if first == second:4print("A draw ๐ค")5elif first == "kamien" and second == "nozyce" or first == "nozyce" and second == "papier" or first == "papier" and second == "kamien":6print("Player 1 wins")7else:8print("Player 2 wins")
โTickets for a trip ๐ฌ
Click Next - a task where you have to pick the cheapest option.
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?
The cheapest tickets on the podium
The three ideas are three bars - the crown goes on the lowest.
Click the arrow - the numbers will stand on the podium as bars.
1people = 132singles = people * 103mixed = people // 10 * 80 + people % 10 * 104packs = (people // 10 + 1) * 805cheapest = singles6if mixed < cheapest:7cheapest = mixed8if packs < cheapest:9cheapest = packs10print(cheapest)
โWork out the tickets for your class ๐๏ธ
Type a number of people ๐ง (e.g. 13, 19 or 27) and press Enter โ.
Click the arrow - the numbers will stand on the podium as bars.
1people_str = input()2people = int(people_str)3singles = people * 104mixed = people // 10 * 80 + people % 10 * 105packs = (people // 10 + 1) * 806cheapest = singles7if mixed < cheapest:8cheapest = mixed9if packs < cheapest:10cheapest = packs11print(cheapest)
โ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!
a = 5b = 5c = 8print("Not a triangle")print("Equilateral")print("Isosceles")print("Scalene")Click the arrow - I'll walk the program's path, line by line.
1a = 52b = 53c = 84if a + b <= c or a + c <= b or b + c <= a:5print("Not a triangle")6elif a == b and b == c:7print("Equilateral")8elif a == b or b == c or a == c:9print("Isosceles")10else:11print("Scalene")
โ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.