Skip to content

Commit dcfd6aa

Browse files
python: add some problems and unittest
1 parent 3e6d293 commit dcfd6aa

File tree

8 files changed

+108
-3
lines changed

8 files changed

+108
-3
lines changed

Python/common_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ def print_linked_list(head):
1414
s += '->'
1515
node = node.next
1616
print(s)
17+
18+
class Interval:
19+
def __init__(self, s=0, e=0):
20+
self.start = s
21+
self.end = e

Python/sln_1_100/solution_11_20.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Solution_11_20(object):
4+
def intToRoman(self, num):
5+
"""
6+
7+
:param num:
8+
:return:
9+
"""
10+
rets = ['I','V','X','L','C','D','M']
11+
File renamed without changes.

Python/sln_1_100/solution_51_60.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
# -*- coding: UTF-8 -*-
2+
from itertools import permutations
3+
from common_utils import Interval
24

35
class Solution_51_60:
6+
def solveNQueens(self, n):
7+
"""
8+
51
9+
:type n: int
10+
:rtype: List[List[str]]
11+
"""
12+
outputs = []
13+
for candidate in permutations(range(0, n)):
14+
left_triangle = {v - i for i, v in enumerate(candidate)}
15+
right_triangle = {v + i for i, v in enumerate(candidate)}
16+
if len(left_triangle) == len(right_triangle) == n:
17+
output = []
18+
for queen in candidate:
19+
output.append('.' * queen + 'Q' + '.' * (n - 1 - queen))
20+
outputs.append(output)
21+
return outputs
22+
423
def spiralOrder(self, matrix):
524
"""
625
problem 54 - Spiral Matrix
@@ -41,7 +60,26 @@ def spiralOrder(self, matrix):
4160
curr_direction = (curr_direction + 1) % 4
4261

4362
return res
44-
63+
def merge(self, intervals):
64+
"""
65+
56
66+
:type intervals: List[Interval]
67+
:rtype: List[Interval]
68+
"""
69+
intervals = sorted(intervals, key=lambda i: i.start)
70+
new_intervals = []
71+
last_end = -1
72+
last_start = -1
73+
for iterval in intervals:
74+
start, end = iterval.start, iterval.end
75+
if new_intervals and last_end >= start:
76+
new_start = start if start < last_start else last_start
77+
new_end = end if end > last_end else last_end
78+
new_intervals.pop(-1)
79+
start, end = new_start, new_end
80+
new_intervals.append(Interval(start, end))
81+
last_start, last_end = start, end
82+
return new_intervals
4583
def lengthOfLastWord(self, s):
4684
"""
4785
58

Python/sln_1_100/solution_71_80.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
class Solution_71_80(object):
3+
def simplifyPath(self, path):
4+
"""
5+
:type path: str
6+
:rtype: str
7+
"""
8+
if not path or path == '/':
9+
return path
10+
11+
r_offset = 0
12+
while len(path) - 1 >= r_offset and path[len(path) - 1 - r_offset] == '/':
13+
r_offset += 1
14+
15+
if r_offset:
16+
path = path[:-r_offset]
17+
18+
if not path:
19+
return '/'
20+
21+
path_segments = path[1:].split('/')
22+
final_path_segments = []
23+
24+
for segment in path_segments:
25+
if segment == '..':
26+
if final_path_segments:
27+
final_path_segments.pop()
28+
elif segment in {'.', ''}:
29+
continue
30+
else:
31+
final_path_segments.append(segment)
32+
33+
return '/' + '/'.join(final_path_segments)

Python/test_solution_41_50.py renamed to Python/sln_1_100/test_solution_41_50.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from unittest import TestCase
2-
from solution_41_50 import Solution_41_50
2+
from sln_1_100.solution_41_50 import Solution_41_50
33

44

55
# -*- coding: UTF-8 -*-

Python/sln_1_100/test_solution_51_60.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from unittest import TestCase
22
from sln_1_100.solution_51_60 import Solution_51_60
3-
3+
from common_utils import Interval
44

55
# -*- coding: UTF-8 -*-
66
class TestSolution_51_60(TestCase):
@@ -12,6 +12,9 @@ def test_spiralOrder(self):
1212
res = [1, 2, 3, 6, 9, 8, 7, 4, 5]
1313
self.assertEqual(self.sln.spiralOrder(m), res)
1414
self.assertEqual(self.sln.spiralOrder([[1]]), [1])
15+
def test_merge(self):
16+
raw_span1 = [[1,3],[2,6],[8,10],[15,18]]
17+
span1 = [Interval(start,end) for start,end in raw_span1]
1518

1619
def test_lengthOfLastWord(self):
1720
length = self.sln.lengthOfLastWord('Hello world')
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
from unittest import TestCase
3+
from sln_1_100.solution_71_80 import Solution_71_80
4+
5+
class TestSolution_71_80(TestCase):
6+
def setUp(self):
7+
self.sln = Solution_71_80()
8+
9+
def test_simplifyPath(self):
10+
ret = self.sln.simplifyPath('/a/./b/../../c/')
11+
print(ret)
12+
ret2 = self.sln.simplifyPath('/a/./b///../c/../././../d/..//../e/./f/./g/././//.//h///././/..///')
13+
print(ret2)
14+
self.assertEqual(self.sln.simplifyPath('///'),'/')
15+
self.assertEqual(self.sln.simplifyPath('/...'), '/...')

0 commit comments

Comments
 (0)