Skip to content

Instantly share code, notes, and snippets.

@QuadFlask
Created March 25, 2016 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuadFlask/73370ba62f6db2deb90b to your computer and use it in GitHub Desktop.
Save QuadFlask/73370ba62f6db2deb90b to your computer and use it in GitHub Desktop.
zigzag problem

(영상)[https://www.youtube.com/watch?v=W23s6kYJbrA]

크롬 콘솔에서만 해보느라(귀찮아서) tdd를 못했넹....

하스켈 좀 공부해서 해보자. recursive, map, filter 가 주 로직이니 쉽게(는 아니고) 가능할거 같다

문제

0   4   8
1 3 5 7 9
2   6   10

이런식으로 지그제그 형태로 텍스트를 만든뒤, 각 줄을 모아서 048135792610 과 같은 문자열을 만든다 (문제의 샘플은 PAYPALISHIRING)

// 처음에 가로(컬럼) 갯수를 구해야 할것 같아서 해봄(무쓸모ㅋ)
/* function cols(i) {
   if (i <= 3) return ~~(i / 3);
   if (i > 3)  return 2 + cols(i - 4);
} */ 
function zigzag(text) {
  var ta = text.split('');
  return  ta.filter((c,i)=> i%4 == 0)
  .concat(ta.filter((c,i)=> i%2 == 1))
  .concat(ta.filter((c,i)=> (i+2)%4 == 0))
  .join('');
}
console.log(zigzag('PAYPALISHIRING')); // prints 'PAHNAPLSIIGYIR'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment