Camel Case 4

Sort by

recency

|

537 Discussions

|

  • + 1 comment

    In python:

    import sys
    
    def split_by_upperCase(word: str):
        split_word = ""
        for i in range(1, len(word)):
            if word[i].isupper():
                upper_case = word[i]
                split_word = raw_str.replace(upper_case, f" {upper_case}", 1)
        return split_word.lower().replace("()", "")
        
    def combine_by_space(word: str):
        split_word = word.split()
        combined_word = split_word[0]
        for wd in split_word[1:]:
            combined_word += wd.capitalize()
        return combined_word
        
    
    userInput = sys.stdin.readlines()
    outputs = []
    for line in userInput:
        tokens = line.strip().split(";")
        raw_str:str = tokens[2]
        
        formatted_str = ""
        if tokens[0] == "S":
            formatted_str = split_by_upperCase(raw_str)
        else:
            combined_word = combine_by_space(raw_str)
            if tokens[1] == "V":
                formatted_str = combined_word
            elif tokens[1] == "M":
                formatted_str = f"{combined_word}()"
            else:
                first_letter = combined_word[0]
                formatted_str = combined_word.replace(first_letter, first_letter.upper(), 1)
                
        outputs.append(formatted_str)
        
    for output in outputs:
    
    print(output)
    
  • + 0 comments

    Working code in python:

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    import sys
    import re
    
    def caseChange(input_lines): 
        lines = input_lines.strip().split("\n")
        
        for line in lines: 
            operation, var_type, words = line.split(";")
            #print(f"INPUT: {line}")
            #print(f"Operation:{operation}, Variable: {var_type}, Words:{words}")
        
            #split -- camel case to space-delimited list of words
            if operation == "S":
                words = re.sub(r'([a-z])([A-Z])', r'\1 \2', words).lower()
                output = words
              
                if var_type == "M":
                    words = words.rstrip("()")
                    output = words
                
                print(output)
        
            
        #combine -- space-delimited to camelCase
            elif operation == "C":
                split_words = words.split()
                
                if var_type == "C":
                    output = ''.join(word.capitalize() for word in split_words)
                    
                else: 
                    output = split_words[0].lower() + "".join(word.capitalize() for word in split_words[1:])
                
                if var_type == "M":
                    output += "()"
                
                print(output)
    
            
    if __name__ == "__main__":
        for line in sys.stdin:
            caseChange(line.strip("\r\n"))
            
    
  • + 0 comments

    It is show the answer ,as it expected but still smae issue "wrong answer"

    function processInput(input) {
        const [operation, type, words] = input.split(";");
    
        if (operation === "S") {
            // SPLIT operation: Insert spaces before uppercase letters, remove "()" for methods
            let result = words.replace(/\(\)$/, '')  // Remove () for methods
                              .replace(/([A-Z])/g, ' $1')  // Insert space before uppercase
                              .toLowerCase()
                              .trim();
            console.log(result);
        } 
        
        else if (operation === "C") {
            // COMBINE operation: Convert to CamelCase
            let wordsArray = words.split(" ");
            let result = wordsArray.map((word, index) =>
                index === 0 && type !== "C" ? word.toLowerCase()  // First word lowercase (except Class)
                : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()  // Capitalize others
            ).join('');
    
            if (type === "M") result += "()"; // Append () for methods
            console.log(result);
        }
    }
    

    Wrong Answer Input (stdin) S;V;iPad C;M;mouse pad C;C;code swarm S;C;OrangeHighlighter Your Output (stdout) i pad mousePad () CodeSwarm orange highlighter Expected Output i pad mousePad() CodeSwarm orange highlighter

  • + 1 comment

    This works fine using Java public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
    
        while(sc.hasNextLine()){
             String line = sc.nextLine();
             String [] parts = line.split(";");
             String operation = parts[0];
             String type = parts[1];
             String text = parts[2];
    
        if(operation.equals("S")){
            String result = split(type, text);
            System.out.println(result);
        } else if(operation.equals("C")){
            String result = combine(type, text);
            System.out.println(result);
        }
    }
       sc.close();
    }
    public static String split(String type, String text){
        StringBuilder result = new StringBuilder();
        for(int i = 0; i < text.length(); i++){
            char c = text.charAt(i);
            if(Character.isUpperCase(c)){
                if(i > 0)
                result.append(" ");
            result.append(Character.toLowerCase(c));   
            } else if( c != '(' && c != ')'){
            result.append(c);
          } 
        } 
        return result.toString();
       }
    public static String combine(String type, String text){
        StringBuilder result = new StringBuilder();
        String[] words = text.split(" ");
        for(int i = 0; i< words.length; i++){
            String word = words[i];
            if(i == 0 && !type.equals("C"))
               result.append(word.toLowerCase());
            else {
                result.append(Character.toUpperCase(word.charAt(0)));
                result.append(word.substring(1).toLowerCase());
            }   
        } 
        if(type.equals("M"))
           result.append("()");
        return result.toString();
    }
    
  • + 0 comments

    I am able to solve this in python

    import re
    import sys
    
    def process_input(s_input):
        operation, type, words = map(str, s_input.split(';'))
        if operation == 'S':
            a = re.sub('([A-Z])', r' \1', words).lower().split()
            if type == 'M':
                a = a[:-2]
            print(" ".join(a))
        if operation == 'C':
            a = words.title().replace(' ', '')
            if type == 'V':
                a = a[:1].lower() + a[1:]
            elif type == 'M':
                a = a[:1].lower() + a[1:] + '()'
            print(a)
    
    if __name__ == "__main__":
        for line in sys.stdin:
            process_input(line.strip("\r\n"))