Camel Case 4

Sort by

recency

|

536 Discussions

|

  • + 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"))
    
  • + 0 comments

    I dont know what is wrong with this code. its give correct output still test cases are getting failed. can somebody help me with that. code in JSI don't know what is wrong with this code. It's giving the correct output, yet the test cases are failing. Can somebody help me with that? The code is in JS.

    function processData(input) { input = input.split('\n');

    // Helper function to combine words into camelCase
    function combineWords(type, name) {
        let words = name.split(' ');
    
        // Capitalize the first letter of each word (except for the first one in the case of methods/variables)
        let camelCaseName = words[0].toLowerCase() + words.slice(1)
            .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
            .join('');
    
        // Handle method, variable, and class naming rules
        if (type === 'M') {
            return camelCaseName + '()';  // Method name ends with parentheses
        } else if (type === 'V') {
            return camelCaseName;  // Variable name is just camelCase
        } else if (type === 'C') {
            return camelCaseName.charAt(0).toUpperCase() + camelCaseName.slice(1);  // Class name starts with uppercase
        }
    }
    
    // Helper function to split camelCase name into space-delimited words
    function splitCamelCase(name) {
        return name.split(/(?=[A-Z])/).join(' ').toLowerCase().replace('()', '');
    }
    
    // Process each line of input
    input.forEach(line => {
        let [operation, type, name] = line.split(';');
    
        // Perform split operation
        if (operation === 'S') {
            const splitName = splitCamelCase(name);
            console.log(splitName);
        }
        // Perform combine operation
        else if (operation === 'C') {
            const combinedName = combineWords(type, name);
            console.log(combinedName);
        }
    });
    

    }