Skip to content

Commit f16b09a

Browse files
refactor
1 parent 9bcb2c2 commit f16b09a

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Definition for a binary tree node.
2+
import collections
3+
import unittest
4+
5+
# Non-threadsafe imports
6+
from collections import deque
7+
import heapq
8+
9+
# Threadsafe imports
10+
from typing import List
11+
12+
try:
13+
# Python2
14+
import queue
15+
except ImportError:
16+
# Python3
17+
import Queue as queue
18+
19+
class TaskScheduler(unittest.TestCase):
20+
21+
def leastInterval(self, tasks: List[str], n: int) -> int:
22+
if n == 0:
23+
return len(tasks)
24+
25+
taskCounter = collections.Counter(tasks)
26+
pq = []
27+
for task, count in taskCounter.items():
28+
heapq.heappush(pq, (-count, task))
29+
30+
result = 0
31+
32+
while pq:
33+
currSlot, tempList = 0, []
34+
while currSlot != n + 1 and pq:
35+
pqHead = heapq.heappop(pq)
36+
if pqHead[0] + 1 < 0:
37+
tempList.append((pqHead[0] + 1, pqHead[1]))
38+
currSlot += 1
39+
40+
if not tempList:
41+
result += currSlot
42+
else:
43+
result += n + 1
44+
45+
for entry in tempList:
46+
heapq.heappush(pq, entry)
47+
48+
return result
49+
50+
def test_normalCase(self):
51+
tasks = ["A", "A", "A", "B", "B", "B"]
52+
n = 2
53+
print(self.leastInterval(tasks, 2))
54+
55+
def test_wrongCase(self):
56+
tasks = ["A", "A", "A", "A", "A", "A", "B", "C", "D", "E", "F", "G"]
57+
print(self.leastInterval(tasks, 2))
58+
59+
if __name__ == '__main__':
60+
unittest.main()

Python/TestDataStructureDict.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
class TestDataStructureDict(unittest.TestCase):
88

9+
910
@unittest.skip
1011
def test_CreateDictionary(self):
1112
# default dictionary for string => string mapping
@@ -75,7 +76,12 @@ def test_DeleteElement(self):
7576
print(dictionary)
7677

7778
def test_NestedDefaultDict(self):
78-
myDict = defaultdict(lambda: defaultdict(dict))
79+
# nestedDict = defaultdict(lambda: defaultdict(dict))
80+
81+
nestedDict = defaultdict(defaultdict)
82+
nestedDict[0][1] = 2
83+
print(nestedDict[0][1])
84+
7985

8086
if __name__ == '__main__':
8187
unittest.main()

Python/TestDataStructurePriorityQueue.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def test_PriorityQueue_NonThreadSafe(self):
2727
print(heapq.heappop(li))
2828
print(heapq.heappop(li))
2929

30+
@unittest.skip
3031
def test_PeekHeapTop(self):
3132
pq = []
3233
heapq.heappush(pq, 3)
@@ -40,7 +41,9 @@ def test_PriorityQueue_Tuple(self):
4041
heapq.heappush(pq, (3, 25))
4142
heapq.heappush(pq, (20, 3))
4243
heapq.heappush(pq, (5, 5))
43-
44+
print(heapq.heappop(pq))
45+
print(heapq.heappop(pq))
46+
print(heapq.heappop(pq))
4447

4548
if __name__ == '__main__':
46-
unittest.main(
49+
unittest.main()

Python/TestDataStructureString.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ def test_ConcatenateString(self):
2222
result = " ".join(str1)
2323
print(result)
2424

25-
@unittest.skip
2625
def test_SplitString(self):
2726
s = " this is my string"
2827
result1 = s.split()
2928
result2 = s.split(' ')
3029

30+
result3 = "hello".split(' ')
31+
print(result3)
32+
3133
@unittest.skip
3234
def test_Convert(self):
3335
# convert int to str

0 commit comments

Comments
 (0)