Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

# Improve a string's capitalization.

# Capitalize the characters in a string that are appropriate.


# @param s the string that has to be capitalized
# @return a new string with the capitalization improved

def capitalize(s):

# Make a new copy of the string to return as the result of the function.
result = s

# Capitalize the string's first non-space character.


s = s.capitalize()
pos = 0

result = result[0: pos] + result[pos].upper() + result[pos + 1: len(result)]

# 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

# Day of the week, month and seasons


tup = list(result. lower().split())
res = ""
for x, item in enumerate(tup):
if item == "monday" or item == "tuesday" or item == "wednesday" or item ==
"thursday" or item == "friday" or item == "saturday" or item == "sunday" or item == "spring"
or item == "summer" or item == "autumn" or item == "fall" or item == "january" or item ==
"february" or item == "march" or item == "april" or item == "may" or item == "june" or item
== "july" or item == "august" or item == "september" or item == "october" or item ==
"november" or item == "december":
res += item.capitalize() + " "
else:
res += item + " "

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.

# Capitalize the letter that comes after a "?", "." or "!"


while pos < len(s):
if result[pos] == "." or result[pos] == "!" or result[pos] == "?":

# Move past the ‘‘?’’, ‘‘.’’ or ‘‘!’


pos = pos + 1

# Move past any spaces


while pos < len(s) and result[pos] == " ":
pos = pos + 1

# 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)]

# Move to the following character.


pos = pos + 1

# 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] == "’"):

# Without affecting any other characters, replace the i with an I.


result = result[0: pos] + "I" + result[pos + 1: len(result)]
pos = pos + 1
return result

# Show how to use the capitalize function.


def main():
s = input("Enter the text: ")
capitalized = capitalize(s)
print("Capitalized as:", capitalized)

# Execute the main program.


main()

You might also like