Josif_tepe

Untitled

Aug 15th, 2025
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.38 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.  
  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. class Test(unittest.TestCase):
  110.     def setUp(self):
  111.         super().setUp()
  112.         warnings.simplefilter("ignore", ResourceWarning)
  113.  
  114.     def test_01_pakiranje(self):
  115.         pakiranje("vhod.txt", "izhod-moj.txt")
  116.         self.assertEqual(open("izhod-moj.txt").read(), open("izhod.txt").read())
  117.  
  118.         pakiranje("vhod2.txt", "izhod2-moj.txt")
  119.         self.assertEqual(open("izhod2-moj.txt").read(), open("izhod2.txt").read())
  120.  
  121.         pakiranje("vhod3.txt", "izhod3-moj.txt")
  122.         self.assertEqual(open("izhod3-moj.txt").read(), open("izhod3.txt").read())
  123.  
  124.     def test_02_varnost(self):
  125.         self.assertTrue(varnost("varnost1.txt"))
  126.         self.assertFalse(varnost("varnost2.txt")) # pretežak stolpec
  127.         self.assertFalse(varnost("varnost3.txt")) # prelahka leva stran
  128.         self.assertFalse(varnost("varnost4.txt")) # prelahka desna stran
  129.         self.assertFalse(varnost("varnost5.txt")) # prelahka sprednja stran
  130.         self.assertTrue(varnost("varnost6.txt")) # na sredini nič, ampak OK
  131.         self.assertTrue(varnost("varnost7.txt")) # srepdaj malo manjka, vendar razlika ni prevlika
  132.  
  133.     def test_03_pregled(self):
  134.         pregled("narocila.json", "posiljke", "porocilo-moj.json")
  135.         self.assertEqual({'Berta': 800, 'Cilka': 300}, json.load(open("porocilo-moj.json")))
  136.  
  137.     def test_04_porocilo(self):
  138.         narocila = json.load(open("narocila.json"))
  139.         dorocila = {'Ana': 1000, 'Berta': 800, 'Dani': 1280}
  140.         self.assertEqual("""Naročnik    Naročeno   Dostava   Razlika
  141. ----------------------------------------
  142. Ana             1000      1000         0
  143. Berta           2000       800     -1200
  144. Cilka            300         0      -300
  145. Dani            1200      1280        80
  146. ----------------------------------------""".strip(), porocilo(narocila, dorocila).strip())
  147.  
  148.  
  149. if __name__ == "__main__":
  150.     unittest.main()
  151.  
Advertisement
Add Comment
Please, Sign In to add comment