Professional Documents
Culture Documents
Abcd
Abcd
def capitalize(s):
# Make a new copy of the string to return as the result of the function.
result = s
# Ordinal numbers
tup = list(result. lower().split())
res = ""
for x, item in enumerate(tup):
if item == "twenty" or item == "thirty" or item == "forty" or item == "fifty" or item ==
"sixty" or item == "seventy" or item == "eighty" or item == "ninety":
if tup[x+1] == "first" or tup[x+1] == "second" or tup[x+1] == "third" or tup[x+1] ==
"fourth" or tup[x+1] == "fifth" or tup[x+1] == "sixth" or tup[x+1] == "seventh" or tup[x+1]
== "eighth" or tup[x+1] == "ninth":
res += item.capitalize() + "-"
else:
res += item + " "
else:
res += item + " "
result = res
result = res
# Fractions
tup = list(result. lower().split())
res = ""
for x, item in enumerate(tup):
if item == "one" or item == "zero" or item == "two" or item == "three" or item == "four"
or item == "five" or item == "six" or item == "seven" or item == "eight" or item == "nine" or
item == "ten" or item == "eleven" or item == "twelve" or item == "thirteen" or item ==
"fourteen" or item == "fifteen" or item == "sixteen" or item == "seventeen" or item ==
"eighteen" or item == "nineteen" or item == "twenty":
if tup[x+1] == "half" or tup[x+1] == "third" or tup[x+1] == "quarter" or tup[x+1] ==
"fifth" or tup[x+1] == "sixth" or tup[x+1] == "seventh" or tup[x+1] == "eighth" or tup[x+1]
== "ninth" or tup[x+1] == "tenth" or tup[x+1] == "eleventh" or tup[x+1] == "twelfth" or
tup[x+1] == "thirteeth" or tup[x+1] == "fourteeth" or tup[x+1] == "fifteenth" or tup[x+1] ==
"sixteenth" or tup[x+1] == "seventeenth" or tup[x+1] == "eighteenth" or tup[x+1] ==
"nineteenth" or tup[x+1] == "twentieth":
res += item + "-"
else:
res += item + " "
elif item == "zero" or item == "two" or item == "three" or item == "four" or item ==
"five" or item == "six" or item == "seven" or item == "eight" or item == "nine" or item ==
"ten" or item == "eleven" or item == "twelve" or item == "thirteen" or item == "fourteen" or
item == "fifteen" or item == "sixteen" or item == "seventeen" or item == "eighteen" or item
== "nineteen" or item == "twenty":
if tup[x+1] == "thirds" or tup[x+1] == "quarters" or tup[x+1] == "fifths" or tup[x+1]
== "sixths" or tup[x+1] == "sevenths" or tup[x+1] == "eighths" or tup[x+1] == "ninths" or
tup[x+1] == "tenths" or tup[x+1] == "elevenths" or tup[x+1] == "twelfths" or tup[x+1] ==
"thirteeths" or tup[x+1] == "fourteeths" or tup[x+1] == "fifteenths" or tup[x+1] ==
"sixteenths" or tup[x+1] == "seventeenths" or tup[x+1] == "eighteenths" or tup[x+1] ==
"nineteenths" or tup[x+1] == "twentieths":
res += item + "-"
else:
res += item + " "
else:
res += item + " "
result = res
# Replace the uppercase version of the character without affecting any other characters.
# Replace the current character with its uppercase equivalent if we haven't reached the end of
the string yet
if pos < len(s):
result = result[0: pos] + \
result[pos].upper() + \
result[pos + 1: len(result)]
# When I is preceded by a space and followed by a space, question mark, period, exclamation
mark or apostrophe, capitalize it.
pos = 1
while pos < len(s) - 1:
if result[pos - 1] == " " and result[pos] == "i" and \
(result[pos + 1] == " " or result[pos + 1] == "." or \
result[pos + 1] == "!" or result[pos + 1] == "?" or \
result[pos + 1] == "’"):