Skip to content

Commit 3e6d293

Browse files
python: add problem 35 and unittest
1 parent 0f1673d commit 3e6d293

File tree

4 files changed

+63
-36
lines changed

4 files changed

+63
-36
lines changed

Python/sln_1_100/solution_31_40.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*- coding: UTF-8 -*-
2+
3+
class Solution_31_40:
4+
def searchInsert(self, nums, target):
5+
"""
6+
35
7+
:type nums: List[int]
8+
:type target: int
9+
:rtype: int
10+
"""
11+
index = 0
12+
if len(nums) and nums[-1] < target:
13+
return len(nums)
14+
for idx, (prev, curr) in enumerate(zip(nums[:-1], nums[1:])):
15+
if prev < target <= curr:
16+
index = idx + 1
17+
break
18+
elif target < curr:
19+
break
20+
21+
return index
22+
23+
def countAndSay(self, n):
24+
"""
25+
38
26+
:type n: int
27+
:rtype: str
28+
"""
29+
curr = '1'
30+
for i in range(1, n):
31+
prev = curr
32+
curr = ''
33+
count = 1
34+
say = prev[0]
35+
for ch in prev[1:]:
36+
if say == ch:
37+
count += 1
38+
else:
39+
curr += str(count) + say
40+
say = ch
41+
count = 1
42+
curr += str(count) + say
43+
return curr
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from unittest import TestCase
2+
3+
from sln_1_100.solution_31_40 import Solution_31_40
4+
5+
6+
# -*- coding: UTF-8 -*-
7+
class TestSolution_31_40(TestCase):
8+
def setUp(self):
9+
self.sln = Solution_31_40()
10+
11+
def test_searchInsert(self):
12+
ret1 = self.sln.searchInsert([1, 3, 5, 6], 5)
13+
self.assertEqual(ret1, 2)
14+
self.assertEqual(self.sln.searchInsert([], 0), 0)
15+
self.assertEqual(self.sln.searchInsert([1, 2, 3], 0), 0)
16+
self.assertEqual(self.sln.searchInsert([1, 2, 3], 10), 3)
17+
18+
def test_countAndSay(self):
19+
self.assertEqual(self.sln.countAndSay(2), '11')
20+
self.assertEqual(self.sln.countAndSay(4), '1211')

Python/solution_31_40.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

Python/test_solution_31_40.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)