Algorithm/codewars

Directions Reduction 답안

Bonita SY 2021. 3. 30. 19:33
728x90
반응형

문제)

www.codewars.com/kata/550f22f4d758534c1100025a/train/javascript

 

Codewars: Achieve mastery through challenge

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

www.codewars.com

 

내가 푼 답)

function dirReduc(arr){
  let arrStr = Array.from(arr).map(elem => elem.toUpperCase()).join('_');
  const direcRegex = /(NORTH_SOUTH)|(SOUTH_NORTH)|(EAST_WEST)|(WEST_EAST)/g;
  const unbar2Regex = /__/g
  
  while(arrStr.match(direcRegex) !== null) {
    arrStr = arrStr.replace(direcRegex, '');
    
    while(arrStr.match(unbar2Regex) !== null) {
      arrStr = arrStr.replace(unbar2Regex, '_');
    }
  }
  
  return arrStr.split('_').filter(e => e);
}

 

테스트 결과)

 

마음에 드는 남이 푼 답)

728x90
반응형

'Algorithm > codewars' 카테고리의 다른 글

Catching Car Mileage Numbers 답안  (0) 2021.04.01
Adding Big Numbers 답안  (0) 2021.03.30
Sum by Factors 답안  (0) 2021.03.30
Number of trailing zeros of N! 답안  (0) 2021.03.29
Permutations 답안  (0) 2021.03.29