# Untitled

Feb 11th, 2024
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import unittest
2.
3.
4. def compute_penalty(log: str, closing_time) -> int:
5.     penalty = 0
6.     log = log.split(" ")
7.     if len(log) == 0:
8.         return 0
9.
10.     #
11.     for index, val in enumerate(log):
12.         if closing_time > index:
13.             if val == "N":
14.                 penalty += 1
15.         else:
16.             if val == "Y":
17.                 penalty += 1
18.     return penalty
19.
20.
21. def find_best_closing_time(log: str) -> int:
22.     print("nira", log)
23.     logList = log.split(" ")
24.     logList = [x for x in ["Y","N"]]
25.     print("nira", logList)
26.     best_closing_time = 0
27.     max_penalty = len(logList) + 1
28.     for i in range(0, len(logList)+1):
29.         penalty = compute_penalty(log, i)
30.         # print("penalty", penalty, max_penalty, best_closing_time)
31.         if (penalty <= max_penalty):
32.             max_penalty = penalty
33.             best_closing_time = i
34.
35.     print("nira", best_closing_time)
36.     return best_closing_time
37.
38. def isValidLog(log) -> bool:
39.     log = log.split(" ")
40.     beginCount = 0
41.     endCount = 0
42.     for s in log:
43.         if s == "BEGIN":
44.             beginCount += 1
45.         if s == "END":
46.             endCount += 1
47.     return beginCount == 1 and endCount == 0
48.
49. def normalizeLog(log: str) -> str:
50.     print("normal1", log)
51.     log = log.replace("BEGIN", "")
52.     print("normal2", log)
53.     return log
54.
55.
56. def get_best_closing_times(log: str) -> list[int]:
57.
58.     log = log.replace("\n", "")
59.     result = []
60.     begin = 0
61.     while (True):
62.         begin = log.find("BEGIN", begin)
63.         if (begin == -1):
64.             break
65.         end = log.find("END", begin)
66.         if (end == -1):
67.             break
68.         valid = isValidLog(log[begin:end])
69.         begin += 1
70.         if (not valid):
71.             continue
72.         else:
73.             normalize_log = normalizeLog(log[begin-1:end])
74.             result.append(find_best_closing_time(normalize_log))
75.     return result
76.
77.
78.
79.
80. # class Test(unittest.TestCase):
81. #     def test1(self):
82. #         self.assertEqual(compute_penalty("Y Y N Y", 0), 3)
83. #     def test2(self):
84. #         self.assertEqual(compute_penalty("", 0), 0)
85. #     def test3(self):
86. #         self.assertEqual(compute_penalty("Y Y Y Y", 4), 0)
87. #     def test4(self):
88. #         self.assertEqual(compute_penalty("Y Y N Y", 4), 1)
89. #     def test5(self):
90. #         self.assertEqual(compute_penalty("N N N N", 4), 4)
91. #     def test6(self):
92. #         self.assertEqual(compute_penalty("N N N N", 100), 4)
93.
94. # class Test(unittest.TestCase):
95. #         def test1(self):
96. #             self.assertEqual(find_best_closing_time("Y Y N Y"),  4)
97. #         def test2(self):
98. #             self.assertEqual(find_best_closing_time("Y"),  1)
99. #         def test3(self):
100. #             self.assertEqual(find_best_closing_time("N N N N"),  0)
101. #         def test4(self):
102. #             self.assertEqual(find_best_closing_time("Y Y Y Y"),  4)
103. #         def test5(self):
104. #             self.assertEqual(find_best_closing_time("N Y Y Y Y N N N Y N N Y Y N N N N Y Y N N Y N N N" ),  5)
105. #         def test6(self):
106. #             self.assertEqual(find_best_closing_time("N N N N N Y Y Y N N N N Y Y Y N N N Y N Y Y N Y N" ), 0)
107. #         def test7(self):
108. #             self.assertEqual(find_best_closing_time("Y Y N N N Y Y N Y Y N N N Y Y N N Y Y Y N Y N Y Y" ), 25)
109.
110. class Test(unittest.TestCase):
111.         def test1(self):
112.             self.assertEqual(get_best_closing_times("BEGIN Y Y END \nBEGIN N N END"),  [2,1])