Skip to content

Instantly share code, notes, and snippets.

@exbotanical
Last active July 18, 2020 05:17
Show Gist options
  • Save exbotanical/e406fd207527aa4cc5824e67d027e904 to your computer and use it in GitHub Desktop.
Save exbotanical/e406fd207527aa4cc5824e67d027e904 to your computer and use it in GitHub Desktop.
more codegolf
const rowWeights = arr => {
let a = 0;
let b = 0;
Object.entries(arr).forEach(([i, val]) => {
i % 2 === 0 ? a += val : b += val;
});
return [a, b];
}
/*
Prompt
Given an array of positive integers (the weights of the people), return a new array/tuple of two integers,
where the first one is the total weight of team 1, and the second one is the total weight of team 2.
*/
const type = obj => Object.prototype.toString.apply(obj).replace(/\[object (.+)\]/i, "$1").toLowerCase();
const humanYearsCatYearsDogYears = n => n === 1 ? [1, 15, 15] : ((n) => {
let v = 0;
for (let i = 0; i <= n; i++) {
i === 1 ? v += 15 : i === 2 ? v += 9 : i > 2 ? v += 4 : null;
};
return [n, v, v + (n - 2)];
})(n);
/*
Code Golfin'
Prompt
"Return their respective ages now as [humanYears,catYears,dogYears]
NOTES:
humanYears >= 1
humanYears are whole numbers only
Cat Years
15 cat years for first year
+9 cat years for second year
+4 cat years for each year after that
Dog Years
15 dog years for first year
+9 dog years for second year
+5 dog years for each year after that"
*/
const validateCode = (code, matches) => matches.includes(Number((code+"")[0]));
const digitsAverage = input => {
if (input < 10) {
return input;
}
while (input > 9) {
input = String(input);
let arr = [];
for (let i = 0; i < input.length - 1; i++) {
arr.push(Math.round((+input[i] + +input[i + 1]) / 2));
}
input = parseInt(arr.join(""));
if (input < 10) {
return input;
}
}
};
/*
Prompt
"Given an integer, take the (mean) average of each pair of consecutive digits.
Repeat this process until you have a single integer, then return that integer.
Note: if the average of two digits is not an integer, round the result up
(e.g. the average of 8 and 9 will be 9)."
*/
const remove = (s, target) => {
if (s[s.length - 1] !== target) {
return s;
}
let x = 0;
for (let i in s) {
if (s[i] == target) {
break;
}
x += 1;
}
return s.slice(0, x) ? s.slice(0, x) : remove(s.slice(x + 1, s.length), target)
}
// removes all occurrences of target char from string if and only if the string ends in said target
const remove = str => {
let arr = str.split("");
for (let i = arr.length - 1; i > 0; i--) {
if (arr[i] === "!") {
arr.pop();
}
else {
break;
}
}
return arr.join("");
}
/*
Prompt
"Remove all exclamation marks from the end of sentence."
*/
// xor using a ternary expression
const xor = (x, y) => x ? !y ? true : false : y ? true : false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment