-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcodechallenge_108.py
98 lines (81 loc) · 2.68 KB
/
codechallenge_108.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'''
Problem statement:
==================
Given k sorted singly linked lists, write a function to merge all the lists into one sorted singly linked list.
Constraints:
============
k == lists.length
lists[i] is sorted in ascending order.
Examples:
=========
Input: lists = [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
Input: lists = [[],[1],[2,3,4,5,6]]
Expected output: ['1'->'2'->'3'->'4'->'5'->'6']
'''
import unittest
import HtmlTestRunner
import os, time
from unittest.runner import TextTestResult
#
# using the + operator
#
def mergeSlists(lists):
if len(lists) == 0:
return None
elif len(lists) == 1:
return lists[0]
else:
for l in lists:
if l is None:
lists.remove(l)
ret = lists[0] + mergeSlists(lists[1:])
return sorted(ret, key = int)
def main():
L1 = ['5','1','3','2','4']
L2 = ['8','10','9','11','7','-11']
mergedList = sorted([*L1, *L2], key = int)
print(mergedList)
print(mergeSlists([[5,1,3,2,4], [8,10,9,11,7], [-1, -2, -3, -4, -5]]))
print(mergeSlists(lists = [[],[1],[2,3,4,5,6]]))
class TestMergeSortedLists(unittest.TestCase):
def setUp(self):
self.startTime = time.time()
def tearDown(self):
t = time.time() - self.startTime
print('%s: %.3f' % (self.id(), t))
def test_kSortedList(self):
time.sleep(1)
self.assertEqual(mergeSlists([[5,1,3,2,4], [8,10,9,11,7], [-1, -2, -3, -4, -5]]), [-5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11])
time.sleep(1)
self.assertEqual(mergeSlists(lists = [[],[1],[2,3,4,5,6]]), [1,2,3,4,5,6])
if __name__ == '__main__':
if os.environ.get('UNITTEST_ONLY') != 'True':
main()
else:
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='test_reports'))
'''
Output:
======
#
# python3 codechallenge_108.py main()
#
PS D:\devel\GIT\DailyCodingChallenge> pipenv run python .\codechallenge_108.py
Loading .env environment variables...
['-11', '1', '2', '3', '4', '5', '7', '8', '9', '10', '11']
[-5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11]
[1, 2, 3, 4, 5, 6]
#
# python3 codechallenge_108.py unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='test_reports'))
#
PS D:\devel\GIT\DailyCodingChallenge> pipenv run python .\codechallenge_108.py
Loading .env environment variables...
Running tests...
----------------------------------------------------------------------
test_kSortedList (__main__.TestMergeSortedLists) ... OK (0.000000)s
----------------------------------------------------------------------
Ran 1 test in 0:00:00
OK
Generating HTML reports...
test_reports\TestResults___main__.TestMergeSortedLists_2022-01-25_15-17-14.html
'''