05_nacrtovanje_poti

This commit is contained in:
Gašper Dobrovoljc 2023-11-16 16:18:51 +01:00
parent 39771d36ce
commit 1cfcb7328b
No known key found for this signature in database
GPG Key ID: 0E7E037018CFA5A5
9 changed files with 310 additions and 44 deletions

View File

View File

@ -1,9 +1,5 @@
def dolzina_ovir(vrstica):
count = 0
for char in vrstica:
if char == "#":
count += 1
return count
return vrstica.count("#")
def stevilo_ovir(vrstica):
count = 0

View File

@ -1,5 +1,7 @@
from .naloga import *
import unittest
from naloga import *
class Test(unittest.TestCase):
def test_dolzina_ovir(self):
@ -52,7 +54,10 @@ class Test(unittest.TestCase):
"...###",
"###.##",
]
self.assertEqual([(3, 4, 2), (2, 3, 3), (5, 5, 3), (4, 6, 4), (1, 3, 5), (5, 6, 5)], pretvori_zemljevid(zemljevid))
self.assertEqual(
[(3, 4, 2), (2, 3, 3), (5, 5, 3), (4, 6, 4), (1, 3, 5), (5, 6, 5)],
pretvori_zemljevid(zemljevid),
)
zemljevid = [
"..............##...",
@ -60,13 +65,25 @@ class Test(unittest.TestCase):
"...###...###...#...",
"...........#.....##",
"...................",
"###.....#####...###"
"###.....#####...###",
]
self.assertEqual([(15, 16, 1),
(3, 5, 2), (11, 13, 2), (18, 19, 2),
(4, 6, 3), (10, 12, 3), (16, 16, 3),
(12, 12, 4), (18, 19, 4),
(1, 3, 6), (9, 13, 6), (17, 19, 6)], pretvori_zemljevid(zemljevid))
self.assertEqual(
[
(15, 16, 1),
(3, 5, 2),
(11, 13, 2),
(18, 19, 2),
(4, 6, 3),
(10, 12, 3),
(16, 16, 3),
(12, 12, 4),
(18, 19, 4),
(1, 3, 6),
(9, 13, 6),
(17, 19, 6),
],
pretvori_zemljevid(zemljevid),
)
def test_izboljsave(self):
prej = [
@ -75,7 +92,7 @@ class Test(unittest.TestCase):
"...###...###...#...",
"...........#.....##",
"...................",
"###.....#####...###"
"###.....#####...###",
]
potem = [
@ -84,10 +101,12 @@ class Test(unittest.TestCase):
"#..###...###...#...",
"...###.....#.....##",
"................###",
"###.....#####...###"
"###.....#####...###",
]
self.assertEqual([(4, 5, 1), (1, 1, 3), (4, 6, 4), (17, 19, 5)], izboljsave(prej, potem))
self.assertEqual(
[(4, 5, 1), (1, 1, 3), (4, 6, 4), (17, 19, 5)], izboljsave(prej, potem)
)
self.assertEqual([], izboljsave(prej, prej))
@ -98,7 +117,7 @@ class Test(unittest.TestCase):
"...###...###...#...",
"...........#.....##",
"...................",
"###.....#####...###"
"###.....#####...###",
]
potem = [
@ -107,16 +126,30 @@ class Test(unittest.TestCase):
"#..###...###...#...",
"...###.....#.....##",
"................###",
"###.....##.##...###"
"###.....##.##...###",
]
dodane, odstranjene = huligani(prej, potem)
self.assertEqual([(4, 5, 1), (1, 1, 3), (4, 6, 4), (17, 19, 5), (9, 10, 6), (12, 13, 6)], dodane, "Napaka v seznamu dodanih")
self.assertEqual([(15, 16, 1), (3, 5, 2), (9, 13, 6)], odstranjene, "Napaka v seznamu odstranjenih")
self.assertEqual(
[(4, 5, 1), (1, 1, 3), (4, 6, 4), (17, 19, 5), (9, 10, 6), (12, 13, 6)],
dodane,
"Napaka v seznamu dodanih",
)
self.assertEqual(
[(15, 16, 1), (3, 5, 2), (9, 13, 6)],
odstranjene,
"Napaka v seznamu odstranjenih",
)
dodane, odstranjene = huligani(potem, prej) # Pazi, obrnjeno!
self.assertEqual([(15, 16, 1), (3, 5, 2), (9, 13, 6)], dodane, "Napaka v seznamu dodanih")
self.assertEqual([(4, 5, 1), (1, 1, 3), (4, 6, 4), (17, 19, 5), (9, 10, 6), (12, 13, 6)], odstranjene, "Napaka v seznamu odstranjenih")
self.assertEqual(
[(15, 16, 1), (3, 5, 2), (9, 13, 6)], dodane, "Napaka v seznamu dodanih"
)
self.assertEqual(
[(4, 5, 1), (1, 1, 3), (4, 6, 4), (17, 19, 5), (9, 10, 6), (12, 13, 6)],
odstranjene,
"Napaka v seznamu odstranjenih",
)
self.assertEqual(([], []), huligani(prej, prej))

