-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefactory.py
More file actions
31 lines (24 loc) · 903 Bytes
/
refactory.py
File metadata and controls
31 lines (24 loc) · 903 Bytes
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
#!/usr/local/bin/python3
small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
def book_title(title):
""" Takes a string and returns a title-case string.
All words EXCEPT for small words are made title case
unless the string starts with a preposition, in which
case the word is correctly capitalized.
>>> book_title('DIVE Into python')
'Dive into Python'
>>> book_title('the great gatsby')
'The Great Gatsby'
>>> book_title('the WORKS OF AleXANDer dumas')
'The Works of Alexander Dumas'
"""
lst_of_words = title.lower().split()
for index, w in enumerate(lst_of_words):
if w not in small_words or index == 0:
lst_of_words[index] = lst_of_words[index].title()
return " ".join(lst_of_words)
def _test():
import doctest, refactory
return doctest.testmod(refactory)
if __name__ == "__main__":
_test()