commit 3a09b99dfa7e38b3ae998a7bbac683040c52e182 Author: Gašper Dobrovoljc Date: Fri Oct 27 12:49:52 2023 +0200 03_zemljevid_ovir diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3dd8a3e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "python.testing.unittestArgs": ["-v", "-s", ".", "-p", "*_test.py"], + "python.testing.pytestEnabled": false, + "python.testing.unittestEnabled": true +} diff --git a/01_kolicina_goriva/naloga.py b/01_kolicina_goriva/naloga.py new file mode 100644 index 0000000..c6b5783 --- /dev/null +++ b/01_kolicina_goriva/naloga.py @@ -0,0 +1,13 @@ +weight = int(input("Vnesi težo tovora: ")) +fuel_weight = 0 +actual_fuel_weight = 0 + +while weight > 0: + weight = weight // 3 - 2 + fuel_weight += 1 + if weight <= 0: + continue + actual_fuel_weight += weight + +print("Teža goriva:", fuel_weight) +print("Dejanska teža goriva:", actual_fuel_weight) diff --git a/02_sikaniranje/naloga.py b/02_sikaniranje/naloga.py new file mode 100644 index 0000000..5e35acb --- /dev/null +++ b/02_sikaniranje/naloga.py @@ -0,0 +1,37 @@ +ovire = [(1, 3, 6), (2, 4, 3), (4, 6, 7), (3, 4, 9), (6, 9, 5), (9, 10, 2), (9, 10, 8)] +x = 6 + +min_y = None +for x1, x2, y in ovire: + if x >= x1 and x <= x2 and (min_y == None or min_y > y): + min_y = y + +print(min_y) + +# Dodatna naloga + +ovire = [(1, 3, 6), (2, 4, 3), (4, 6, 7), (3, 4, 9), (6, 9, 5), (9, 10, 2), (9, 10, 8)] + +sirina = 0 +for x1, x2, y in ovire: + if x2 > sirina: + sirina = x2 + +max_x = 0 +max_y = 0 + +for x in range(1, sirina+1): + min_y = None + for x1, x2, y in ovire: + if x >= x1 and x <= x2 and (min_y == None or min_y > y): + min_y = y + + if min_y == None: + print(x, "Zmaga!") + break + + if max_y < min_y: + max_y = min_y + max_x = x +else: + print(max_x, max_y) \ No newline at end of file diff --git a/03_zemljevid_ovir/__pycache__/naloga.cpython-311.pyc b/03_zemljevid_ovir/__pycache__/naloga.cpython-311.pyc new file mode 100644 index 0000000..6b2a1d5 Binary files /dev/null and b/03_zemljevid_ovir/__pycache__/naloga.cpython-311.pyc differ diff --git a/03_zemljevid_ovir/__pycache__/naloga_test.cpython-311-pytest-7.4.2.pyc b/03_zemljevid_ovir/__pycache__/naloga_test.cpython-311-pytest-7.4.2.pyc new file mode 100644 index 0000000..3553fbf Binary files /dev/null and b/03_zemljevid_ovir/__pycache__/naloga_test.cpython-311-pytest-7.4.2.pyc differ diff --git a/03_zemljevid_ovir/naloga.py b/03_zemljevid_ovir/naloga.py new file mode 100644 index 0000000..d5ff3c2 --- /dev/null +++ b/03_zemljevid_ovir/naloga.py @@ -0,0 +1,82 @@ +def dolzina_ovir(vrstica): + count = 0 + for char in vrstica: + if char == "#": + count += 1 + return count + +def stevilo_ovir(vrstica): + count = 0 + ovira = False + for char in vrstica: + if char == "#": + if not ovira: + ovira = True + elif ovira: + count += 1 + ovira = False + if ovira: + count += 1 + return count + +def najsirsa_ovira(vrstica): + sirina_max = 0 + sirina = 0 + for char in vrstica: + if char == "#": + sirina += 1 + else: + if sirina > sirina_max: + sirina_max = sirina + sirina = 0 + if sirina > sirina_max: + sirina_max = sirina + return sirina_max + +def pretvori_vrstico(vrstica): + ovire = [] + ovira = False + start = 0 + for i, char in enumerate(vrstica): + if char == "#": + if not ovira: + start = i + 1 + ovira = True + elif ovira: + ovire.append((start, i)) + ovira = False + if ovira: + ovire.append((start, i + 1)) + return ovire + +def pretvori_zemljevid(vrstice): + ovire = [] + for y, vrstica in enumerate(vrstice, start=1): + for ovira in pretvori_vrstico(vrstica): + ovire.append((*ovira, y)) + return ovire + +def izboljsave(prej, potem): + nove_ovire = [] + star_zemljevid = pretvori_zemljevid(prej) + for ovira in pretvori_zemljevid(potem): + if ovira not in star_zemljevid: + nove_ovire.append(ovira) + return nove_ovire + +def huligani(prej, potem): + dodane_ovire = [] + odstranjene_ovire = [] + + star_zemljevid = pretvori_zemljevid(prej) + nov_zemljevid = pretvori_zemljevid(potem) + + for ovira in star_zemljevid: + if ovira not in nov_zemljevid: + odstranjene_ovire.append(ovira) + + for ovira in nov_zemljevid: + if ovira not in star_zemljevid: + dodane_ovire.append(ovira) + + return dodane_ovire, odstranjene_ovire \ No newline at end of file diff --git a/03_zemljevid_ovir/naloga_test.py b/03_zemljevid_ovir/naloga_test.py new file mode 100644 index 0000000..91a8cce --- /dev/null +++ b/03_zemljevid_ovir/naloga_test.py @@ -0,0 +1,125 @@ +import unittest +from naloga import * + +class Test(unittest.TestCase): + def test_dolzina_ovir(self): + self.assertEqual(3, dolzina_ovir("...###...")) + self.assertEqual(1, dolzina_ovir("...#...")) + self.assertEqual(2, dolzina_ovir("...#..#.")) + self.assertEqual(7, dolzina_ovir("#...#####..#.")) + self.assertEqual(8, dolzina_ovir("...#####.##...#")) + self.assertEqual(9, dolzina_ovir("#...#####.##...#")) + self.assertEqual(6, dolzina_ovir("##...#.#...##")) + self.assertEqual(0, dolzina_ovir("...")) + self.assertEqual(0, dolzina_ovir(".")) + + def test_stevilo_ovir(self): + self.assertEqual(1, stevilo_ovir("...###...")) + self.assertEqual(1, stevilo_ovir("...#...")) + self.assertEqual(2, stevilo_ovir("...#..#.")) + self.assertEqual(3, stevilo_ovir("#...#####..#.")) + self.assertEqual(3, stevilo_ovir("...#####.##...#")) + self.assertEqual(4, stevilo_ovir("#...#####.##...#")) + self.assertEqual(4, stevilo_ovir("##...#.#...##")) + self.assertEqual(0, stevilo_ovir("...")) + self.assertEqual(0, stevilo_ovir(".")) + + def test_najsirsa_ovira(self): + self.assertEqual(3, najsirsa_ovira("...###...")) + self.assertEqual(1, najsirsa_ovira("...#...")) + self.assertEqual(1, najsirsa_ovira("...#..#.")) + self.assertEqual(5, najsirsa_ovira("#...#####..#.")) + self.assertEqual(5, najsirsa_ovira("...#####.##...#")) + self.assertEqual(5, najsirsa_ovira("#...#####.##...#")) + self.assertEqual(6, najsirsa_ovira("######...#####.##...#")) + self.assertEqual(6, najsirsa_ovira("...#####.##...######")) + + def test_pretvori_vrstico(self): + self.assertEqual([(3, 5)], pretvori_vrstico("..###.")) + self.assertEqual([(3, 5), (7, 7)], pretvori_vrstico("..###.#.")) + self.assertEqual([(1, 2), (5, 7), (9, 9)], pretvori_vrstico("##..###.#.")) + self.assertEqual([(1, 1), (4, 6), (8, 8)], pretvori_vrstico("#..###.#.")) + self.assertEqual([(1, 1), (4, 6), (8, 8)], pretvori_vrstico("#..###.#")) + self.assertEqual([], pretvori_vrstico("...")) + self.assertEqual([], pretvori_vrstico("..")) + self.assertEqual([], pretvori_vrstico(".")) + + def test_pretvori_zemljevid(self): + 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 = [ + "..............##...", + "..###.....###....##", + "...###...###...#...", + "...........#.....##", + "...................", + "###.....#####...###" + ] + 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 = [ + "..............##...", + "..###.....###....##", + "...###...###...#...", + "...........#.....##", + "...................", + "###.....#####...###" + ] + + potem = [ + "...##.........##...", + "..###.....###....##", + "#..###...###...#...", + "...###.....#.....##", + "................###", + "###.....#####...###" + ] + + self.assertEqual([(4, 5, 1), (1, 1, 3), (4, 6, 4), (17, 19, 5)], izboljsave(prej, potem)) + + self.assertEqual([], izboljsave(prej, prej)) + + def test_huligani(self): + prej = [ + "..............##...", + "..###.....###....##", + "...###...###...#...", + "...........#.....##", + "...................", + "###.....#####...###" + ] + + potem = [ + "...##..............", + "..........###....##", + "#..###...###...#...", + "...###.....#.....##", + "................###", + "###.....##.##...###" + ] + + 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") + + 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(([], []), huligani(prej, prej)) + + +if __name__ == "__main__": + unittest.main()