문제: Correct variable names consist only of English letters, digits and underscores and they can't start with a digit.
Check if the given string is a correct variable name.
Example
- For name = "var_1__Int", the output should be
variableName(name) = true; - For name = "qq-q", the output should be
variableName(name) = false; - For name = "2w2", the output should be
variableName(name) = false.
-문제 해결 정리-
1. name 안에 있는 each character 들에 대하여 for loop을 돌기전에 먼저 name 안에 제일 처음 단어가 숫자인지 확인해준다. 만약 숫자일 경우 바로 False를 리턴.
2. 다음으로는 name 안의 each character들을 돌며 하나씩 확인해 주는데, 먼저 char 가 '_' (underscore) 일 경우에는 continue를 써서 바로 다음 캐릭터 확인을 해 주도록 한다.
3. underscore인지 아닌지를 check 한 다음에는, char 이 알파벳인지 또는 숫자인지 확인하여 둘다 아닐 경우에는, False를 리턴
4. loop이 다 돌고, False를 리턴 하지 않았을 경우에는, 무조건 True 리턴.
다 풀고 난 후에 답을 보니, 파이썬에는 .isidentifier() 이라는 함수가 있어 한줄로도 문제를 풀 수 있는 방법을 볼 수 있었다. 파이썬의 라이브러리는 봐도 봐도 놀랍다.
모든 문제에 대한 저작권은 CodeSignal 회사에 있습니다. [Copyright © 2020 BrainFights Inc. All rights reserved]
'CodeSignal > Arcade' 카테고리의 다른 글
CodeSignal [30/60] circleOfNumbers (0) | 2020.05.21 |
---|---|
CodeSignal [29/60] chessBoardCellColor (0) | 2020.05.20 |
CodeSignal [28/60] alphabeticShift (0) | 2020.05.19 |
CodeSignal [26/60] evenDigitsOnly (0) | 2020.05.17 |
CodeSignal [25/60] Array Replace (0) | 2020.05.16 |
댓글