Get infected parameters starting from a specific carrier
Imagine one guy got infected by a virus and you want to detect potential infections by direct contact. We have an array that represents the city where he lives and subarrays that represent the houses where he went. We are gonna use numbers (people identification) for our example.
Updated version using Set properly thanks to japanfever
japanfever I fixed it, now you can use pre tag
//blog code tag not working so you probably cant see my solution
//japanfever returns E3839EE383AAE382AAE9B3A9E38388E383ACE383AD
function get_infected(structure, carrier) {
var tree = {}
structure.forEach(([a, b]) => {
tree[a] = (new Set(tree[a])).add(b)
tree[b] = (new Set(tree[b])).add(a)
})
return carrier.reduce((acc, sub) => acc.concat(Array.from(tree[sub])), carrier)
}
Dude, that’s nice xD well done, but it doesn’t work when you change the quantity of people living in the houses. For example, try: structure = [[0, 1, 5], [0, 4], [2, 3], [1, 4], [5, 6]]
Much easier than mine, I should have used Set properly. It works like a charm!
you lie, pre not fixed
No, it’s fixed. I just added the pre tags to your code and it’s working. How did you do it? https://codex.wordpress.org/Writing_Code_in_Your_Posts