Angelca Format

Last year, the Department of Economy and Motor Traffic of the City Municipality of Ljubljana learned that the students from FRI had developed software for reading their dot and hash maps, so they quickly prepared a new data format. However, this new format is already outdated and can only be found in the description of the last year's homework. This year, Mrs. Angelca (the one with long nails) convinced (with relentless pestering from morning to evening) the other employees in the department that the format is ugly and that she would prefer to record obstacles like this:

(4) 5--
(13) 90----------- 5---- 19---
(5) 9--- 19-- 30-----
(4) 9---
(13) 22---- 17---

And if they don't like it, she will go to the mayor because he is the only smart one and always agrees with her. (He has better things to do than listen to her daily ideas. These days, he starts unconsciously nodding even when he recognizes the sound of her high heels approaching in the hallway.)

Her format goes like this.

  • The first number in each line indicates the line number on the bike path. It is enclosed in parentheses, because this is a totally logical thing to do.

    In the example above, it provides some obstacles in line 4, then some in line 13, obstacles in line 5, followed by more obstacles in line 4 and 13. (One line can appear for multiple times. This allows for easily adding subsequently placed obstacles at the end of the file.)

  • The numbers in parentheses are followed by descriptions of the obstacles in the specified line. An obstacle is described with the starting column coordinate, followed by as many minuses as the length of the obstacle.

    In the fourth line, there is only one obstacle: it starts in the fifth column and is two units long. In our notation, this would be (5, 6, 4). In the 13th line, there are three obstacles; the first one starts at 90 and is 11 units long, so it would be represented as (90, 100, 13). The second starts at 5 and is 4 units long, hence (5, 9, 8). The third one is (19, 21, 13).

To make the notation more flexible (Angelca is not a computer scientist, she has studied at the faculty of in fine arts, study programme Industrial and unique design), it can include any number of spaces at the beginning or end of a line and between the descriptions of individual obstacles, as clearly shown in the example above.

Disclaimer

While the tasks in this subject are based on real events, the author of the assignment doesn't know whether there is actually an Angelca matching the description in the City Municipality of Ljubljana (MOL), though this can't be ruled out. In this assignment, Angelca is a fictional character, and any resemblance to real individuals with similar or, probably, different names is purely coincidental and perhaps even somewhat intentional.

Mandatory part

Write the following functions:

  • koordinate(s) receives a description of an obstacle and returns a tuple with its coordinates.

    Calling koordinate("5---")`` returns(5, 7), as it represents an obstacle that starts in column 5 and ends in column 7 - its length is 3. Calling `koordinate("123-")` returns `(123, 123).

  • vrstica(s) takes a string representing one line and returns a list of triplets (x0, x1, y) representing the obstacles in that line.

    Calling vrstica(" (4) 1--- 5------- 15-") returns a list [(1, 3, 4), (5, 11, 4), (15, 15, 4)].

  • preberi(s) receives a complete, multi-line string with obstacles and returns a list of obstacles. The obstacles should be stored in the same order in which they appear.

    When called with the above string, it returns:

    [(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) receives a list of pairs (x0, x1) and returns a list of strings describing these intervals.

    Calling intervali([(6, 10), (12, 12), (20, 22), (98, 102)]) returns "6-----", "12-", "20---", "98-----".

  • zapisi_vrstico(y, xs) receives a line number and a list of pairs (x0, x1). It should return the description of one line.

    Calling zapisi_vrstico(8, [(6, 10), (12, 12), (20, 22), (98, 102)]) returns the string "(8) 6----- 12- 20--- 98-----". Note: Do not add unnecessary spaces. Angelca didn't say they were required.

Non-mandatory part

Write a function zapisi(ovire) that takes a list of obstacles and returns a string containing the description of obstacles in a new format. Unlike the chaos you get from MOL, the record must be organized: lines should appear only once and in the correct order, and obstacles should be sorted from left to right.

Calling

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 the string

(4) 5-- 9---
(5) 9--- 19-- 30-----
(13) 5---- 9--- 17--- 22---- 90-----------

Again, the string should be in exactly the same format, without extra or missing spaces.