Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import unittest
- def compute_penalty(log: str, closing_time) -> int:
- penalty = 0
- log = log.split(" ")
- if len(log) == 0:
- return 0
- #
- for index, val in enumerate(log):
- if closing_time > index:
- if val == "N":
- penalty += 1
- else:
- if val == "Y":
- penalty += 1
- return penalty
- def find_best_closing_time(log: str) -> int:
- print("nira", log)
- logList = log.split(" ")
- logList = [x for x in ["Y","N"]]
- print("nira", logList)
- best_closing_time = 0
- max_penalty = len(logList) + 1
- for i in range(0, len(logList)+1):
- penalty = compute_penalty(log, i)
- # print("penalty", penalty, max_penalty, best_closing_time)
- if (penalty <= max_penalty):
- max_penalty = penalty
- best_closing_time = i
- print("nira", best_closing_time)
- return best_closing_time
- def isValidLog(log) -> bool:
- log = log.split(" ")
- beginCount = 0
- endCount = 0
- for s in log:
- if s == "BEGIN":
- beginCount += 1
- if s == "END":
- endCount += 1
- return beginCount == 1 and endCount == 0
- def normalizeLog(log: str) -> str:
- print("normal1", log)
- log = log.replace("BEGIN", "")
- print("normal2", log)
- return log
- def get_best_closing_times(log: str) -> list[int]:
- log = log.replace("\n", "")
- result = []
- begin = 0
- while (True):
- begin = log.find("BEGIN", begin)
- if (begin == -1):
- break
- end = log.find("END", begin)
- if (end == -1):
- break
- valid = isValidLog(log[begin:end])
- begin += 1
- if (not valid):
- continue
- else:
- normalize_log = normalizeLog(log[begin-1:end])
- result.append(find_best_closing_time(normalize_log))
- return result
- # class Test(unittest.TestCase):
- # def test1(self):
- # self.assertEqual(compute_penalty("Y Y N Y", 0), 3)
- # def test2(self):
- # self.assertEqual(compute_penalty("", 0), 0)
- # def test3(self):
- # self.assertEqual(compute_penalty("Y Y Y Y", 4), 0)
- # def test4(self):
- # self.assertEqual(compute_penalty("Y Y N Y", 4), 1)
- # def test5(self):
- # self.assertEqual(compute_penalty("N N N N", 4), 4)
- # def test6(self):
- # self.assertEqual(compute_penalty("N N N N", 100), 4)
- # class Test(unittest.TestCase):
- # def test1(self):
- # self.assertEqual(find_best_closing_time("Y Y N Y"), 4)
- # def test2(self):
- # self.assertEqual(find_best_closing_time("Y"), 1)
- # def test3(self):
- # self.assertEqual(find_best_closing_time("N N N N"), 0)
- # def test4(self):
- # self.assertEqual(find_best_closing_time("Y Y Y Y"), 4)
- # def test5(self):
- # 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)
- # def test6(self):
- # 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)
- # def test7(self):
- # 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)
- class Test(unittest.TestCase):
- def test1(self):
- self.assertEqual(get_best_closing_times("BEGIN Y Y END \nBEGIN N N END"), [2,1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement