Case Conversion Function
Case Conversion Function
In this kata, you will make a function that converts between camelCase, snake_case, and kebab-
case.
You must write a function that changes to a given case. It must be able to handle all three case
types:
To tackle this problem, we need to identify the case type of the input string and then convert it to the
requested case (`camel`, `snake`, or `kebab`). We'll also handle invalid cases by returning `None`.
1. Identify Case Type: Determine if the input string is in `camelCase`, `snake_case`, or `kebab-case`.
`camelCase`:Has no underscores or hyphens, and some letters are uppercase.
`snake_case`: Has only lowercase letters and underscores (`_`).
`kebab-case`: Has only lowercase letters and hyphens (`-`).
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/4
3. Invalid Cases:
If the input contains both underscores and hyphens, it's considered invalid.
If the input case type does not match any of the three expected types, return `None`.
If the target case is invalid, return `None`.
python
import re
# Test cases
print(change_case("snakeCase", "snake")) # "snake_case"
print(change_case("some-lisp-name", "camel")) # "someLispName"
print(change_case("map_to_all", "kebab")) # "map-to-all"
print(change_case("doHTMLRequest", "kebab")) # "do-h-t-m-l-request"
print(change_case("invalid-inPut_bad", "kebab")) # None
print(change_case("valid-input", "huh???")) # None
print(change_case("", "camel")) # ""
Explanation:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/4
For `snake_case`, we join words with `_`.
For `kebab-case`, we join words with `-`.
For `camelCase`, we capitalize all words except the first, then join them.
This solution ensures correct conversions and handles invalid inputs gracefully.
You're correct; the solution needs to account for inputs where `camelCase`-style uppercase letters are
mixed with `snake_case` or `kebab-case`. If there’s any mixture of `camelCase` capitalization with
underscores or hyphens, we should return `None`. Let’s update the function to include this validation.
python
import re
# Check for invalid input: both hyphens and underscores, or uppercase letters with und
erscores/hyphens
if '-' in identifier and '_' in identifier:
return None
if any(c.isupper() for c in identifier) and ('_' in identifier or '-' in identifier):
return None
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/4
# Test cases
print(change_case("snakeCase", "snake")) # "snake_case"
print(change_case("some-lisp-name", "camel")) # "someLispName"
print(change_case("map_to_all", "kebab")) # "map-to-all"
print(change_case("doHTMLRequest", "kebab")) # "do-h-t-m-l-request"
print(change_case("invalid-inPut_bad", "kebab")) # None
print(change_case("valid-input", "huh???")) # None
print(change_case("", "camel")) # ""
print(change_case("snakeCamel_case", "kebab")) # None
print(change_case("kebabCamel-case", "snake")) # None
print(change_case("case-Camel", "camel")) # None
Explanation of Changes:
New Validation: The condition `if any(c.isupper() for c in identifier) and ('_' in
identifier or '-' in identifier)` checks if there’s any uppercase letter together with either `_`
or `-`, marking it as invalid.
This ensures mixed-case strings with separators are caught and handled as invalid.
ChatGPT can make mistakes. Check important info.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/4