The city department for economy and motorised traffic heard that students hate those dot-and-hashes maps, so they prepared the data in (yet) another form. We will need to write all functions anew.
Barriers are now desribed in a multiline string:
4: 5-6
13: 90-100, 5-8, 9-11
5: 9-11, 19-20, 30-34
4: 9-11
13: 22-25, 17-19
This means that row 4 has a barrier spanning from 5 to 6. Row 13 has barriers between 90 and 100, 5 and 8, and 9 and 11. Row 5 ... And, yes, then they remembered that there's another one in row 4, columns 9 to 11. And two more in row 13.
Each line thus contains a row number, followed by a colon. On the right side of the colon, there are intervals, separated by commas; each interval is given as a pair of numbers, divided by dashes.
How to split / join multiline strings
If s
is a multiline string, split it into lines by calling s.splitlines()
.
If we have two strings, say first = "First line"
and second = "Second line"
, we form a two-line string by adding first + "\n" + second
. Characters \n
denote an end of the line.
Mandatory task
Write the following functions.
vrstica(s)
gets a single line and returns a list of triplets (tuples)(x0, x1, y)
, representing all barriers in that line.Call
vrstica("4: 1-3, 5-11, 15-33")
returns[(1, 3, 4), (5, 11, 4), (15, 33, 4)]
.preberi(s)
gets the entire, multiline stinrg and returns a list of barriers. Barriers should appear in the same order as in the string.Calling the function with the string at the top should return
[(5, 6, 4), (90, 100, 13), (5, 8, 13), (9, 11, 13), (9, 11, 5), (19, 20, 5), (30, 34, 5), (9, 11, 4), (22, 25, 13), (17, 19, 13)]
intervali(xs)
gets a list of pairs(x0, x1)
and returns a list of strings that describe those pairs.Call
intervali([(6, 10), (12, 16), (20, 22), (98, 102)])
returns["6-10", "12-16", "20-22", "98-102"]
.zapisi_vrstico(y, xs)
gets a row number and a list of pairs, and returns a description of a single row.Call
zapisi_vrstico(5, [(6, 10), (12, 16)]))
returns"5: 6-10, 12-16"
. Be careful: no extra (or missing) spaces or commas. The string must look exactly as shown here.
Extra task
Write a function zapisi(ovire)
, that gets a list of obstacles and returns a string that describes it - like the one above. However, unlike the chaos we got from the city, the string must be ordered: rows must be ordered by increasing numbers, each row must appear only once and obstacles themselves must be ordered by increasing numbers.
Call
zapisi([(5, 6, 4),
(90, 100, 13), (5, 8, 13), (9, 11, 13),
(9, 11, 5), (19, 20, 5), (30, 34, 5),
(9, 11, 4),
(22, 25, 13), (17, 19, 13)])
returns
4: 5-6, 9-11
5: 9-11, 19-20, 30-34
13: 5-8, 9-11, 17-19, 22-25, 90-100
Again: there must be no extra (or missing) spaces or commas.