Problem 255218 · medium · Phase 02 Linear Data Structures

Flip the Identifier Style

strings · transformation · conditionals

Programmers switch between snake_case and camelCase. Write flip_case_style(name) that converts an identifier from one style to the other:

  • If name contains an underscore, it is snake_case: split on underscores, drop empty pieces, lowercase the first piece and capitalise each later piece (first letter upper, rest lower), then join with nothing in between.
  • Otherwise it is camelCase: insert _ before an uppercase letter that starts a new word and lowercase everything. An uppercase letter starts a new word when the previous character is not uppercase, or when it is uppercase but the next character is a lowercase letter (so acronyms stay together: getHTTPResponse -> get_http_response).

Examples

Input:  name = "user_first_name"
Output: "userFirstName"

Input:  name = "getHTTPResponse"
Output: "get_http_response"

Input:  name = "MAX_retry_COUNT"
Output: "maxRetryCount"

Constraints

  • 0 <= len(name) <= 10**4, letters, digits and underscores only
  • Target: O(n) time

Goals

  • Detect which naming convention an identifier uses
  • Split and recombine on underscores
  • Insert separators at word boundaries, keeping acronyms together
Starting Python…