-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlength-of-last-word.py
More file actions
57 lines (39 loc) · 2.12 KB
/
length-of-last-word.py
File metadata and controls
57 lines (39 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# https://leetcode.com/problems/length-of-last-word/
# Given a string s consists of upper/lower-case alphabets and empty space characters return the length of last word in the string.
# If the last word does not exist, return 0.
# Note: A word is defined as a character sequence consists of non-space characters only.
# For example,
# Given s = "Hello World",
# return 5.
import unittest
class Solution:
# @param s, a string
# @return an integer
def lengthOfLastWord(self, s):
s = s.strip()
string_list = s.split(' ')
return len(string_list[len(string_list) - 1])
class TestLastWordLength(unittest.TestCase):
my_solution = Solution()
def test_default_pass(self):
self.assertEqual(self.my_solution.lengthOfLastWord("Hello World"),5)
def test_leading_spaces_string(self):
self.assertEqual(self.my_solution.lengthOfLastWord(" Hey you world"),5)
def test_trailing_spaces_string(self):
self.assertEqual(self.my_solution.lengthOfLastWord("Hiiiiiiii World "),5)
def test_uppercase_last_word(self):
self.assertEqual(self.my_solution.lengthOfLastWord("hello WORLD"),5)
def test_empty_string(self):
self.assertEqual(self.my_solution.lengthOfLastWord(""),0)
def test_one_word_string(self):
self.assertEqual(self.my_solution.lengthOfLastWord("dkjhvalsjvhjkalvhgdslahkvacsdhksfashjkfnjdafsoyucigkhj;gudsioygkjhjldfsyuig"),75)
def test_one_word_integer_string(self):
self.assertEqual(self.my_solution.lengthOfLastWord("6787676543456789876543456789"),28)
def test_last_word_integer_string(self):
self.assertEqual(self.my_solution.lengthOfLastWord("helllo world 678767654345678987654345678990"),30)
def test_one_word_special_charaters(self):
self.assertEqual(self.my_solution.lengthOfLastWord("$%Y$#$!%$@&%^$!%@#$#^%$&^*(&)*^$%$#@!#@^$&%*$%(&)^*_&(+_"),56)
def test_last_word_special_characters(self):
self.assertEqual(self.my_solution.lengthOfLastWord("hello World $%Y$#$!%$@&%^$!%@#$#^%$&^*(&)*^$%$#@!#@^$&%*$%(&)^*_&(+_$$##"),60)
if __name__ == "__main__":
unittest.main()