【Codility Lesson2】CyclicRotation

[Codilityの勧め ~JavaScriptで解くアルゴリズム~](https://qiita.com/kyohei_ai/items/f4601cc6b55463565392)の実践編です。

## 問題

An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).

The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.

Write a function:

class Solution { public int[] solution(int[] A, int K); }

that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.

For example, given

    A = [3, 8, 9, 7, 6]
    K = 3
the function should return [9, 7, 6, 3, 8]. Three rotations were made:

    [3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7]
    [6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9]
    [7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8]
For another example, given

    A = [0, 0, 0]
    K = 1
the function should return [0, 0, 0]

Given

    A = [1, 2, 3, 4]
    K = 4
the function should return [1, 2, 3, 4]

Assume that:

N and K are integers within the range [0..100];
each element of array A is an integer within the range [−1,000..1,000].
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

### 語彙メモ

- `integer` 整数
- `rotate array A K times` arrayAをK回回転させる
- `Write a function:` (:以下の)関数を書け
- `Assume that:` (that:以下を)仮定しなさい

## 解法

### 1. pop()とunshift()を使用する

pop()メソッドは配列から最後の要素を取り除き、その取り除いた要素を返します。
unshift()メソッドは配列の最初に一つ以上の要素を追加し、新しい配列の長さを返り値として返します。
また、pop(), unshift()共に**破壊的なメソッド（＝元の配列を直接変化させる）**であることに留意してください。

配列A、回転数Kを引数とした関数を作ります。
変数iに初期値として0を代入します。
A.pop()で配列Aの最後の要素を取り出し、A.unshift(A.pop())で配列Aの最後の要素を最初の要素として追加した新しい配列Aを作ります。これをwhile文でK回ループさせます。


``` JavaScript
function solution(A, K) {
    // write your code in JavaScript (Node.js 8.9.4)
    let i = 0;
    while(i < K) {
        A.unshift(A.pop())
        i++
    }
    
    return A
}
```

for文を使用する際はこちら。

``` JavaScript
function solution(A, K) {
    // write your code in JavaScript (Node.js 8.9.4)
    for(let i = 0; i < K; i++) {
        A.unshift(A.pop())
    }
    
    return A
}

```

### 2.pop()の代わりにslice()を使用する

``` JavaScript
function solution(A, K) {
  if (A.length >= K) {
    A.unshift(...A.splice(-K));
  } else {
    let i = 0;
    while (i < K) {
      A.shift(A.splice(-1));
      i++;
    }
  }
  return A;
}

// line 4: To add the elements of that array to the start of nums (instead of the array itself) we use the spread operator: ...nums.splice(-K)
```

## 参考

https://medium.com/javascript-in-plain-english/algorithms-101-rotate-array-in-javascript-three-solutions-260fbc923b64

<a href="//af.moshimo.com/af/c/click?a_id=1875799&amp;p_id=170&amp;pc_id=185&amp;pl_id=4062&amp;url=https%3A%2F%2Fwww.amazon.co.jp%2Fdp%2FB07179Q3MJ" rel="nofollow"><img src="https://images-fe.ssl-images-amazon.com/images/I/51ElwXwvJ-L._SL75_.jpg" alt="" style="border: none;" /><br />アルゴリズム図鑑 絵で見てわかる26のアルゴリズム</a><img src="//i.moshimo.com/af/i/impression?a_id=1875799&amp;p_id=170&amp;pc_id=185&amp;pl_id=4062" alt="" width="1" height="1" style="border: 0px;" />

<a href="//af.moshimo.com/af/c/click?a_id=1875799&amp;p_id=170&amp;pc_id=185&amp;pl_id=4062&amp;url=https%3A%2F%2Fwww.amazon.co.jp%2Fdp%2FB00FR78X64" rel="nofollow"><img src="https://images-fe.ssl-images-amazon.com/images/I/51bZA91ZWqL._SL75_.jpg" alt="" style="border: none;" /><br />世界でもっとも強力な9のアルゴリズム</a><img src="//i.moshimo.com/af/i/impression?a_id=1875799&amp;p_id=170&amp;pc_id=185&amp;pl_id=4062" alt="" width="1" height="1" style="border: 0px;" />

<a href="//af.moshimo.com/af/c/click?a_id=1875799&amp;p_id=170&amp;pc_id=185&amp;pl_id=4062&amp;url=https%3A%2F%2Fwww.amazon.co.jp%2Fdp%2FB01N14WBX3" rel="nofollow"><img src="https://images-fe.ssl-images-amazon.com/images/I/61bXMHbyv3L._SL75_.jpg" alt="" style="border: none;" /><br />なっとく！アルゴリズム</a><img src="//i.moshimo.com/af/i/impression?a_id=1875799&amp;p_id=170&amp;pc_id=185&amp;pl_id=4062" alt="" width="1" height="1" style="border: 0px;" />

<a href="//af.moshimo.com/af/c/click?a_id=1875799&amp;p_id=170&amp;pc_id=185&amp;pl_id=4062&amp;url=https%3A%2F%2Fwww.amazon.co.jp%2Fdp%2F4873117836" rel="nofollow"><img src="https://images-fe.ssl-images-amazon.com/images/I/51U44SJi3jL._SL75_.jpg" alt="" style="border: none;" /><br />初めてのJavaScript 第3版 ―ES2015以降の最新ウェブ開発</a><img src="//i.moshimo.com/af/i/impression?a_id=1875799&amp;p_id=170&amp;pc_id=185&amp;pl_id=4062" alt="" width="1" height="1" style="border: 0px;" />

<a href="//af.moshimo.com/af/c/click?a_id=1875799&amp;p_id=170&amp;pc_id=185&amp;pl_id=4062&amp;url=https%3A%2F%2Fwww.amazon.co.jp%2Fdp%2FB01MZ9MGRV" rel="nofollow"><img src="https://images-fe.ssl-images-amazon.com/images/I/41i2cQpdbPL._SL75_.jpg" alt="" style="border: none;" /><br />徹底例解ロイヤル英文法　改訂新版</a><img src="//i.moshimo.com/af/i/impression?a_id=1875799&amp;p_id=170&amp;pc_id=185&amp;pl_id=4062" alt="" width="1" height="1" style="border: 0px;" />