Skip to content

Commit dd1536a

Browse files
bite 149 - It took me 3 days and I think way too much.
1 parent f92f9dc commit dd1536a

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
/172/README.md
2525
/21/README.md
2626
/238/README.md
27+
/149/README.md

149/test_words.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from words import sort_words_case_insensitively
2+
3+
4+
def test_sort_words_case_insensitively():
5+
words = ("It's almost Holidays and PyBites wishes You a "
6+
"Merry Christmas and a Happy 2019").split()
7+
actual = sort_words_case_insensitively(words)
8+
expected = ['a', 'a', 'almost', 'and', 'and', 'Christmas',
9+
'Happy', 'Holidays', "It's", 'Merry', 'PyBites',
10+
'wishes', 'You', '2019']
11+
assert actual == expected
12+
13+
14+
def test_sort_words_case_insensitively_another_phrase():
15+
words = ("Andrew Carnegie's 64-room chateau at 2 East 91st "
16+
"Street was converted into the Cooper-Hewitt National "
17+
"Design Museum of the Smithsonian Institution "
18+
"in the 1970's").split()
19+
actual = sort_words_case_insensitively(words)
20+
expected = ['Andrew', 'at', "Carnegie's", 'chateau', 'converted',
21+
'Cooper-Hewitt', 'Design', 'East', 'in', 'Institution',
22+
'into', 'Museum', 'National', 'of', 'Smithsonian',
23+
'Street', 'the', 'the', 'the', 'was', "1970's", '2',
24+
'64-room', '91st']
25+
assert actual == expected
26+
27+
28+
def test_digit_inside_word_does_not_matter():
29+
"""We only care about the first char being a number"""
30+
words = ("It was the twenty9th of October when it was questioned"
31+
"the meaning of nuMbers and weather hiding a number Inside"
32+
"tex56t should be treated as a word or another number").split()
33+
actual = sort_words_case_insensitively(words)
34+
expected = ['a', 'a', 'and', 'another', 'as', 'be', 'hiding',
35+
'Insidetex56t', 'It', 'it', 'meaning', 'number', 'number',
36+
'nuMbers', 'October', 'of', 'of', 'or', 'questionedthe',
37+
'should', 'the', 'treated', 'twenty9th', 'was',
38+
'was', 'weather', 'when', 'word']
39+
assert actual == expected
40+
41+
42+
def test_words_with_mixed_chars_and_digits():
43+
words = ("Let's see how4 this 1sorts, hope it works 4 this "
44+
"B1te 22 55abc abc55").split()
45+
actual = sort_words_case_insensitively(words)
46+
expected = ['abc55', 'B1te', 'hope', 'how4', 'it', "Let's", 'see',
47+
'this', 'this', 'works', '1sorts,', '22', '4', '55abc']
48+
assert actual == expected

149/words.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
wordsx = "It's almost Holidays and PyBites wishes You a Merry Christmas and a Happy 2019".split()
2+
words = ['abc55', 'B1te', 'hope', 'how4', 'it', "Let's", 'see', 'this', 'this', 'works', '1sorts,', '22', '4', '55abc']
3+
4+
5+
def sort_words_case_insensitively(words):
6+
"""Sort the provided word list ignoring case, and numbers last
7+
(1995, 19ab = numbers / Happy, happy4you = strings, hence for
8+
numbers you only need to check the first char of the word)
9+
"""
10+
# numbers = list()
11+
words.sort(key=lambda x: x.lower())
12+
words.sort(key=lambda x: x[0].isdigit())
13+
# for key, word in enumerate(words):
14+
# if word[0].isdigit():
15+
# numbers.append(word)
16+
# words.pop(key)
17+
return words
18+
19+
20+
print(sort_words_case_insensitively(words))

0 commit comments

Comments
 (0)