JS switch语句与回调函数KATA练习:多Casing风格转换终端输出问题
Solution for Multi-Casing Style String Conversion
Overview
You’re tasked with converting an input string into specified casing styles (with priority: camel > pascal > snake > kebab) — handling both single style inputs and arrays of styles, with all output printed directly to the terminal. Here’s a practical, terminal-friendly implementation to solve this.
Step-by-Step Approach
- Normalize the Input: First, we parse the input string into clean words. We prioritize splitting camelCase/PascalCase first (per the given priority) by inserting spaces before uppercase letters, then split on any non-alphanumeric characters (like hyphens, underscores, spaces) to get a list of lowercase base words.
- Style Conversion Functions: Build dedicated functions for each casing style to convert the normalized word list into the desired format.
- Handle Multiple Styles: Accept either a single style or an array of styles, apply each conversion, and print results clearly to the terminal.
Code Implementation (Python)
import re def split_into_words(input_str): # Prioritize splitting camelCase/PascalCase first camel_split = re.sub('([a-z0-9])([A-Z])', r'\1 \2', input_str) # Split on any non-alphanumeric characters and filter empty strings words = re.split(r'[^a-zA-Z0-9]', camel_split) return [word.lower() for word in words if word] def to_camel_case(words): if not words: return "" return words[0] + ''.join(word.capitalize() for word in words[1:]) def to_pascal_case(words): return ''.join(word.capitalize() for word in words) def to_snake_case(words): return '_'.join(words) def to_kebab_case(words): return '-'.join(words) def convert_string(input_str, casing_styles): words = split_into_words(input_str) style_map = { 'camel': to_camel_case, 'pascal': to_pascal_case, 'snake': to_snake_case, 'kebab': to_kebab_case } results = {} for style in casing_styles: clean_style = style.strip().lower() if clean_style in style_map: results[clean_style] = style_map[clean_style](words) else: results[clean_style] = f"⚠️ Unsupported style: {clean_style}" return results def main(): print("=== String Casing Converter ===") input_str = input("Enter your string: ") styles_input = input("Enter casing styles (comma-separated, e.g., camel,pascal): ") casing_styles = styles_input.split(',') conversions = convert_string(input_str, casing_styles) print("\n📋 Conversion Results:") for style, result in conversions.items(): print(f"{style.capitalize()}Case: {result}") if __name__ == "__main__": main()
How It Works
- Input Normalization: The
split_into_wordsfunction ensures we correctly parse strings with mixed styles (likeHello-World_testorhelloWorldTest) into a consistent list of lowercase words. - Style Functions: Each function takes the normalized word list and transforms it:
- camelCase: First word lowercase, subsequent words capitalized with no separators.
- PascalCase: All words capitalized with no separators.
- snake_case: Words joined with underscores, all lowercase.
- kebab-case: Words joined with hyphens, all lowercase.
- Terminal Interaction: The
mainfunction handles user input, runs conversions, and prints results in a readable format directly to the terminal.
Example Usage
If you run the script and input:
Enter your string: My-Favorite_ProgrammingLanguage Enter casing styles (comma-separated): camel, snake, kebab
You’ll get this output:
📋 Conversion Results: CamelCase: myFavoriteProgramminglanguage SnakeCase: my_favorite_programminglanguage KebabCase: my-favorite-programminglanguage
内容的提问来源于stack exchange,提问作者ashToronto
相关产品推荐
相关产品推荐

