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 Combinatorics2
3allegies = ["eggs",4"peanuts",5"shellfish",6"strawberries",7"tomatoes",8"chocolate",9"pollen",10"cats"]11
12function getCombs()13 N = 714
15 scores = 2 .^collect(0:N)16 combs = []17 for i in 1:N+118 combs = vcat(combs,collect(combinations(scores,i)))19 end20 21 combs22end23
24allCombs = getCombs()25
26function getAllergies(score)27 28 println(score)29
30 #score = 4531 score1 = mod(score,256)32 33 flag = 034 35 36 for comb in allCombs37 if sum(comb) == score138 flag = 139 #println(comb, ", ")40 println(join(allegies[Int64.(trunc.(log.(2,comb))) .+ 1],","))41 end42 end43 44 if flag == 045 println("No allergies found")46 end47 48end49
50for sc in 0:51251 getAllergies(sc)52end53
54
Comments
Post a Comment