Professional Documents
Culture Documents
Assignment Two
Assignment Two
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.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 current character with its uppercase equivalent if we haven't reached the
end of the string yet
while pos < len(s) and result[pos] == " ":
pos += 1
if pos < len(s):
result = result[0: pos] + \
result[pos].upper() + \
result[pos + 1: len(result)]
return result