Shifting Rows And Columns In 2d Arrays - Javascript
Solution 1:
The previous answers looks ok, but lacks one major thing when dealing with array indexes ; validation checks.
You do not want to try to access non-existent array indexes. Therefore, I created a small class to shift your array as needed, with validation. This will throw an Error
if either the row or column index is invalid.
classArrayShifter {
staticshowArray(array) {
// console.log("Array : ", array);console.log('------');
for (const [index, elem] of array.entries()) {
console.log(''+elem);
}
}
staticvalidateRowIndex(array, rowIndex) {
if (!isArray(array) || !isInt(rowIndex) || rowIndex <= 0 || rowIndex > array.length) {
thrownewError('The row index is wrong');
}
}
staticvalidateColumnIndex(array, columnIndex) {
if (!isArray(array) || !isInt(columnIndex) || columnIndex <= 0 || columnIndex > array[0].length) {
thrownewError('The column index is wrong');
}
}
staticshiftRow(array, rowIndex) {
ArrayShifter.validateRowIndex(array, rowIndex);
array[rowIndex - 1].unshift(array[rowIndex - 1].pop());
return array;
}
staticshiftColumn(array, columnIndex) {
ArrayShifter.validateColumnIndex(array, columnIndex);
let prev = array[array.length - 1][columnIndex - 1];
for (const elem of array) {
let tmp = elem[columnIndex - 1];
elem[columnIndex - 1] = prev;
prev = tmp;
}
return array;
}
}
let sourceArray1 = [
[1,2,3],
[4,5,6],
[7,8,9],
];
let sourceArray2 = [
[1,2,3],
[4,5,6],
[7,8,9],
];
let controlArrayShiftRow = [
[3,1,2],
[4,5,6],
[7,8,9],
];
let controlArrayColumnRow = [
[7,2,3],
[1,5,6],
[4,8,9],
];
// arrayShifter.showArray(sourceArray1);console.log(`Shift row test is ${areArraysEqual(controlArrayShiftRow, ArrayShifter.shiftRow(sourceArray1, 1))}.`);
// arrayShifter.showArray(sourceArray2);console.log(`Shift column test is ${areArraysEqual(controlArrayColumnRow, ArrayShifter.shiftColumn(sourceArray2, 1))}.`);
//-------------------- Unimportant js functions --------------------functionisArray(arr) {
if (Object.prototype.toString.call([]) === '[object Array]') { //Make sure an array has a class attribute of [object Array]//Test passed, now check if is an ArrayreturnArray.isArray(arr) || (typeof arr === 'object' && Object.prototype.toString.call(arr) === '[object Array]');
}
else {
thrownewException('toString message changed for Object Array'); //Make sure the 'toString' output won't change in the futur (cf. http://stackoverflow.com/a/8365215)
}
}
functionisInt(n) {
returntypeof n === 'number' && parseFloat(n) === parseInt(n, 10) && !isNaN(n);
}
functionareArraysEqual(a1, a2) {
returnJSON.stringify(a1) == JSON.stringify(a2);
}
The working code can be seen in this codepen.
Solution 2:
For row shift you can use Array#unshift
and Array#pop
methods. And for shifting the column use a Array#forEach
method with a temp variable.
var array = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
],
array1 = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
functionshiftRow(arr, row) {
arr[row - 1].unshift(arr[row - 1].pop());
return arr;
}
functionshiftCol(arr, col) {
var prev = arr[arr.length - 1][col-1];
arr.forEach(function(v) {
var t = v[col - 1];
v[col - 1] = prev;
prev = t;
})
return arr;
}
console.log(shiftRow(array, 1))
console.log(shiftCol(array1, 1))
Solution 3:
First, you have to pass two arguments: the array, and the row/column you want to shift. And remember that arrays are zero-based, not 1. So in your example, if you want to shit the first row, you need to pass 0, not 1.
Second, since you want to put the last element at the front, and push others down, you need to loop, for shiftRow
, from back to front. Here's a solution. Feel free to improve on it.
functionshiftRow(arr, row) {
var temp = arr[row];
var j=temp.length-1;
var x=temp[j];
for(var i = j; i > 0; i--) {
temp[i]=temp[i-1];
}
temp[0]=x;
arr[row]=temp;
}
As you can see it works only on the row you want to shift, and starts from the end, working its way to the front. Before the loop I save the last element (which will be overwritten) and put that in the first slot at the end of the loop.
Solution 4:
Given this question :
Transposing a javascript array efficiently
It is sufficient to implement only shiftRow
and transpose before and after it if we want to achieve shiftCol
functionshiftRow(array,n)
{
let retVal=[[]];
for(i=0;i<array.length;i++)
{
if (i==n)
retVal[i]= array[i].slice(1,array.length).concat(array[i][0]);
else
retVal[i]=array[i];
}
return retVal;
}
Post a Comment for "Shifting Rows And Columns In 2d Arrays - Javascript"