Juni_DEV

[Python, HackerRank] Camel Case 4 본문

Coding Interview

[Python, HackerRank] Camel Case 4

junni :p 2023. 4. 19. 18:22
반응형
 

Camel Case 4 | HackerRank

 

www.hackerrank.com


주어진 문제

더보기

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).

 

처음에 코드를 제출했을 때 Ouput과 Expected Output이 동일한데도 Wrong Answer라는 결과가 나옴

=> 공백제거를 해줘야했음 !!!

import sys

# 원래 코드에는 이 부분만 있었는데 
input_list = sys.stdin.readlines() 

# 새로 배열을 선언해서 각 데이터별로 공백 제거해줌
str_list = []
for strs in input_list:
    str_list.append(strs.rstrip())

 

Split인 경우
- 첫 글자를 대문자로 변경
- 대문자가 나오면 띄어쓰기하고 소문자로 변경
1. M : 맨 뒤 "()" 삭제
2. V : -
3. C : -
=> M인 경우만 따로 처리

Combine인 경우
- 띄어쓰기가 있으면 띄어쓰기를 없애고 다음 문자를 대문자로 변경
1. M : 맨 뒤 "()" 추가
2. V : -
3. C : 첫 글자를 대문자로 변경


풀이

# Enter your code here. Read input from STDIN. Print output to STDOUT
import sys

input_list = sys.stdin.readlines()

str_list = []
for strs in input_list:
    str_list.append(strs.rstrip())

for str_input in str_list :
    wtd = str_input[0] # what to do S/C
    s_type = str_input[2] #str type M/C/V
    title = str_input[4:]
    answer = ""
    
    if wtd == "S":
        # Split
        answer += title[0].lower()
        for i in title[1:] :
            if i.isupper():
                answer += " "+i.lower()
            else :
                answer += i
        if "()" in answer :
            answer = answer[:-2]
                
    elif wtd == "C":
        # Combine
        prev = False
        for i in title:
            if i == " " :
                prev = True
                pass
            elif prev :
                prev = False
                answer += i.upper()
            else :
                answer += i
            
        if s_type == "M" : 
            answer += "()"
        elif s_type == "C" :
            answer = answer[0].upper()+answer[1:]
                

    sys.stdout.write(answer+"\n")

 

 

GitHub - SVB-algorithm-study/JuniPark: SVB

SVB. Contribute to SVB-algorithm-study/JuniPark development by creating an account on GitHub.

github.com

 

참고:
https://codechacha.com/ko/python-string-strip/

반응형
Comments