Created
April 17, 2017 16:38
-
-
Save pavlelekic/f83b22bd97d606fbec6b87bc52fb20e2 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/fimifuc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
function findSolution(xs, k) { | |
var length = xs.length; | |
k = k % length; | |
if (k === 0) { | |
return xs; | |
} | |
else { | |
var valueFromPreviousIteration = xs[0], tmp, index; | |
for (var i = 1; i <= length; i++) { | |
index = (i * k) % length; | |
tmp = xs[index]; | |
xs[index] = valueFromPreviousIteration; | |
valueFromPreviousIteration = tmp; | |
} | |
return xs; | |
} | |
} | |
var A = [3, 8, 9, 7, 6]; | |
console.log(findSolution(A, 3)); // should output [9, 7, 6, 3, 8] | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript"> | |
function findSolution(xs, k) { | |
var length = xs.length; | |
k = k % length; | |
if (k === 0) { | |
return xs; | |
} | |
else { | |
var valueFromPreviousIteration = xs[0], tmp, index; | |
for (var i = 1; i <= length; i++) { | |
index = (i * k) % length; | |
tmp = xs[index]; | |
xs[index] = valueFromPreviousIteration; | |
valueFromPreviousIteration = tmp; | |
} | |
return xs; | |
} | |
} | |
var A = [3, 8, 9, 7, 6]; | |
console.log(findSolution(A, 3)); // should output [9, 7, 6, 3, 8]</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findSolution(xs, k) { | |
var length = xs.length; | |
k = k % length; | |
if (k === 0) { | |
return xs; | |
} | |
else { | |
var valueFromPreviousIteration = xs[0], tmp, index; | |
for (var i = 1; i <= length; i++) { | |
index = (i * k) % length; | |
tmp = xs[index]; | |
xs[index] = valueFromPreviousIteration; | |
valueFromPreviousIteration = tmp; | |
} | |
return xs; | |
} | |
} | |
var A = [3, 8, 9, 7, 6]; | |
console.log(findSolution(A, 3)); // should output [9, 7, 6, 3, 8] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solution to CyclicRotation problem from codility.com.