Camel Case 4 [Hacker Rank]

Kim Hayeon·2023년 4월 18일
0

Algorithm Study

목록 보기
7/37
post-thumbnail

Question

Camel Case is a naming style common in many programming languages. In Java, method and variable names typically start with a lowercase letter, with all subsequent words starting with a capital letter (example: startThread). Names of classes follow the same pattern, except that they start with a capital letter (example: BlueCar).

Your task is to write a program that creates or splits Camel Case variable, method, and class names.

Input Format

  • Each line of the input file will begin with an operation (S or C) followed by a semi-colon followed by M, C, or V followed by a semi-colon followed by the words you'll need to operate on.
  • The operation will either be S (split) or C (combine)
  • M indicates method, C indicates class, and V indicates variable
    In the case of a split operation, the words will be a camel case method, class or variable name that you need to split into a space-delimited list of words starting with a lowercase letter.
  • In the case of a combine operation, the words will be a space-delimited list of words starting with lowercase letters that you need to combine into the appropriate camel case String. Methods should end with an empty set of parentheses to differentiate them from variable names.

Output Format

  • For each input line, your program should print either the space-delimited list of words (in the case of a split operation) or the appropriate camel case string (in the case of a combine operation).

Sample Input

S;M;plasticCup()
C;V;mobile phone
C;C;coffee machine
S;C;LargeSoftwareBook
C;M;white sheet of paper
S;V;pictureFrame

Sample Output

plastic cup
mobilePhone
CoffeeMachine
large software book
whiteSheetOfPaper()
picture frame

Explanation

  • Use Scanner to read in all information as if it were coming from the keyboard.

  • Print all information to the console using standard output (System.out.print() or System.out.println()).

  • Outputs must be exact (exact spaces and casing).

풀이


# Enter your code here. Read input from STDIN. Print output to STDOUT
import sys
lines = sys.stdin.readlines()
lines = [line.rstrip() for line in lines]
for s in lines:
    word = s[4:]
    tmp = ''
    answer = ''
    #split
    if s[0] == 'S':
        if s[2] == 'M':
            word = word[:-2]
        for i in range(len(word)):
            if word[i].isupper():
                if tmp:
                    answer = answer + tmp + " " 
                    tmp = ''
            tmp += word[i].lower()
        answer += tmp
    #combine
    else:
        lst = word.split()
        answer = lst[0]
        for i in range(1, len(lst)):
            answer += lst[i][0].upper()
            answer += lst[i][1:]
        if s[2] == 'M':
            answer += '()'  
        if s[2] == 'C':
            answer = answer[0].upper() + answer[1:]
    print(answer)
profile
우리는 무엇이든 될 수 있어

0개의 댓글