Stop gninnipS My sdroW! ( 6 kyu )

문제

Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.

For example:
spinWords( "Hey fellow warriors" ) => returns “Hey wollef sroirraw”
spinWords( "This is a test") => returns “This is a test”
spinWords( "This is another test" )=> returns “This is rehtona test”

단어가 5글자 이상일 때 단어를 거꾸로 배치하는 문제

풀이

function spinWords(str){
  let arr = str.split(' ');
  for ( let i = 0; i < arr.length; i++ ) {
    if ( arr[i].length >= 5 ) {
      arr[i] = arr[i].split('').reverse().join('');
    }
  }
  return arr.join(' ');
}

단어 별로 쪼개서 배열에 넣고 길이가 5이상이면 뒤집어주고 join해서 리턴.

매우 간단한 문제고 이게 왜 6 kyu인지 모르겟다.

다른 사람의 풀이

function spinWords(string){
  return string.replace(/\w{5,}/g, function(w) { return w.split('').reverse().join('') })
}

정규식 멋있게 써서 가져왔고, 익혀놔야겠다.

'자료구조, 알고리즘 > Codewars 문제' 카테고리의 다른 글

Take a Ten Minute Walk ( 6 kyu )  (0) 2017.11.24
IQ Test ( 6 kyu )  (0) 2017.11.24
Duplicate Encoder ( 6 kyu )  (0) 2017.11.23
Find The Parity Outlier ( 6 kyu )  (0) 2017.11.22
Persistent Bugger. ( 6 kyu )  (0) 2017.11.22

+ Recent posts