Skip to content

Commit 0f1673d

Browse files
python: add problem 58 and unittest
1 parent 9d7f6ea commit 0f1673d

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

Python/solution_51_60.py renamed to Python/sln_1_100/solution_51_60.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,30 @@ def spiralOrder(self, matrix):
4141
curr_direction = (curr_direction + 1) % 4
4242

4343
return res
44+
45+
def lengthOfLastWord(self, s):
46+
"""
47+
58
48+
:type s: str
49+
:rtype: int
50+
"""
51+
if len(s) <= 1:
52+
if not s or s == ' ':
53+
return 0
54+
else:
55+
return 1
56+
start = end = len(s)
57+
index = len(s) - 2
58+
last_ch = s[-1]
59+
while index >= 0:
60+
cur_ch = s[index]
61+
if last_ch == ' ' and cur_ch != ' ':
62+
end = index + 1
63+
elif cur_ch == ' ' and last_ch != ' ':
64+
start = index + 1
65+
break
66+
if index == 0 and cur_ch != ' ':
67+
start = index
68+
last_ch = cur_ch
69+
index -= 1
70+
return end - start
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from unittest import TestCase
2+
from sln_1_100.solution_51_60 import Solution_51_60
3+
4+
5+
# -*- coding: UTF-8 -*-
6+
class TestSolution_51_60(TestCase):
7+
def setUp(self):
8+
self.sln = Solution_51_60()
9+
10+
def test_spiralOrder(self):
11+
m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
12+
res = [1, 2, 3, 6, 9, 8, 7, 4, 5]
13+
self.assertEqual(self.sln.spiralOrder(m), res)
14+
self.assertEqual(self.sln.spiralOrder([[1]]), [1])
15+
16+
def test_lengthOfLastWord(self):
17+
length = self.sln.lengthOfLastWord('Hello world')
18+
self.assertEqual(length, 5)
19+
self.assertEqual(self.sln.lengthOfLastWord(' '), 0)
20+
self.assertEqual(self.sln.lengthOfLastWord('a '), 1)

Python/test_solution_51_60.py

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

0 commit comments

Comments
 (0)