Grade 6

Implement a class Steza with the following methods.

  • Constructor gets the width and height of the cycling path and a list of triplets (y, x0, x1) describing the obstacles. The width (and height) give the number of the last column (and row); as usual, the left-most column (and top-most row) have index 1.
  • zaprto(x, y) returns True if the field (x, y) is covered by an obstacle.
  • konec(x, y, smer) gets some coordinates and direction, which can be <, >, ^ or v. It must return the coordinates of the field that the cyclist will reach if (s)he starts at (x, y) and travels in the given direction. The cyclist stops before an obstacle or at the edge of the path. The function can also return the same coordinates if the cyclist cannot move at all in that direction.

Grade 7

Implement a class Kolesar.

  • Constructor gets the initial coordinates of the cyclist and the cycling path - an instances of class Steza.
  • pozicija() returns the current position, as tuple (x, y).
  • premik(smer) moves the cyclist in the given direction up to the point when (s)he encounters an obstacle or the end of the path. It may also mean that (s)he does not move at all. The function does not return anything. The function is expected to use Steza.konec. The test will check for that.
  • prevozeno() returns the total distance that the cyclist rode so far.

The following program (where ovire contains the list from the previous homework) must move the cyclist as shown in the picture. Numbers at junctions represent distances.

steza = Steza(17, 18, ovire)
kolesar = Kolesar(12, 1, steza)
kolesar.premik("v")
kolesar.premik(">")
kolesar.premik("v")
kolesar.premik("<")
kolesar.premik("v")
kolesar.premik("<")
kolesar.premik("v")
kolesar.premik(">")
kolesar.premik("v")
kolesar.premik(">")
kolesar.premik("v")
kolesar.premik(">")
kolesar.premik("v")

 

Grade 8

Implement a class AvtonomniKolesar, which is derived from Kolesar.

  • Construcotr gets the same arguments, plus an argument that described a plan. The plan is a string consisting of characters < and >.
  • Method premik no longet accepts any arguments (except for self) and works as follows.

    • When called for the first time, it moves the cyclist downwards. The cyclist goes as far as possible (which may also mean nowhere at all). (Just use the inherited premik with proper arguments.)
    • When called for the second time, it moves (as far as possible) according to the first character of the plan.
    • Third time, it goes down again.
    • Fourth time, it uses the second character of the plan.
    • ...

    In other words, it alternates between moving down and taking a direction from the plan. If the plan is too short, it repeats again from the beginning.

    The method must make appropriate use of the inherited method premik.

The following will take the cyclist along the path in the picture.

steza = Steza(17, 18, ovire)  # kjer so ovire seznam ovir kot v prejšnji nalogi
kolesar = AvtonomniKolesar(12, 1, steza, "><<>><>")
for _ in range(14):
    kolesar.premik()

Grade 9

Implement a class VzvratniKolesar, derived from Kolesar.

  • Amend the constructor as necessary, but without changing the arguments.
  • Amend the method premik as necessary, again without changing its arguments (as compared to Kolesar!).
  • Add a method nazaj() that undoes the last premik. The method must restore the coordinates as well as decrease the covered distance - as if the last premik never happenned.

    The method can be called repeatedly, reverting one premik after another, until reaching the initial state. When there is nothing to revert, the method does nothing.

    In other words, it behaves like a typical undo.

Grade 10

Implement a class PametniKolesar, derived from VzvratniKolesar.

The class must have a method nacrt that finds the shortest path (that is, the path with the shortest distance) from the cyclist current position to the last line. The cyclist can move left, right and down, again, as in previous tasks, so that is always rides all the way to the next obstacle or the boundary of the cycling path.

The function must return a pair: the total length of the path and the string with the plan (including v). If the path is not possible, the function must return (None, "").

steza = Steza(17, 18, ovire)
kolesar = PametniKolesar(12, 1, steza)
print(kolesar.nacrt())

prints

(23, 'v<v>v>v>v')

The path is shown on the right. Note that it could be shorter if the cyclist moved from (12, 6) to (11, 6) instead of to (10, 6) - but he can't do that because he stops only at obstacles.

Advise: to avoid an endless loop, never allow the cyclist to move left after he just moved right or the opposite. The simplest way to check for this is to add an argument (with default value of False) that will tell whether the last move was down. Another way is to have a complete path covered so far as an argument; default would of course be "".

Be aware of blocked directions: if the cyclist cannot move, for instance, right, do not make recursive calls after that "move" without any effect.

Последна промена: четврток, 29 декември 2022, 12:54