Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies in Julia
x541using Combinatorics
2
3allegies = ["eggs",
4"peanuts",
5"shellfish",
6"strawberries",
7"tomatoes",
8"chocolate",
9"pollen",
10"cats"]
11
12function getCombs()
13 N = 7
14
15 scores = 2 .^collect(0:N)
16 combs = []
17 for i in 1:N+1
18 combs = vcat(combs,collect(combinations(scores,i)))
19 end
20
21 combs
22end
23
24allCombs = getCombs()
25
26function getAllergies(score)
27
28 println(score)
29
30 #score = 45
31 score1 = mod(score,256)
32
33 flag = 0
34
35
36 for comb in allCombs
37 if sum(comb) == score1
38 flag = 1
39 #println(comb, ", ")
40 println(join(allegies[Int64.(trunc.(log.(2,comb))) .+ 1],","))
41 end
42 end
43
44 if flag == 0
45 println("No allergies found")
46 end
47
48end
49
50for sc in 0:512
51 getAllergies(sc)
52end
53
54
Comments
Post a Comment