Created
January 9, 2019 04:51
-
-
Save psaitu/485d33fd17d9eaf0caef134b78e1ebb4 to your computer and use it in GitHub Desktop.
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
var id = 3; | |
var ids = [2, 4, 6, 3, 9, 3, 6, 1, 7]; | |
var floats = [0.392, 0.328, 34.3, 45.2, 2.5, 1.54, 2.2, 4.0, 9.2]; | |
function sum_magic_numbers(id, list_of_ids, list_of_floats) { | |
// Start by finding indexes of ids in the list_of_ids for the given id | |
// O(n) | |
var list_of_indexes = [] | |
for(var i = 0; i < list_of_ids.length; i++) { | |
if(list_of_ids[i] == i) { | |
list_of_indexes.push(i); | |
} | |
} | |
// Sum the floats in list_of_floats based on indexes in list_of_indexes | |
var sum_of_floats = 0.0 | |
for(var i = 0; i < list_of_indexes.length; i++) { | |
var index = list_of_indexes[i]; | |
sum_of_floats += list_of_floats[index]; | |
} | |
return sum_of_floats; | |
} | |
console.log('Magic Numbers Sum'); | |
console.log(sum_magic_numbers(id, ids, floats)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment