-
Notifications
You must be signed in to change notification settings - Fork 17
/
reveal-cards-in-increasing-order.py
51 lines (35 loc) · 1.28 KB
/
reveal-cards-in-increasing-order.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import unittest
from collections import deque
class Solution:
def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
queue = deque()
for num in reversed(sorted(deck)):
if queue:
queue.append(queue.popleft())
queue.append(num)
return reversed(queue)
def deckRevealedIncreasing2(self, deck):
queue = deque(range(len(deck)))
out = [None] * len(deck)
for elem in sorted(deck):
pos = queue.popleft()
out[pos] = elem
if queue:
pos = queue.popleft()
queue.append(pos)
return out
class TestSolution(unittest.TestCase):
def setUp(self):
self.sol = Solution()
def test_empty(self):
self.assertEqual(self.sol.deckRevealedIncreasing([]), [])
def test_one(self):
self.assertEqual(self.sol.deckRevealedIncreasing([2]), [2])
def test_two1(self):
self.assertEqual(self.sol.deckRevealedIncreasing([1, 2]), [1, 2])
def test_two2(self):
self.assertEqual(self.sol.deckRevealedIncreasing([2, 1]), [1, 2])
def test_custom(self):
self.assertEqual(self.sol.deckRevealedIncreasing([17,13,11,2,3,5,7]), [2,13,3,11,5,17,7])
if __name__ == "__main__":
unittest.main()