curriculum/challenges/english/blocks/daily-coding-challenges-python/6821ebd4237de8297eaee791.md
Given a string, return its camel case version using the following rules:
), dash (-), or underscore (_). Treat any sequence of these as a word break.to_camel_case("hello world") should return "helloWorld".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(to_camel_case("hello world"), "helloWorld")`)
}})
to_camel_case("HELLO WORLD") should return "helloWorld".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(to_camel_case("HELLO WORLD"), "helloWorld")`)
}})
to_camel_case("secret agent-X") should return "secretAgentX".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(to_camel_case("secret agent-X"), "secretAgentX")`)
}})
to_camel_case("FREE cODE cAMP") should return "freeCodeCamp".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(to_camel_case("FREE cODE cAMP"), "freeCodeCamp")`)
}})
to_camel_case("ye old-_-sea faring_buccaneer_-_with a - peg__leg----and a_parrot_ _named- _squawk") should return "yeOldSeaFaringBuccaneerWithAPegLegAndAParrotNamedSquawk".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(to_camel_case("ye old-_-sea faring_buccaneer_-_with a - peg__leg----and a_parrot_ _named- _squawk"), "yeOldSeaFaringBuccaneerWithAPegLegAndAParrotNamedSquawk")`)
}})
def to_camel_case(s):
return s
import re
def to_camel_case(s):
words = re.split(r'[_\- ]+', s)
camel = [
words[0].lower() if words else ''
] + [
word.capitalize() for word in words[1:]
]
return ''.join(camel)