Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

CamelCase Conversion Functions

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is CamelCase Conversion Functions. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Andela's developer tooling needs consistent names for generated fields and readable labels for display. Implement both directions of a naming conversion: convert normal text into lower camelCase, then convert lower camelCase back into lowercase, space-separated text.

Formal Specification

Implement convert_case(text, mode), where text is a string and mode is either "to_camel" or "from_camel".

  1. For "to_camel", treat spaces, hyphens, and underscores as word separators. Return the first word in lowercase and capitalize the first character of every later word.
  2. For "from_camel", insert word boundaries before uppercase letters, including boundaries between an acronym and a normally capitalized word. Return all words in lowercase, separated by single spaces.
  3. Inputs contain only ASCII letters, digits, spaces, hyphens, and underscores. Leading, trailing, or repeated separators should be ignored.
  4. A camelCase input starts with a lowercase letter or digit and contains no separators.

Constraints

  • 0 <= len(text) <= 10^5
  • mode is either "to_camel" or "from_camel"
  • Input characters are ASCII letters, digits, spaces, hyphens, or underscores
  • Leading, trailing, and repeated separators may occur in normal text

Function Signature

def convert_case(text, mode):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output