View File

View File

@ -5,7 +5,7 @@ def koordinate(s):
return (int(start), int(start) + len(dolzina) - 1)
def vrstica(s):
(stev, *ovire) = s.strip().split()
(stev, *ovire) = s.split()
return list(map(lambda ovira: (*koordinate(ovira), int(stev[1:-1])), ovire))
def preberi(s):

View File

@ -1,5 +1,7 @@
from .naloga import *
import unittest
from naloga import *
class Obvezna(unittest.TestCase):
def test_koordinate(self):
@ -9,44 +11,78 @@ class Obvezna(unittest.TestCase):
self.assertEqual((123, 125), koordinate("123---"))
def test_vrstica(self):
self.assertEqual([(1, 3, 4), (5, 11, 4), (15, 15, 4)], vrstica(" (4) 1--- 5------- 15-"))
self.assertEqual(
[(1, 3, 4), (5, 11, 4), (15, 15, 4)], vrstica(" (4) 1--- 5------- 15-")
)
self.assertEqual([(989, 991, 1234)], vrstica("(1234) 989---"))
def test_preberi(self):
self.assertEqual([(5, 6, 4),
(90, 100, 13), (5, 8, 13), (19, 21, 13),
(9, 11, 5), (19, 20, 5), (30, 34, 5),
(9, 11, 4),
(22, 25, 13), (17, 19, 13)], preberi(
""" (4) 5--
self.assertEqual(
[
(5, 6, 4),
(90, 100, 13),
(5, 8, 13),
(19, 21, 13),
(9, 11, 5),
(19, 20, 5),
(30, 34, 5),
(9, 11, 4),
(22, 25, 13),
(17, 19, 13),
],
preberi(
""" (4) 5--
(13) 90----------- 5---- 19---
(5) 9--- 19-- 30-----
(4) 9---
(13) 22---- 17---
"""))
"""
),
)
def test_intervali(self):
self.assertEqual(["6-----", "12-", "20---", "98-----"], intervali([(6, 10), (12, 12), (20, 22), (98, 102)]))
self.assertEqual(
["6-----", "12-", "20---", "98-----"],
intervali([(6, 10), (12, 12), (20, 22), (98, 102)]),
)
def test_zapisi_vrstico(self):
self.assertEqual("(5) 6----- 12-", zapisi_vrstico(5, [(6, 10), (12, 12)]).rstrip("\n"))
self.assertEqual("(8) 6----- 12- 20--- 98-----", zapisi_vrstico(8, [(6, 10), (12, 12), (20, 22), (98, 102)]).rstrip("\n"))
self.assertEqual("(8) 6----- 12- 20--- 98-----", zapisi_vrstico(8, [(6, 10), (12, 12), (20, 22), (98, 102)]).rstrip("\n"))
self.assertEqual(
"(5) 6----- 12-", zapisi_vrstico(5, [(6, 10), (12, 12)]).rstrip("\n")
)
self.assertEqual(
"(8) 6----- 12- 20--- 98-----",
zapisi_vrstico(8, [(6, 10), (12, 12), (20, 22), (98, 102)]).rstrip("\n"),
)
self.assertEqual(
"(8) 6----- 12- 20--- 98-----",
zapisi_vrstico(8, [(6, 10), (12, 12), (20, 22), (98, 102)]).rstrip("\n"),
)
class Dodatna(unittest.TestCase):
def test_zapisi(self):
ovire = [(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)]
ovire = [
(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),
]
kopija_ovir = ovire.copy()
self.assertEqual("""(4) 5-- 9---
self.assertEqual(
"""(4) 5-- 9---
(5) 9--- 19-- 30-----
(13) 5---- 9--- 17--- 22---- 90-----------""", zapisi(ovire).rstrip("\n"))
(13) 5---- 9--- 17--- 22---- 90-----------""",
zapisi(ovire).rstrip("\n"),
)
self.assertEqual(ovire, kopija_ovir, "Pusti seznam `ovire` pri miru")
if __name__ == "__main__":
unittest.main()
unittest.main()

