Это вопрос от Codewars: завершите метод / функцию, чтобы она преобразовывала слова, разделенные тире / подчеркиванием, в верблюжий корпус. Первое слово в выходных данных следует использовать только с заглавной буквы, если исходное слово было написано с заглавной буквы (известный как верхний регистр верблюда, также часто называемый Pascal регистр).
Входные тестовые примеры следующие:
test.describe("Testing function to_camel_case")
test.it("Basic tests")
test.assert_equals(to_camel_case(''), '', "An empty string was provided but not returned")
test.assert_equals(to_camel_case("the_stealth_warrior"), "theStealthWarrior", "to_camel_case('the_stealth_warrior') did not return correct value")
test.assert_equals(to_camel_case("The-Stealth-Warrior"), "TheStealthWarrior", "to_camel_case('The-Stealth-Warrior') did not return correct value")
test.assert_equals(to_camel_case("A-B-C"), "ABC", "to_camel_case('A-B-C') did not return correct value")
Это то, что я пробовал до сих пор:
def to_camel_case(text):
str=text
str=str.replace(' ','')
for i in str:
if ( str[i] == '-'):
str[i]=str.replace('-','')
str[i+1].toUpperCase()
elif ( str[i] == '_'):
str[i]=str.replace('-','')
str[i+1].toUpperCase()
return str
Проходит первые два теста, но не основные:
test.assert_equals(to_camel_case("the_stealth_warrior"), "theStealthWarrior", "to_camel_case('the_stealth_warrior') did not return correct value")
test.assert_equals(to_camel_case("The-Stealth-Warrior"), "TheStealthWarrior", "to_camel_case('The-Stealth-Warrior') did not return correct value")
test.assert_equals(to_camel_case("A-B-C"), "ABC", "to_camel_case('A-B-C') did not return correct value")
Что я делать неправильно?