Josif_tepe

Untitled

Aug 16th, 2025
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.21 KB | None | 0 0
  1.  
  2.  
  3. import os
  4. import copy
  5. import json
  6. import random
  7. import warnings
  8. import unittest
  9. import numpy as np
  10. random.seed(42)
  11.  
  12. with open("vhod.txt", "w") as f:
  13.     f.write("".join(f"{random.uniform(0.3, 2):.1f}\n" for _ in range(200)))
  14. with open("izhod.txt", "w") as f:
  15.     f.write("""19.3
  16. 20.0
  17. 20.0
  18. 19.6
  19. 20.0
  20. 19.9
  21. 19.4
  22. 19.7
  23. 18.9
  24. 18.4
  25. 19.9
  26. 10.1
  27. """)
  28. with open("vhod2.txt", "w") as f:
  29.     f.write("".join(f"{random.uniform(0.3, 2):.1f}\n" for _ in range(5)))
  30. with open("izhod2.txt", "w") as f:
  31.     f.write("7.2\n")
  32. with open("vhod3.txt", "w") as f:
  33.     f.write("1.0\n" * 20)
  34. with open("izhod3.txt", "w") as f:
  35.     f.write("20.0\n")
  36.  
  37.  
  38. t20 = [[20] * 5 for _ in range(7 * 4)]
  39.  
  40. # Pretežak stolpec
  41. tst = copy.deepcopy(t20)
  42. tst[3][2] = 21
  43.  
  44. # Prelahka leva
  45. tl = copy.deepcopy(t20)
  46. for n in range(4):
  47.     tl[1 + 7 * n][:2] = [0, 0]
  48.     tl[2 + 7 * n][:2] = [0, 0]
  49.     tl[5 + 7 * n][:2] = [0, 0]
  50.     tl[6 + 7 * n][:2] = [0, 0]
  51.  
  52. # Prelahka desna
  53. td = copy.deepcopy(t20)
  54. for n in range(4):
  55.     td[1 + 7 * n][-2:] = [0, 0]
  56.     td[3 + 7 * n][-2:] = [0, 0]
  57.  
  58. # Prelahko spredaj
  59. ts = copy.deepcopy(t20)
  60. for n in range(4):
  61.     for v in range(3):
  62.         ts[7 * n  + v][1:4] = [0, 0, 0]
  63.  
  64. # Vse OK - ignororaj srednjo :)
  65. tok = copy.deepcopy(t20)
  66. for n in range(4):
  67.     for v in range(7):
  68.         tok[7 * n  + v][2] = 0
  69.  
  70. # Vse OK - razlike niso prevelike
  71. toks = copy.deepcopy(t20)
  72. for n in range(4):
  73.     for v in range(3):
  74.         toks[7 * n  + v][1] = 0
  75.  
  76. for i, tab in enumerate((t20, tst, tl, td, ts, tok, toks), start=1):
  77.     with open(f"varnost{i}.txt", "w") as f:\
  78.         f.write("".join(",".join(map(str, v)) + "\n" for v in tab))
  79.  
  80.  
  81. with open("narocila.json", "w") as f:
  82.     json.dump({"Ana": 1000, "Berta": 2000, "Cilka": 300, "Dani": 1200}, f)
  83.  
  84. os.makedirs("posiljke", exist_ok=True)
  85. for komu, teze in (("Ana", (100, 400, 300, 200)), ("Berta", (200, 1000)), ("Dani", (1000, 200, 80))):
  86.     for teza in teze:
  87.         with open(f"posiljke/N{random.randint(100000, 999999)}.json", "w") as f:
  88.             json.dump({"narocnik": komu, "teza": teza, "koda_prevoznika": random.randint(100, 999)}, f)
  89.  
  90. def pakiranje(vlez, izlez):
  91.     sum = 0.0
  92.     res = []
  93.     with open(vlez, "r") as f:
  94.         for line in f:
  95.             broj = float(line)
  96.  
  97.             if sum + broj > 20.0:
  98.                 res.append(f"{sum:.1f}")
  99.                 sum = 0.0
  100.             sum += broj
  101.     if sum > 0:
  102.         res.append(f"{sum:.1f}")
  103.     with open(izlez, "w") as f:
  104.         for i in res:
  105.             print(i, file=f)
  106.  
  107.     print(res)
  108.  
  109.  
  110. def varnost(ime_datoteka):
  111.     vlez = np.genfromtxt(ime_datoteka, delimiter=',')
  112.     vkupno_rows, cols = vlez.shape
  113.     num_levels = 4
  114.  
  115.     rows = vkupno_rows // num_levels
  116.  
  117.     kutii = vlez.reshape(num_levels, rows, cols)
  118.  
  119.     column_weight = kutii.sum(axis=0)
  120.     print(column_weight)
  121.     if np.any(column_weight > 80):
  122.         return False
  123.  
  124.     total_weight = kutii.sum()
  125.     print(total_weight)
  126.     half_cols = cols // 2
  127.  
  128.     leva_tezina = column_weight[:, :half_cols].sum()
  129.     desna_tezina = column_weight[:, -half_cols:].sum()
  130.     print(leva_tezina, desna_tezina)
  131.     razlika = abs(leva_tezina - desna_tezina)
  132.     if razlika > total_weight * 0.1:
  133.         return False
  134.  
  135.     half_rows = rows // 2
  136.  
  137.     napred_tezina = column_weight[:half_rows, :].sum()
  138.     nazad_tezina = column_weight[-half_rows:, :].sum()
  139.  
  140.     razlika = abs(napred_tezina - nazad_tezina)
  141.  
  142.     if razlika > (total_weight * 0.1):
  143.         return False
  144.  
  145.     return True
  146.  
  147. def pregled(orders, dir_name, file_to_print):
  148.     name_dict = {}
  149.     with open(orders, "r") as f:
  150.         name_dict = json.load(f)
  151.  
  152.     zemena_roba = {}
  153.     names = []
  154.     for ime, vrednost in name_dict.items():
  155.         names.append(ime)
  156.         zemena_roba[ime] = 0
  157.  
  158.     for file_name in os.listdir(dir_name):
  159.         if file_name.endswith(".json"):
  160.             path = os.path.join(dir_name, file_name)
  161.  
  162.  
  163.             with open(path, "r") as f:
  164.                 d = json.load(f)
  165.  
  166.             if "narocnik" in d:
  167.                 if d["narocnik"] in zemena_roba and "teza" in d:
  168.                     zemena_roba[d["narocnik"]] += d["teza"]
  169.  
  170.     res = {}
  171.     for ime, tezina in zemena_roba.items():
  172.  
  173.         if tezina < name_dict[ime]:
  174.             res[ime] = name_dict[ime] - tezina
  175.  
  176.     with open(file_to_print, "w") as f:
  177.         json.dump(res, f)
  178.  
  179.  
  180.  
  181. class Test(unittest.TestCase):
  182.     def setUp(self):
  183.         super().setUp()
  184.         warnings.simplefilter("ignore", ResourceWarning)
  185.  
  186.     def test_01_pakiranje(self):
  187.         pakiranje("vhod.txt", "izhod-moj.txt")
  188.         self.assertEqual(open("izhod-moj.txt").read(), open("izhod.txt").read())
  189.  
  190.         pakiranje("vhod2.txt", "izhod2-moj.txt")
  191.         self.assertEqual(open("izhod2-moj.txt").read(), open("izhod2.txt").read())
  192.  
  193.         pakiranje("vhod3.txt", "izhod3-moj.txt")
  194.         self.assertEqual(open("izhod3-moj.txt").read(), open("izhod3.txt").read())
  195.  
  196.     def test_02_varnost(self):
  197.         self.assertTrue(varnost("varnost1.txt"))
  198.         self.assertFalse(varnost("varnost2.txt")) # pretežak stolpec
  199.         self.assertFalse(varnost("varnost3.txt")) # prelahka leva stran
  200.         self.assertFalse(varnost("varnost4.txt")) # prelahka desna stran
  201.         self.assertFalse(varnost("varnost5.txt")) # prelahka sprednja stran
  202.         self.assertTrue(varnost("varnost6.txt")) # na sredini nič, ampak OK
  203.         self.assertTrue(varnost("varnost7.txt")) # srepdaj malo manjka, vendar razlika ni prevlika
  204.  
  205.     def test_03_pregled(self):
  206.         pregled("narocila.json", "posiljke", "porocilo-moj.json")
  207.         self.assertEqual({'Berta': 800, 'Cilka': 300}, json.load(open("porocilo-moj.json")))
  208.  
  209.     def test_04_porocilo(self):
  210.         narocila = json.load(open("narocila.json"))
  211.         dorocila = {'Ana': 1000, 'Berta': 800, 'Dani': 1280}
  212.         self.assertEqual("""Naročnik    Naročeno   Dostava   Razlika
  213. ----------------------------------------
  214. Ana             1000      1000         0
  215. Berta           2000       800     -1200
  216. Cilka            300         0      -300
  217. Dani            1200      1280        80
  218. ----------------------------------------""".strip(), porocilo(narocila, dorocila).strip())
  219.  
  220.  
  221. if __name__ == "__main__":
  222.     unittest.main()
  223.  
Advertisement
Add Comment
Please, Sign In to add comment