View File

View File

@ -0,0 +1,41 @@
def dvosmerni_zemljevid(zemljevid):
nov_zemljevid = {}
for (a, b), vescine in zemljevid.items():
nov_zemljevid[(a, b)] = nov_zemljevid[(b, a)] = set(vescine.split())
return nov_zemljevid
def mozna_pot(pot, zemljevid):
z = dvosmerni_zemljevid(zemljevid)
for k in zip(pot, pot[1:]):
if not z.__contains__(k):
return False
return True
def potrebne_vescine(pot, zemljevid):
z = dvosmerni_zemljevid(zemljevid)
vescine = set()
for k in zip(pot, pot[1:]):
vescine = vescine.union(set(z[k]))
return vescine
def nepotrebne_vescine(pot, zemljevid, vescine):
return vescine.difference(potrebne_vescine(pot, zemljevid))
def tocke_vescine(zemljevid, vescina):
z = dvosmerni_zemljevid(zemljevid)
tocke = set()
for k, v in z.items():
if v.__contains__(vescina):
tocke.add(k[0])
return "".join(sorted(tocke))
def koncna_tocka(pot, zemljevid, vescine):
z = dvosmerni_zemljevid(zemljevid)
for k in zip(pot, pot[1:]):
if not z[k].issubset(vescine):
return k[0], z[k].difference(vescine)

View File

