Skip to content

Musaddango #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 226 additions & 0 deletions Solutions/musaddango_solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
const identity = (n) => n;

const addb = (a, b) => a + b;

const subb = (a, b) => a > b ? a - b : b - a;

const mulb = (a, b) => a * b;

const minb = (a, b) => a < b ? a : b;

const maxb = (a, b) => a > b ? a : b;

function add(...nums) {
const array = [...nums];
let sum = 0;
for (let i = 0; i < array.length; i++) {
sum += array[i]
}
return sum;
};


function sub(...args) {

let result = args[i];
for (let i = 0; i < args.length; i++) {
result -= args[i];
};
}


function mul(...nums) {
const array = [...nums];
let mult = 1;
for (let i = 0; i < array.length; i++) {
mult *= array[i];
}
return mult;
}

function min(...nums) {
const array = [...nums];
let smallest = array[0];
for (let i = 0; i < array.length; i++) {
if (smallest > array[i]) {
smallest = array[i];
}
}
return smallest;
}

function max(...nums) {
const array = [...nums];
let maxNum = array[0];
for (let i = 0; i < array.length; i++) {
if (array[i] > maxNum) {
maxNum = array[i];
}
}
return maxNum;
}

function addRecurse(...nums) {
if (nums.length === 0) return 0;

let newArray = nums.slice(1)
return nums[0] + addRecurse(newArray);
}

function mulRecurse(...nums) {
if (nums.length === 0) return 1;

let newArray = nums.slice(1);
return nums[0] * mulRecurse(...newArray);
}

function minRecurse(...nums) {
// Base case
if (nums.length === 1) {
return nums[0];
}
const lastSmallest = minRecurse(...nums.slice(1));
return nums[0] < lastSmallest ? nums[0] : lastSmallest;
}

function maxRecurse(...nums) {
//Base case
if (nums.length === 1) {
return [0];
}
const lastBiggest = maxRecurse(...nums.slice(1));
return nums[0] > lastBiggest ? nums[0] : lastBiggest;
}

function not(func) {
if (!(func instanceof Function) || typeof func() !== "number") {
return "Invalid argument.";
}

return func() * -1;
}

function acc(func, initial) {

return function (...args) {
return args.reduce((accum, current) => func(current, accum), initial);
}
}

function accPartial(func, start, end) {
return function (...args) {
let sub = args.slice(start, end).reduce((accum, current) => func(current, accum), 0)
const result = [...args.slice(0, start), sub, ...args.slice(end)];
return result;
}
}

function fill(num) {
return new Array(num).fill(num);
}

function fillRecurse(value, length = value) {
// Base case
if (length === 0) return [];

let result = [...fillRecurse(value, length - 1), value];

return result;
}

function set(arr) {
let result = [];
let hash = {};


for (let i = 0; i < arr.length; i++) {
if (!(arr[i] in hash)) {
hash[arr[i]] = true;
result.push(arr[i]);
}
}

return result;
}

module.exports = {
identity,
addb,
subb,
mulb,
minb,
maxb,
add,
sub,
mul,
min,
max,
addRecurse,
mulRecurse,
minRecurse,
maxRecurse,
not,
acc,
accPartial,
// accRecurse,
fill,
fillRecurse,
set,
// identityf,
// addf,
// liftf,
// pure,
// curryb,
// curry,
// inc,
// twiceUnary,
// doubl,
// square,
// twice,
// reverseb,
// reverse,
// composeuTwo,
// composeu,
// composeb,
// composeTwo,
// compose,
// limitb,
// limit,
// genFrom,
// genTo,
// genFromTo,
// elementGen,
// element,
// collect,
// filter,
// filterTail,
// concatTwo,
// concat,
// concatTail,
// gensymf,
// gensymff,
// fibonaccif,
// counter,
// revocableb,
// revocable,
// extract,
// m,
// addmTwo,
// addm,
// liftmbM,
// liftmb,
// liftm,
// exp,
// expn,
// addg,
// liftg,
// arrayg,
// continuizeu,
// continuize,
// vector,
// exploitVector,
// vectorSafe,
// pubsub,
// mapRecurse,
// filterRecurse,
};
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.