03_zemljevid_ovir

This commit is contained in:
Gašper Dobrovoljc 2023-10-27 12:49:52 +02:00
commit 3a09b99dfa
No known key found for this signature in database
GPG Key ID: 0E7E037018CFA5A5
7 changed files with 262 additions and 0 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"python.testing.unittestArgs": ["-v", "-s", ".", "-p", "*_test.py"],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
}

View File

@ -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)

37
02_sikaniranje/naloga.py Normal file
View File

@ -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)

Binary file not shown.

View File

@ -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

View File

@ -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()