@ -0,0 +1,160 @@
from .naloga import *
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, R, S, T, U, V = "ABCDEFGHIJKLMNOPRSTUV"
zemljevid = {
(A, B): "gravel trava",
(A, V): "pešci lonci",
(B, C): "bolt lonci",
(B, V): "",
(C, R): "stopnice pešci lonci",
(D, F): "stopnice pešci",
(D, R): "pešci",
(E, I): "trava lonci",
(F, G): "trava črepinje",
(G, H): "črepinje pešci",
(G, I): "avtocesta",
(H, J): "robnik bolt",
(I, M): "avtocesta",
(I, P): "gravel",
(I, R): "stopnice robnik",
(J, K): "",
(J, L): "gravel bolt",
(K, M): "stopnice bolt",
(L, M): "robnik pešci",
(M, N): "rodeo",
(N, P): "gravel",
(O, P): "gravel",
(P, S): "",
(R, U): "trava pešci",
(R, V): "pešci lonci",
(S, T): "robnik trava",
(T, U): "gravel trava",
(U, V): "robnik lonci trava",
}
mali_zemljevid = {(A, B): "robnik bolt", (A, C): "bolt rodeo pešci", (C, D): ""}
import unittest
import ast
class TestObvezna(unittest.TestCase):
def test_1_dvosmerni_zemljevid(self):
kopija = mali_zemljevid.copy()
self.assertEqual(
{
("A", "B"): {"robnik", "bolt"},
("B", "A"): {"robnik", "bolt"},
("A", "C"): {"bolt", "rodeo", "pešci"},
("C", "A"): {"bolt", "rodeo", "pešci"},
("C", "D"): set(),
("D", "C"): set(),
},
dvosmerni_zemljevid(mali_zemljevid),
)
self.assertEqual(
mali_zemljevid, kopija, "Ne spreminjaj zemljevida, temveč sestavi novega!"
)
def test_2_mozna_pot(self):
self.assertTrue(mozna_pot("ACD", mali_zemljevid))
self.assertTrue(mozna_pot("ABACD", mali_zemljevid))
self.assertTrue(mozna_pot("AB", mali_zemljevid))
self.assertFalse(mozna_pot("ABD", mali_zemljevid))
self.assertTrue(mozna_pot("ABCRVRIEIPNM", zemljevid))
self.assertTrue(mozna_pot("HJKMLJH", zemljevid))
self.assertFalse(mozna_pot("AC", zemljevid))
self.assertFalse(mozna_pot("ABCRVRIEPNM", zemljevid))
self.assertTrue(mozna_pot("A", zemljevid))
def test_3_potrebne_vescine(self):
self.assertEqual(
{"pešci", "bolt", "rodeo"}, potrebne_vescine("AC", mali_zemljevid)
)
self.assertEqual(
{"pešci", "bolt", "rodeo"}, potrebne_vescine("ACD", mali_zemljevid)
)
self.assertEqual(
{"pešci", "robnik", "bolt", "rodeo"},
potrebne_vescine("ABACD", mali_zemljevid),
)
self.assertEqual(
{"robnik", "stopnice", "gravel", "trava"},
potrebne_vescine("RIPSTUT", zemljevid),
)
self.assertEqual(
{"pešci", "trava", "lonci", "bolt", "stopnice", "gravel"},
potrebne_vescine("ABCRVR", zemljevid),
)
self.assertEqual(
{
"pešci",
"trava",
"robnik",
"lonci",
"bolt",
"stopnice",
"rodeo",
"gravel",
},
potrebne_vescine("ABCRVRIEIPNM", zemljevid),
)
self.assertEqual(
{"pešci", "robnik", "bolt", "stopnice", "gravel"},
potrebne_vescine("HJKMLJH", zemljevid),
)
self.assertEqual(set(), potrebne_vescine("BVBVBVB", zemljevid))
def test_4_nepotrebne_vescine(self):
vescine = {"pešci", "robnik", "stopnice", "gravel", "bolt", "rodeo"}
kopija = vescine.copy()
self.assertEqual(
{"stopnice", "gravel"}, nepotrebne_vescine("ABACD", mali_zemljevid, vescine)
)
self.assertEqual(
vescine,
kopija,
"Se mi prav zdi, da je funkcija nepotrebne_vescine spremenila "
"vrednost svojega argumenta `vescine`? Fail, fail!",
)
vescine = {"stopnice", "gravel", "bolt", "rodeo"}
self.assertEqual(
{"stopnice", "bolt"}, nepotrebne_vescine("IPNMNPO", zemljevid, vescine)
)
vescine = {"gravel", "rodeo"}
self.assertEqual(set(), nepotrebne_vescine("IPNMNPO", zemljevid, vescine))
def test_5_tocke_vescine(self):
self.assertEqual("GIM", tocke_vescine(zemljevid, "avtocesta"))
self.assertEqual("HIJLMRSTUV", tocke_vescine(zemljevid, "robnik"))
self.assertEqual("MN", tocke_vescine(zemljevid, "rodeo"))
self.assertEqual("ABIJLNOPTU", tocke_vescine(zemljevid, "gravel"))
class TestDodatna(unittest.TestCase):
def test_1_koncna_tocka(self):
vescine = {"pešci", "robnik", "bolt", "stopnice", "gravel"}
self.assertEqual(
("H", {"črepinje"}), koncna_tocka("HJKMLJHGFD", zemljevid, vescine)
)
self.assertEqual(("M", {"rodeo"}), koncna_tocka("HJKMNPIG", zemljevid, vescine))
self.assertEqual(
("B", {"lonci", "bolt"}),
koncna_tocka("ABCRVB", zemljevid, {"gravel", "trava"}),
)
if "__main__" == __name__:
unittest.main()