Skip to content

Instantly share code, notes, and snippets.

@pavlelekic
Created April 17, 2017 16:38
Show Gist options
  • Save pavlelekic/f83b22bd97d606fbec6b87bc52fb20e2 to your computer and use it in GitHub Desktop.
Save pavlelekic/f83b22bd97d606fbec6b87bc52fb20e2 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/fimifuc
<!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>
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]
@pavlelekic
Copy link
Author

Solution to CyclicRotation problem from codility.com.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment