-
32. 이상한 문자 만들기코딩 테스트/Level 1 2019. 10. 30. 23:06반응형
https://programmers.co.kr/learn/courses/30/lessons/12930
파이썬
평범하게..
def solution(s): answer = [] for each in s.split(' '): temp = '' for index, letter in enumerate(each): if index % 2: temp += letter.lower() else: temp += letter.upper() answer.append(temp) return ' '.join(answer)
코드 뽐내기 --;
def solution(s): return ' '.join([''.join([letter.lower() if index % 2 else letter.upper() for index, letter in enumerate(each)]) for each in s.split(' ')])
자바스크립트
function solution(s) { var result = [] s.split(' ').forEach( function(letters) { let temp = ''; for (let i in letters) temp += (i % 2) ? letters[i].toLowerCase() : letters[i].toUpperCase(); result.push(temp); } ); return result.join(' '); }
자바
class Solution { public String solution(String s) { var words = s.toCharArray(); var count = 0; for (int i = 0; i < s.length(); i++) { count = (words[i] == ' ') ? 0 : ++count; words[i] = (count % 2 == 0) ? Character.toLowerCase(words[i]) : Character.toUpperCase(words[i]); } return String.valueOf(words); } }
코틀린
class Solution { fun solution(s: String): String { val words = s.toCharArray() var count = 0 for (i in s.indices) { count = if (words[i] == ' ') 0 else ++count words[i] = if (count % 2 == 0) Character.toLowerCase(words[i]) else Character.toUpperCase(words[i]) } return String(words) } }
고
import "unicode" func solution(s string) string { words := []rune(s) count := 0 for i, letter := range words { if letter == ' ' { count = 0 } else { count++ } if count%2 == 0 { words[i] = unicode.ToLower(words[i]) } else { words[i] = unicode.ToUpper(words[i]) } } return string(words) }
C#
public class Solution { public string solution(string s) { int count = 0; string answer = ""; for (int i = 0; i < s.Length; i++) { answer += (count % 2 == 0) ? char.ToUpper(s[i]) : char.ToLower(s[i]); count = (s[i] == ' ') ? 0 : ++count; } return answer; } }
반응형