Camel Case 4

Sort by

recency

|

520 Discussions

|

  • + 0 comments
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner input = new Scanner(System.in);
            while(input.hasNextLine()){
                String next = input.nextLine();
                char operation = next.charAt(0);
                char type = next.charAt(2);
                String item = next.substring(4);
                if(operation=='S'){
                    String result = new String();
                    for(char i : item.toCharArray()){
                        if(type=='M'&& i=='('){
                            break;
                        }
                        if(result.isEmpty()==false && Character.isUpperCase(i)){
                            result+=' ';
                        }
                        result+=Character.toLowerCase(i);
                    }
                    System.out.println(result);
                }
                if(operation=='C'){
                    String result = new String();
                    int flag = 0 ;
                    for(char i : item.toCharArray()){
                        if(result.isEmpty()){
                            if(type=='C'){
                                result+=Character.toUpperCase(i);
                                continue;
                            }  
                        }
                        if(flag!=1 && i!=' '){
                            result+=i;
                        }
                        if(flag == 1){
                            result+=Character.toUpperCase(i);
                            flag = 0;
                        }
                        if(i==' '){
                            flag = 1;
                            continue;
                        }
                    }
                    if(type=='M'){
                        result+="()";
                    }
                    System.out.println(result);
                }
            }
        }
    }
    
  • + 1 comment

    Hi everyone! Here’s my solution to this exercise. I’m currently having an issue with Test Case 2. When I run the code, it seems to produce the expected result, but it’s still not passing. Does anyone have an idea of what might be going on?

    import re
    import sys
    # Enter your code here. Read input from STDIN. Print output to STDOUT
    
    def split_word(word):
        word = re.sub(r'\(\)$', r'', word)        
        word = re.sub(r'(?<!^)([A-Z])', r' \1', word)
        word = word.lower()
        print(word)
        
        
    def combine_word(word, word_type):
        word = word.strip()
        if word_type == 'M':
            word = word + "()"
        elif word_type == 'C':
            word = word.capitalize()
        word = re.sub(r'\s([a-z])', lambda x: x.group(1).upper(), word)
                
        print(word)
    
    
    def camelCase(action, word_type, word):
        if action == 'S':
            split_word(word)
        elif action == 'C':
            combine_word(word, word_type)
    
    input_lines = sys.stdin.read().strip().split('\n')
    for line in input_lines:
        action, word_type, word = line.split(';')
        camelCase(action, word_type, word)
    
  • + 0 comments

    def camelCasing(word): if word[0] == 'S': #Split Operation new_word = word[4:] result = ''

        if word[2] == 'V':          #For Variables
            for letter in new_word:
                if letter.isupper():
                   result += ' ' + letter.lower()
                else: 
                    result += letter
            print(result)
    
        elif word[2] == 'M':            #For Methods
            for letter in new_word:
                if letter.isupper():
                   result += ' ' + letter.lower()
                elif letter == '(':
                    result = result
                elif letter == ')':
                    result = result    
                else:
                    result += letter
            print(result)        
    
        elif word[2] == 'C':            #For Classes
            new_word = new_word[0].lower() + new_word[1:]
            for letter in new_word:
                if letter.isupper():
                   result += ' ' + letter
                else:
                    result += letter   
            fresult = result.lower()       
            print(fresult)
    
    if word[0] == 'C':          #Combine Operations
        n_word = word[4:]
        new_word = n_word.split()
        result = ''
    
        if word[2] == 'V':          #For Variables
            result += new_word[0].lower() + ''.join(letter.capitalize() for letter in new_word[1:])
            print(result)
    
        if word[2] == 'M':          #For Methods
            result += new_word[0].lower() + ''.join(letter.capitalize() for letter in new_word[1:])
            result +='()'
            print(result)
    
        if word[2] == 'C':          # For Classes
            for letter in new_word:
                result = ''.join(letter.capitalize() for letter in new_word)
            print(result)
    

    if name == 'main': import sys

    input_lines = sys.stdin.read().strip().split('\n')  # Read all input lines
    for line in input_lines:
        camelCasing(line)
    
  • + 0 comments

    My Python3 solution:

    import sys
    all_inp = sys.stdin.read().split('\r\n')
    
    for e in all_inp:
        if e[0] == 'S':
            s = e[4].lower()
            for i, l in enumerate(e[5:]):
                if l.isupper():
                    s += ' '+l.lower()
                else: s += l.lower()
            print(s if e[2] != 'M' else s[:-2]) 
        else:
            s = e[4].lower() if e[2] != "C" else e[4].upper() 
            for i, l in enumerate(e[5:]):
                if l == ' ':
                    continue
                if e[i+(5-1)] == ' ':
                    s += l.upper()
                else:
                    s += l.lower()
            print(s if e[2] != 'M' else s+'()')
        
                    
                    
                
                
    
  • + 1 comment

    How can I have my function to read the several lines of the input and produce the several lines output?