04_angelcin_zapis

This commit is contained in:
Gašper Dobrovoljc 2023-11-09 21:58:58 +01:00
parent fd4a954d41
commit 39771d36ce
No known key found for this signature in database
GPG Key ID: 0E7E037018CFA5A5
3 changed files with 76 additions and 3 deletions

View File

@ -8,9 +8,9 @@ def dolzina_ovir(vrstica):
def stevilo_ovir(vrstica):
count = 0
ovira = False
for char in vrstica:
if char == "#":
if not ovira:
ovira = True
elif ovira:
count += 1

View File

@ -0,0 +1,21 @@
import itertools
def koordinate(s):
(start, *dolzina) = s.split("-")
return (int(start), int(start) + len(dolzina) - 1)
def vrstica(s):
(stev, *ovire) = s.strip().split()
return list(map(lambda ovira: (*koordinate(ovira), int(stev[1:-1])), ovire))
def preberi(s):
return list(itertools.chain(*map(vrstica, s.splitlines())))
def intervali(s):
return list(map(lambda x: str(x[0]) + "-" * (x[1] - x[0] + 1), s))
def zapisi_vrstico(y, xs):
return " ".join(["(" + str(y) + ")", *intervali(xs)])
def zapisi(ovire):
return "\n".join(map(lambda a: zapisi_vrstico(a[0], map(lambda b: (b[0], b[1]), sorted(a[1]))), itertools.groupby(sorted(ovire, key=lambda x: x[2]), lambda x: x[2])))

View File

@ -0,0 +1,52 @@
import unittest
from naloga import *
class Obvezna(unittest.TestCase):
def test_koordinate(self):
self.assertEqual((3, 4), koordinate("3--"))
self.assertEqual((5, 10), koordinate("5------"))
self.assertEqual((123, 123), koordinate("123-"))
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([(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--
(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)]))
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"))
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)]
kopija_ovir = ovire.copy()
self.assertEqual("""(4) 5-- 9---
(5) 9--- 19-- 30-----
(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()