Skip to content

Instantly share code, notes, and snippets.

@ukoasis
Created August 15, 2018 02:50
Show Gist options
  • Save ukoasis/e7a9f18e5b28af77dda65977c1437bc2 to your computer and use it in GitHub Desktop.
Save ukoasis/e7a9f18e5b28af77dda65977c1437bc2 to your computer and use it in GitHub Desktop.
2次元配列を左に90°回転させるサンプル
const assert = require('assert');
const arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
const answer = [[3, 6, 9], [2, 5, 8], [1, 4, 7]];
const wrongAnswer = [[4, 6, 9], [2, 5, 8], [1, 4, 7]];
const rotate = b => {
const transpose = a => a[0].map((_, c) => a.map(r => r[c]));
return transpose(b).reverse();
};
assert.deepStrictEqual(answer, rotate(arr), '正しく処理されていない');
assert.notDeepStrictEqual(wrongAnswer, rotate(arr), '正しく処理されていない');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment