Algorithms
Logic
Arrays
Data Types
Mathematics
Numbers
Statistics
Data
function mad(numbers) { if (!numbers.length) return null; const mean = numbers.reduce((mean, number) => mean + number) / numbers.length; return ( numbers.reduce((mad, number) => mad + Math.abs(number - mean), 0) / numbers.length ); }
function mad(a) {if (!a.length) return null;let n = a.reduce((x, y) => x + y, 0) / a.length;return a.reduce((x, y) => x + Math.abs(y - n), 0) / a.length;}- function mad(numbers) {
- if (!numbers.length) return null;
- const mean = numbers.reduce((mean, number) => mean + number) / numbers.length;
- return (
- numbers.reduce((mad, number) => mad + Math.abs(number - mean), 0) /
- numbers.length
- );
- }
const {assert} = require("chai"); describe("Solution", function() { it("Simple and Edge Cases", function() { assert.equal(mad([1,1,4,6]), 2); assert.equal(mad([2,2,4,4]), 1); }); it("Variable Length Arrays", function() { assert.equal(mad([4,6]), 1); assert.equal(mad([2,2,2,4,4,4]), 1); assert.equal(mad([]), null); assert.equal(mad([5223]),0); }); }); function solution(array) { if(array.length === 0){return null} if(array.length === 1){return 0} let average = (array.reduce((acc, cV) => acc + cV))/array.length let array2 = array.map((item,index,arr)=> Math.abs(item-average)) return (array2.reduce((acc, cV) => acc + cV))/array2.length; } const randint = require("lodash/random"); describe("Random tests", function() { it("Random Test", function() { for (let i = 0; i < 100; i++) { let arr = Array.from({length: randint(0, 50)}, _ => randint(0, 99)); let expected = solution(arr); assert.equal(mad(arr), expected, 1e-9); } }); });
const {assert} = require("chai");describe("Solution", function() {it("Simple and Edge Cases", function() {assert.equal(mad([1,1,4,6]), 2);assert.equal(mad([2,2,4,4]), 1);});it("Variable Length Arrays", function() {assert.equal(mad([4,6]), 1);assert.equal(mad([2,2,2,4,4,4]), 1);assert.equal(mad([]), null);assert.equal(mad([5223]),0);});});function solution(array) {if(array.length === 0){return null}if(array.length === 1){return 0}let average = (array.reduce((acc, cV) => acc + cV))/array.lengthlet array2 = array.map((item,index,arr)=> Math.abs(item-average))return (array2.reduce((acc, cV) => acc + cV))/array2.length;}const randint = require("lodash/random");describe("Random tests", function() {it("Random Test", function() {for (let i = 0; i < 100; i++) {let arr = Array.from({length: randint(0, 50)}, _ => randint(0, 99));let expected = solution(arr);assert.equal(mad(arr), expected, 1e-9);}});- const {assert} = require("chai");
- describe("Solution", function() {
- it("Simple and Edge Cases", function() {
- assert.equal(mad([1,1,4,6]), 2);
- assert.equal(mad([2,2,4,4]), 1);
- });
- it("Variable Length Arrays", function() {
- assert.equal(mad([4,6]), 1);
- assert.equal(mad([2,2,2,4,4,4]), 1);
- assert.equal(mad([]), null);
- assert.equal(mad([5223]),0);
- });
- });
- function solution(array) {
- if(array.length === 0){return null}
- if(array.length === 1){return 0}
- let average = (array.reduce((acc, cV) => acc + cV))/array.length
- let array2 = array.map((item,index,arr)=> Math.abs(item-average))
- return (array2.reduce((acc, cV) => acc + cV))/array2.length;
- }
- const randint = require("lodash/random");
- describe("Random tests", function() {
- it("Random Test", function() {
- for (let i = 0; i < 100; i++) {
- let arr = Array.from({length: randint(0, 50)}, _ => randint(0, 99));
- let expected = solution(arr);
- assert.equal(mad(arr), expected, 1e-9);
- }
- });
- });
Given an array of numbers, move all the zeros to the end of the array without changing the order of the non-zero numbers in the array.
E.g.
[1,0,2,0,3,0,4,5] --> [1,2,3,4,5,0,0,0]
const moveZeros = (arr) => [...arr.filter(n => n), ...arr.filter(n => !n)];
var moveZeros = function (arr) {return arr.filter(el => el !== 0).concat(arr.filter(el => el === 0));}- const moveZeros = (arr) => [...arr.filter(n => n), ...arr.filter(n => !n)];
describe("moveZeros", function() { it("should move zeros to the end of an array", function() { Test.assertDeepEquals(moveZeros([1,2,3,4,5,0,0,0]), [1,2,3,4,5,0,0,0]); Test.assertDeepEquals(moveZeros([0,0,0,1,2,3,4,5]), [1,2,3,4,5,0,0,0]); Test.assertDeepEquals(moveZeros([1,0,2,0,3,0,4,5]), [1,2,3,4,5,0,0,0]); Test.assertDeepEquals(moveZeros([1,2,3,4,5]), [1,2,3,4,5]); }); });
// Don't need them- describe("moveZeros", function() {
- it("should move zeros to the end of an array", function() {
- Test.assertDeepEquals(moveZeros([1,2,3,4,5,0,0,0]), [1,2,3,4,5,0,0,0]);
- Test.assertDeepEquals(moveZeros([0,0,0,1,2,3,4,5]), [1,2,3,4,5,0,0,0]);
- Test.assertDeepEquals(moveZeros([1,0,2,0,3,0,4,5]), [1,2,3,4,5,0,0,0]);
- Test.assertDeepEquals(moveZeros([1,2,3,4,5]), [1,2,3,4,5]);
- });
- });