Domanda

This program is meant to change something from normal to camelCase. EG: Not_Camel_case -> notCamelCase or Camel_Case to camelCase.

def titlecase(value):
    return "".join(word.title() for word in value.split("_"))

def titlecase2(value):
    return value[:1].lower() + titlecase(value)[1:]
def to_camel(value):
    return titlecase2(value)

This outputs what i want BUT..... This is for a competition and putting in Not_An_SMS returns notAnSms instead of notAnSMS? Also putting in num2words is supposed to return the same but instead my program capitalizes it like num2Words. What do i do to fix these problems?

EDIT: I have to change things within the functions not the output as the comp directly checks the functions in particular to_camel.

È stato utile?

Soluzione

If you want Not_An_SMS to return notAnSms, you have to stop using the word.title() function, and instead just uppercase/lowercase the first letter of each word, but preserve the case of the others.

That will still mean that SMS_status will return sMSStatus but I guess that can be considered an exception. If you want to fix that you'll need to have a dictionary of words so you can figure out if something is a word or not. And that's certainly not withing the scope of the competition.

Altri suggerimenti

This simplified piece is stolen from Rails:

acronyms={}
def camelize(identifier, upper_first=False):
    words = identifier.split('_')
    if upper_first:
        return ''.join(acronyms.get(w) or w.capitalize() for w in words)
    else:
        return (acronyms.get(words[0]) or words[0].lower()) + \
               ''.join(acronyms.get(w) or w.capitalize() for w in words[1:])

acronyms['SMS']='SMS'
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top