-
Notifications
You must be signed in to change notification settings - Fork 0
/
homework_w_mentor_1.py
107 lines (71 loc) · 3.22 KB
/
homework_w_mentor_1.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import unittest
def get_longest_str(input_list):
"""Returns the longest string"""
longest_str = ""
for element in input_list:
if len(element) >= len(longest_str):
longest_str = element
return longest_str
def get_kth_longest_str(input_list, k=1):
""" Returns the kth longest string """
# bubblesort_list = input_list
# for i in range(len(input_list) - 1):
# for x in range(len(input_list) - 1 - i):
# if len(bubblesort_list[x]) < len(bubblesort_list[x+1]):
# temp = bubblesort_list[x]
# bubblesort_list[x] = bubblesort_list[x+1]
# bubblesort_list[x+1] = temp
# return bubblesort_list[k-1]
sorted_list = []
for i in range(len(input_list)):
longest_str = get_longest_str(input_list)
sorted_list.append(longest_str)
input_list.remove(longest_str)
return sorted_list[k - 1]
# Next Solution: split the list in two with shortest, longest lists then sort each list
class KthLongestTest(unittest.TestCase):
def test_three_strings_longest_is_last(self):
result = get_longest_str(["A", "String", "But this is longer"])
self.assertEqual(result, "But this is longer")
def test_three_strings_longest_is_middle(self):
result = get_longest_str(["A", "But this is longer", "String"])
self.assertEqual(result, "But this is longer")
def test_three_strings_tie_for_longest(self):
result = get_longest_str(["A", "String 1", "String 2"])
self.assertEqual(result, "String 2")
def test_longest_element_and_k_is_one(self):
result = get_kth_longest_str(["String 1"], 1)
self.assertEqual(result, "String 1")
def test_two_elements_get_longest_and_k_is_one(self):
result = get_kth_longest_str(["A", "String 1"], 1)
self.assertEqual(result, "String 1")
def test_two_elements_first_is_longer_and_k_is_one(self):
result = get_kth_longest_str(["String 1", "A"], 1)
self.assertEqual(result, "String 1")
def test_three_elements_first_is_longest_and_k_is_two(self):
result = get_kth_longest_str(["A", "But this is longer", "String 1"], 2)
self.assertEqual(result, "String 1")
def test_three_elements_two_same_entry_and_k_is_two(self):
result = get_kth_longest_str(["A", "String", "But this is longer"], 2)
self.assertEqual(result, "String")
def test_four_elements_and_k_is_two(self):
result = get_kth_longest_str(
["A", "String 1", "String", "But this is longer"], 2
)
self.assertEqual(result, "String 1")
def test_four_elements_and_k_is_three(self):
result = get_kth_longest_str(
["A", "String 1", "String", "But this is longer"], 3
)
self.assertEqual(result, "String")
def test_four_elements_and_k_is_four(self):
result = get_kth_longest_str(
["A", "String 1", "String", "But this is longer"], 4
)
self.assertEqual(result, "A")
def test_four_elements_and_k_is_four_in_diff_order(self):
result = get_kth_longest_str(
["String 2", "But this is longer", "A", "String 1"], 4
)
self.assertEqual(result, "A")
unittest.main()