Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5f65f4e

Browse files
python: add problem 6 and unittest
1 parent 588369c commit 5f65f4e

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

Python/sln_1_100/solution_1_10.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- coding: utf-8 -*-
2+
class Solution_1_10(object):
3+
def convert(self, s: str, numRows: int) -> str:
4+
"""
5+
6
6+
:param s:
7+
:param numRows:
8+
:return:
9+
"""
10+
if numRows == 1:
11+
return s
12+
13+
item = []
14+
arr = []
15+
i = 0
16+
while s:
17+
item.append(s[0])
18+
i += 1
19+
s = s[1:]
20+
if i == 2 * numRows - 2:
21+
arr.append(item)
22+
item = []
23+
i = 0
24+
if item:
25+
arr.append(item)
26+
final_res = []
27+
for row in range(numRows):
28+
for idx in range(len(arr)):
29+
if len(arr[idx]) < row + 1:
30+
continue
31+
final_res.append(arr[idx][row])
32+
if 0 < row < numRows - 1:
33+
if len(arr[idx]) < 2 * numRows - 1 - row:
34+
continue
35+
final_res.append(arr[idx][2 * numRows - 2 - row])
36+
return ''.join(final_res)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# -*- coding: utf-8 -*-
2+
from unittest import TestCase
3+
from sln_1_100.solution_1_10 import Solution_1_10
4+
5+
6+
class Test_Solution_1_10(TestCase):
7+
def setUp(self) -> None:
8+
self.sln = Solution_1_10()
9+
10+
def test_convert(self):
11+
self.assertEqual(self.sln.convert("PAYPALISHIRING", 3), "PAHNAPLSIIGYIR")
12+
self.assertEqual(self.sln.convert("AB", 1), "AB")

0 commit comments

Comments
 (0)