secretgspot
10/7/2019 - 3:18 AM

ARRAYS

/*
.__..__ .__ .__..   , __.
[__][__)[__)[__] \./ (__ 
|  ||  \|  \|  |  |  .__)
*/

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 2, 3, 4, 5, 7, 7];


/* ▀▀▀ // Shuffle array */
function shuffle(array = []) {
	let m = array.length;
	// While there remain elements to shuffle…
	while (m > 0) {
		// Pick a remaining element…
		const i = Math.floor(Math.random() * m--);
		// And swap it with the current element.
		const t = array[m];
		array[m] = array[i];
		array[i] = t;
	}
	return array;
}
shuffle(arr);


/* ▀▀▀ // Pick random from array */
function pickRandom(array = []) {
	const i = ~~(Math.random() * array.length);
	return array[i];
}
pickRandom(arr);

/* ▀▀▀ // ZIPPED ARRAY */
var a = [1, 2, 3]
var b = ['a', 'b', 'c']
var zipped = b.map((e, i) => [e, a[i]]);


/* ▀▀▀ // REMOVE ELEMENT FROM ARRAY */
function arrayRemove(arr, value) {
  return arr.filter(ele => ele != value);
}
function isEven(number) {
  return number % 2 === 0;
}
const filteredEven = arr.filter(isEven);
const filteredArray = arrayRemove(arr, 5);
const filteredArr = arr.splice(2,2);


/* ▀▀▀ // Head and Tail of ARRAY */
const numbers = [1,2,3,4,5,6,7,8,9,0];
const head = ([x]) => x;
head(numbers)
const tail = ([,...xs]) => xs;
tail(numbers)


/* ▀▀▀ // Shuffle prototype for Array */
Array.prototype.shuffle = function() {
  var i = this.length, j, temp;
  if (i==0) return this;
  while (--i) {
    j = Math.floor( Math.random() * (i+1));
    temp = this[i];
    this[i] = this[j];
    this[j] = temp;
  }
  return this;
}


/* ▀▀▀ // checks if array. */
function isArray(v) {
	return Array.isArray(v);
}
isArray(arr); // true


/* ▀▀▀ // Easily remove duplicates from array */
Array.from(new Set(arr));


/* ▀▀▀ // Pick-out the largest even number from an array of integers */
function maximalEven(inputArray) {
    var max = -9999999;
    for(var i=0; i<inputArray.length; i++){
        if(inputArray[i] % 2 === 0 && inputArray[i] > max) max = inputArray[i];
    }
    return max;
}
maximalEven(arr); // 8


/* ▀▀▀ // Array packing of bits */
function arrayPacking(a) {
    var res = [];
    for(var i=0; i<a.length; i++)
        res[i] = ('00000000'+a[i].toString(2)).substr(-8);
    return parseInt(res.reverse().join(''), 2);
}
arrayPacking(arr); // 9.341043778395696e+36


/* ▀▀▀ // Boy walking counting house numbers */
function houseNumbersSum(inputArray) {
    var i=0, sum=0;

    while(inputArray[i] !== 0){
        sum += inputArray[i];
        i++;
    }

    return sum;
}
houseNumbersSum(arr); // 45


/* ▀▀▀ // Given an array of integers, find the maximal absolute difference between any two of its adjacent elements. */
function arrayMaximalAdjacentDifference(inputArray) {
    var max = 0;
    for(var i=0; i<inputArray.length-1; i++){
        var dif = Math.abs(inputArray[i+1] - inputArray[i]);
        if(dif > max) max = dif;
    }

    return max;
}