Reputation: 13853
Need a function which return next N elements from given array, with given offset, but when offset larger then array length, it must return elements at the beginning of array.
Interface:
slice2(array, chunk, offset);
Examples:
var array = [1,2,3,4,5];
slice2(array,2,2)
Output: [3,4]
slice2(array,2,4)
Output: [5,1]
slice2(array,3,4)
Output: [5,1,2]
Upvotes: 1
Views: 3312
Reputation: 71
function sleepPromise(durationInMs) {
return new Promise((resolve) => {
setTimeout(resolve, durationInMs);
})
}
function getNextChunk(array, chunkSize, indexDoneUpto) {
let nextChunk = []
let arrayLength = array.length
if (chunkSize > arrayLength) chunkSize = arrayLength
for (let i = 0; i < chunkSize; i++) {
nextChunk.push(array[indexDoneUpto])
indexDoneUpto++
if (indexDoneUpto > (arrayLength - 1)) {
indexDoneUpto = 0
}
}
return { nextChunk, indexDoneUpto }
}
let array1 = [1, 2, 3, 4, 5, 6, 7, 8]
let chunkSize = 3
let indexDoneUpto = 0
while (true) {
let result = getNextChunk(array1, chunkSize, indexDoneUpto)
indexDoneUpto = result.indexDoneUpto
console.log(result.nextChunk)
await sleepPromise(500)
}
will output:
[ 1, 2, 3 ]
[ 4, 5, 6 ]
[ 7, 8, 1 ]
[ 2, 3, 4 ]
[ 5, 6, 7 ]
[ 8, 1, 2 ]
[ 3, 4, 5 ]
[ 6, 7, 8 ]
[ 1, 2, 3 ]
[ 4, 5, 6 ]
[ 7, 8, 1 ]
...
Upvotes: 0
Reputation: 1591
Here is a snippet of what I did. Credits to @Andrei I just modified his code. So what I wanted was a function to get the selected number in an array and x numbers before and x numbers after that number. Eg, [1,2,3,4,5,6,7,8] if I select 2 as my offset and chunk as 3, output would be: [ 8, 1 , 2, 3, 4, 5, 6 ] If offset = 3, chunk = 3, op: [ 1, 2, 3, 4, 5, 6, 7]
function cyclicSlice(array, chunk, offset) {
var subarray = [];
for (var i = array.length; i>=(array.length-chunk); i--) {
var ind = (offset + i) % array.length;
subarray.push(array[ind]);
}
subarray = subarray.reverse();
const newOffset = offset + 1; // To avoid the repititon of the selected number
for (var i = 0; i<chunk; i++) {
var ind = ( newOffset + i) % array.length;
subarray.push(array[ind]);
}
return subarray;
}
Upvotes: 0
Reputation: 5822
Sorry couldn't help myself :)
The following function handles a few more scenarios and does this with only 2 slices.
// standard
slice2( [1,2,3,4,5], 3, 3 ); // [1, 4, 5]
// specify chunk as a negative number
slice2( [1,2,3,4,5], -3, 3 ); // [1, 2, 3]
// what if chunk is larger than array
slice2( [1,2,3,4,5], -7, 3 ); // [1, 2, 3, 4, 5]
// what if chunk is 0
slice2( [1,2,3,4,5], 0, 3 ); // []
function slice2( array, chunk, offset ) {
var start1, len = array.length, end1, start2, end2, result;
if( !chunk || !array ) return []; // if chunk or array resolve to falsy value.
if( Math.abs(chunk) >= len ) return array;
if( chunk < 0 ) {
end1 = offset;
leftover = offset + chunk;
start1 = leftover < 0 ? 0 : leftover;
start2 = leftover;
end2 = len;
} else {
start1 = offset;
leftover = ( offset + chunk ) - len;
end1 = leftover > 0 ? ( offset + chunk ) : len;
start2 = 0;
end2 = leftover;
}
result = array.slice( start1, end1 );
if( leftover ) result = array.slice( start2, end2 ).concat( result );
return result;
}
Fiddle here
Upvotes: 0
Reputation: 5
private static List sliceArray(List mainarray, int chunk, int offset) {
int size = mainarray.size();
List<Integer> resultArray = new ArrayList<Integer>();
if(!mainarray.contains(chunk)){
return null;
}
int index = mainarray.indexOf(chunk);
int doOffset = size - index;
if(doOffset > offset){
for(int element = 1 ; element <= offset; element++ ){
resultArray.add(mainarray.get(index+1));
index++;
}
}else if(doOffset == 0){
for(int element = 0 ; element <= offset; element++ ){
element = mainarray.get(element);
resultArray.add(element);
}
}else{
int position =0;
for(int element = index ; element <= offset; element++ ){
int value = 0;
if(element < size-1){
value = mainarray.get(element+1);
}
else
{
value = mainarray.get(position);
position ++;
}
resultArray.add(value);
}
}
return resultArray;
// TODO Auto-generated method stub
}
Upvotes: -1
Reputation: 72967
Try this:
function slice2(array, chunk, offset){
var end = offset + chunk,
out = array.slice(offset, end); // Get the chunk
if(array.length < end){ // If the chunk should wrap
out = out.concat(array.slice(0, end - array.length)); // Concatenate a the rest of the chunk, from the start of the array, to the output.
}
return out;
}
It doesn't manually loop through the array, and uses a bare minimal of calculations.
Upvotes: 0
Reputation: 56726
function slice2(array, chunk, offset) {
var subarray = [];
for (var i = 0; i<chunk; i++) {
var ind = (offset + i) % array.length;
subarray.push(array[ind]);
}
return subarray;
}
Upvotes: 9
Reputation: 956
The buillt-in .slice() method may be enough for your problem :
var slice = array.slice(1,3);
I set-up a quick jsfiddle, the interface would need change since it only allows a rance
Upvotes: 0