Created
January 21, 2019 20:37
-
-
Save apolopena/6508d1bf19004e46db819cdceaef774c to your computer and use it in GitHub Desktop.
JavaScript: HackerRank Apples and Oranges Tree falling by the house problem solved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// my solution using easy to refactor methods (in case you wanted to either share logic or compare new types of fruit) | |
function countApplesAndOranges(s, t, a, b, apples, oranges) { | |
const fLoc = function (treeLoc, arr2d) { | |
return arr2d.map(fruitLoc => (treeLoc + fruitLoc)); | |
} | |
const fRange = function (s, t, arr2d) { | |
let a, b; | |
a = 0; b = 0; | |
arr2d.forEach((f, i) => { | |
if (i === 0) { // apple count | |
f.forEach(loc => s <= loc && loc <= t ? a++ : null); | |
} | |
if (i === 1) { // orange count | |
f.forEach(loc => s <= loc && loc <= t ? b++ : null); | |
} | |
}); | |
return [a, b]; | |
} | |
console.log(fRange(s, t, [fLoc(a, apples), fLoc(b, oranges)]).join('\n')); | |
} | |
// shorter solutions... | |
// using filter | |
function countApplesAndOranges2(s, t, a, b, apples, oranges) { | |
console.log(apples.filter(d => s - a <= d && d <= t - a).length); | |
console.log(oranges.filter(d => s - b <= d && d <= t - b).length); | |
} | |
// using reduce | |
function countApplesAndOranges3(s, t, a, b, apples, oranges) { | |
console.log( apple.reduce((sum, d) => sum + (s - a <= d && d <= t - a), 0)); | |
console.log(orange.reduce((sum, d) => sum + (s - b <= d && d <= t - b), 0)); | |
} | |
// using filter and map | |
function countApplesAndOranges4 { | |
function addBy(num) { | |
return (d) => d + num; | |
} | |
function isScored(d) { | |
return s <= d && d <= t; | |
} | |
const larry = apples.map(addBy(a)).filter(isScored).length; | |
const rob = oranges.map(addBy(b)).filter(isScored).length; | |
console.log(larry, rob); | |
} | |
// using Array.prototype (bad because a new version of ECMA could come out with a 'sum' helper for arrays) | |
function countApplesAndOranges5 { | |
Array.prototype.sum = function(f) { | |
return this.reduce((s, v) => s + f(v), 0); | |
} | |
console.log( apple.sum(d => s - a <= d && d <= t - a)); | |
console.log(orange.sum(d => s - b <= d && d <= t - b)); | |
} | |
how you are getting i === 0 & i===1 count ?
how you are getting i === 0 & i===1 count ?
i
is the index of the 2d f
'fruit' array containing the apples and oranges data
The index of the fruit array will be 0
for apple data since it is assumed that apple data will always come first
The index of the fruit array will be 1
for orange data since it is assumes apple data will always come second
arr2d.forEach((f, i) => { // f stands for fruit
if (i === 0) { // apple count
f.forEach(loc => s <= loc && loc <= t ? a++ : null);
}
if (i === 1) { // orange count
f.forEach(loc => s <= loc && loc <= t ? b++ : null);
}
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how you are getting i === 0 & i===1 count ?