Skip to content

Instantly share code, notes, and snippets.

@DaniloOliveira28
Created June 20, 2016 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaniloOliveira28/a7323865a89aaf6c242ba9708f9bbf13 to your computer and use it in GitHub Desktop.
Save DaniloOliveira28/a7323865a89aaf6c242ba9708f9bbf13 to your computer and use it in GitHub Desktop.
def n_lower_chars(string):
return sum(1 for c in string if c.islower())
def n_lower_chars_position(string):
return list((x, string[x] + "->" + string[x].upper())
for x in range(0, len(string)) if string[x].islower())
def format_word(word):
if word == word.upper() or word == word.lower():
return 0
M = dict()
M[0] = {}
M[0]["cost"] = 0
M[0]["replace"] = []
for i in range(1, len(word)):
M[i] = {}
if word[i] == word[i].lower():
M[i]["cost"] = M[i - 1]["cost"]
M[i]["replace"] = M[i - 1]["replace"]
elif word[i] == word[i].upper():
if(n_lower_chars(word[:i]) < M[i - 1]["cost"] + 1):
M[i]["cost"] = n_lower_chars(word[:i])
M[i]["replace"] = (n_lower_chars_position(word[:i]))
else:
M[i]["cost"] = M[i - 1]["cost"] + 1
aux = (i, word[i] + "->" + word[i].lower())
M[i]["replace"] = M[i - 1]["replace"] + [aux]
return M[len(word) - 1]
if __name__ == "__main__":
print format_word("CasaDanilo")
print format_word("AaAaAAAaAAAaAAaaaaaaA")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment