Camel Case 4

Sort by

recency

|

519 Discussions

|

  • + 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?

  • + 0 comments

    Python solution using inheritance:

    from typing import List, Dict
    from abc import ABC
    
    class Formatter(ABC):
      def combine(self, words: List[str]) -> str:
        var: str = ''
    
        for i, word in enumerate(words):
          if i != 0:
            var += word[0].upper() + word[1:]
          else:
            var += word
        return var
    
      def split(self, var: str) -> List[str]:
        words: List[str] = []
        curr_word: str = ''
    
        for c in var:
          if c.isupper():
            words.append(curr_word)
            curr_word = c.lower()
          else:
            curr_word += c
    
        words.append(curr_word)
        return words
    
    class VariableFormatter(Formatter):
      pass
    
    class MethodFormatter(Formatter):
      def combine(self, words: List[str]) -> str:
        return super().combine(words) + '()'
    
      def split(self, var: str) -> List[str]:
        return super().split(var[:-2])
    
    class ClassFormatter(Formatter):
      def combine(self, words: List[str]) -> str:
        combined: str = super().combine(words)
        return combined[0].upper() + combined[1:]
    
      def split(self, var: str) -> List[str]:
        return super().split(var[0].lower() + var[1:])
    
    formatter: Dict[str, Formatter] = {
      'V': VariableFormatter(),
      'M': MethodFormatter(),
      'C': ClassFormatter(),
    }
    
    while True:
      try:
        command, type, param = input().split(';')
        param = param.strip()
    
        if command == 'C':
          print(formatter[type].combine(param.split()))
        else:
          print(' '.join(formatter[type].split(param)))
      except:
        break