1
$\begingroup$

I have this code that gives me all possible coalitions after the Dutch elections of maximum 4 political parties. However, the output is given in numbers. I would rather have the output in the names of the parties. How do I do it?

VVD = 33;
PVDA = 9;
PVV = 20;
SP = 14;
CDA = 19;
D66 = 19;
ChristenUnie = 5;
GroenLinks = 14;
SGP = 3;
PVDD = 5;
VPLUS = 4;
DENK = 3;
FVD = 2;

Lijst := {VVD, PVDA, PVV, SP, CDA, D66, ChristenUnie, GroenLinks, SGP,
   PVDD, VPLUS, DENK, FVD}

Select[Subsets[Lijst], Total[#] > 75 && Length[#] <= 4 &]
$\endgroup$
  • $\begingroup$ Is this topic enough 70250? also related: 40663 $\endgroup$ – Kuba Mar 17 '17 at 11:00
  • 1
    $\begingroup$ @Szabolcs Ok, I changed my question to what I really want to know ;-) $\endgroup$ – GambitSquared Mar 17 '17 at 11:18
  • 1
    $\begingroup$ @Szabolcs What I try to do in the end is show which coalitions are possible. You suggest using an association? How would that work then? $\endgroup$ – GambitSquared Mar 17 '17 at 11:23
  • 1
    $\begingroup$ Same as your original example except you use mylist = <|"a"->1, "b"->5, "c"->7|>. For this application variables are not appropriate. Instead, bundle the name and the value together into one data structure. That can be {"name", value}, "name" -> value, an association like above, etc. There are lots of choices. Variables are about the worst possibility. $\endgroup$ – Szabolcs Mar 17 '17 at 11:59
  • 1
    $\begingroup$ Just for fun: SortBy[(Select[ Thread[# -> Total[#[[All, All, 2]], {2}] ], #[[2]] > 75 &] &@ Subsets[list, 4]), Minus@*Last] // TableForm where list = {{VVD, 33}, {PVDA, 9}, {PVV, 20}, {SP, 14}, {CDA, 19}, {D66, 19}, {ChristenUnie, 5}, {GroenLinks, 14}, {SGP, 3}, {PVDD, 5}, {VPLUS, 4}, {DENK, 3}, {FVD, 2}} $\endgroup$ – user1066 Mar 17 '17 at 15:12
6
$\begingroup$

For example

a = <|"VVD" -> 33, "PVDA" -> 9, "PVV" -> 20, "SP" -> 14, 
   "CDA" -> 19, "D66" -> 19, "ChristenUnie" -> 5, "GroenLinks" -> 14, 
   "SGP" -> 3, "PVDD" -> 5, "VPLUS" -> 4, "DENK" -> 3, "FVD" -> 2|>;

Select[Subsets[Keys[a], {4}], Total[a[[#]]] == 75 &]

(* {{"VVD", "PVDA", "SP", "CDA"}, {"VVD", "PVDA", "SP", 
  "D66"}, {"VVD", "PVDA", "CDA", "GroenLinks"}, {"VVD", "PVDA", "D66",
   "GroenLinks"}, {"VVD", "PVV", "CDA", "SGP"}, {"VVD", "PVV", "CDA", 
  "DENK"}, {"VVD", "PVV", "D66", "SGP"}, {"VVD", "PVV", "D66", 
  "DENK"}, {"VVD", "CDA", "D66", "VPLUS"}} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.