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