09_kontrola - ocena 7-9

This commit is contained in:
Gašper Dobrovoljc 2023-12-26 20:37:48 +01:00
parent cc849fd852
commit 3b7295f5c6
No known key found for this signature in database
GPG Key ID: 0E7E037018CFA5A5
3 changed files with 222 additions and 24 deletions

View File

@ -2,6 +2,7 @@ from itertools import groupby
import operator
from os import path
from collections import Counter
import re
def preberi_zemljevid(ime_dat: str):
@ -10,41 +11,49 @@ def preberi_zemljevid(ime_dat: str):
return file.read().strip().split("\n")
def check(x: int, y: int, c: str, zemljevid: list[str]):
return x >= 0 and y >= 0 and (c == "" and zemljevid[y][x] != "." or zemljevid[y][x] == c)
def check(x: int, y: int, c: str, zemljevid: list[str], prepovedani: set = None):
if prepovedani is None:
prepovedani = set()
return (
(x, y) not in prepovedani
and 0 <= y < len(zemljevid)
and 0 <= x < len(zemljevid[0])
and (c == "" and zemljevid[y][x] != "." or zemljevid[y][x] == c)
)
def najblizji(x: int, y: int, c: str, zemljevid: list[str]):
def najblizji_distance(x: int, y: int, c: str, zemljevid: list[str], prepovedani: set):
d = 1
max_d = max(len(zemljevid), len(zemljevid[0]))
max_d = len(zemljevid) + len(zemljevid[0])
while d <= max_d:
x -= 1
for i in range(d):
if check(x, y, c, zemljevid):
return x, y
x += 1
y += 1
for i in range(d):
if check(x, y, c, zemljevid):
return x, y
x += 1
y -= 1
for i in range(d):
if check(x, y, c, zemljevid):
return x, y
x -= 1
y -= 1
for i in range(d):
if check(x, y, c, zemljevid):
return x, y
x -= 1
y += 1
ry = 0
ry_inc = 1
for rx in range(d * 2 + 1):
if check(x + rx, y - ry, c, zemljevid, prepovedani):
return x + rx, y - ry, d
if check(x + rx, y + ry, c, zemljevid, prepovedani):
return x + rx, y + ry, d
if ry == d:
ry_inc = -1
ry += ry_inc
d += 1
def najblizji(x: int, y: int, c: str, zemljevid: list[str], prepovedani: set = None):
if prepovedani is None:
prepovedani = set()
res = najblizji_distance(x, y, c, zemljevid, prepovedani)
if res is None:
return
return res[0], res[1]
def najpogostejsi(x, y, d, zemljevid):
chars = []
for y2, line in enumerate(zemljevid):
@ -75,3 +84,71 @@ def vsi_najpogostejsi(x, y, d, zemljevid):
_, c = max(sorted(a))
return set(k for (k, v) in c)
def angelca(x, y, znamenitosti, zemljevid):
prepovedani = set()
distance = 0
for znam in znamenitosti:
res = najblizji_distance(x, y, znam, zemljevid, prepovedani)
if res is None:
return distance
prepovedani.add((x, y))
x, y, d = res
distance += d
return distance
def johanca(x, y, pot, zemljevid):
znamenitosti = []
obiskane = set()
for match in re.finditer("([0-9]*)([<>v^])", pot):
dist, dir = match.groups()
dist = int(dist) if dist != "" else 1
match dir:
case "^":
y -= dist
case "v":
y += dist
case "<":
x -= dist
case ">":
x += dist
if (
(x, y) not in obiskane
and 0 <= y < len(zemljevid)
and 0 <= x < len(zemljevid[0])
and zemljevid[y][x] != "."
):
obiskane.add((x, y))
znamenitosti.append(zemljevid[y][x])
return "".join(znamenitosti)
def najboljsa_cetrt(a: int, zemljevid: list[str]):
x_len = len(zemljevid[0]) - a
y_len = len(zemljevid) - a
y_len = y_len if y_len > 0 else 1
max_count = 0
max_x = 0
max_y = 0
for x in range(x_len):
for y in range(y_len):
count = 0
for ry in range(a):
for rx in range(a):
if zemljevid[y + ry][x + rx] != ".":
count += 1
if count > max_count:
max_count = count
max_x = x
max_y = y
return max_x, max_y

View File

@ -0,0 +1,91 @@
import time
from os import path, system
def preberi_zemljevid(ime_dat: str):
directory = path.dirname(path.abspath(__file__))
file = open(path.join(directory, ime_dat))
return file.read().strip().split("\n")
z = [list(" " + c for c in line) for line in preberi_zemljevid("test.txt")]
def print_z():
system('clear')
for line in z:
print("".join(line))
def check(x: int, y: int, c: str, zemljevid: list[list[str]], prepovedani: set = None):
if prepovedani is None:
prepovedani = set()
return (
(x, y) not in prepovedani
and 0 <= y < len(zemljevid)
and 0 <= x < len(zemljevid[0])
and (c == "" and zemljevid[y][x] != "." or zemljevid[y][x] == c)
)
counter = 1
def najblizji_distance(x: int, y: int, c: str, zemljevid: list[list[str]], prepovedani: set):
global counter
z[y][x] = " ."
d = 1
max_d = len(zemljevid) + len(zemljevid[0])
while d <= max_d:
x -= 1
ry = 0
ry_inc = 1
for rx in range(d * 2 + 1):
if (0 <= y - ry < len(zemljevid)
and 0 <= x + rx < len(zemljevid[0])):
z[y - ry][x + rx] = (" " * (5 - len(str(d)))) + str(d)
print_z()
counter += 1
if (0 <= y + ry < len(zemljevid)
and 0 <= x + rx < len(zemljevid[0])):
z[y + ry][x + rx] = (" " * (5 - len(str(d)))) + str(d)
print_z()
counter += 1
if check(x + rx, y + ry, c, zemljevid, prepovedani):
return x + rx, y + ry, d
if check(x + rx, y - ry, c, zemljevid, prepovedani):
return x + rx, y - ry, d
if ry == d:
ry_inc = -1
ry += ry_inc
d += 1
def angelca(x, y, znamenitosti, zemljevid):
prepovedani = set()
distance = 0
for znam in znamenitosti:
res = najblizji_distance(x, y, znam, zemljevid, prepovedani)
if res is None:
return distance
x, y, d = res
prepovedani.add((x, y))
distance += d
return distance
res = najblizji_distance(37, 14, "c", z, set())
print(res)

30
09_kontrola/test.txt Normal file
View File

@ -0,0 +1,30 @@
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
..c......................................
.........................................
.........................................