Skip to content

Commit 09e63c4

Browse files
python: add problem 38 and test function
1 parent 5f28408 commit 09e63c4

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Python/solution_31_40.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: UTF-8 -*-
2+
3+
class Solution_31_40:
4+
def countAndSay(self, n):
5+
"""
6+
:type n: int
7+
:rtype: str
8+
"""
9+
curr = '1'
10+
for i in range(1, n):
11+
prev = curr
12+
curr = ''
13+
count = 1
14+
say = prev[0]
15+
for ch in prev[1:]:
16+
if say == ch:
17+
count += 1
18+
else:
19+
curr += str(count) + say
20+
say = ch
21+
count = 1
22+
curr += str(count) + say
23+
return curr

Python/test_solution_31_40.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from unittest import TestCase
2+
3+
from 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_countAndSay(self):
12+
self.assertEqual(self.sln.countAndSay(2), '11')
13+
self.assertEqual(self.sln.countAndSay(4), '1211')

0 commit comments

Comments
 (0)