Jump to content

Department numbers: Difference between revisions

(→‎{{header|Javascript}}: Added an ES5 version)
Line 714:
 
=={{header|zkl}}==
<lang zkl>Utils.Helpers.pickNFrom(3,[1..7].walk()) // 35 combos
.filter(fcn(numbers){ numbers.sum(0)==12 }) // which all sum to 12 (==5)
.filter("apply","isEven") // at least one number is even
.println();</lang>
{{output}}
Line 722 ⟶ 721:
L(L(1,4,7),L(1,5,6),L(2,3,7),L(2,4,6),L(3,4,5))
</pre>
Note: The sum of three odd numbers is odd, so a+b+c=12 means at least one even
For a table with repeated solutions:
nmber (1 even, two odd or 3 even). Futher, 2a+b=12, a,b in (2,4,6) has one
<lang zkl>ns:=Utils.Helpers.pickNFrom(3,[1..7].walk()) // 35 combos
solution: a=2,b=4
.filter(fcn(numbers){ numbers.sum(0)==12 }) // which all sum to 12 (==5)
 
.filter("apply","isEven") // at least one number is even
For a table with repeated solutions using nested loops:
.pump(List,Utils.Helpers.permute) // expand 5 results --> list of lists
<lang zkl>println("Police Fire Sanitation");
.flatten() // ( (),()..) --> ()
foreach p,f,s in ([2..7,2], [1..7], [1..7])
.filter(fcn([(p,_,_)]){ p.isEven }); // with even first number
{ if((p!=s!=f) and p+f+s==12) println(p,"\t",f,"\t",s) }</lang>
println("Police Fire Sanitation");
foreach pfs in (ns){ "%d\t%d\t%d".fmt(pfs.xplode()).println() }</lang>
{{out}}
<pre>
Police Fire Sanitation
4 7 1
4 1 7
6 1 5
6 5 1
2 3 7
2 7 3
2 4 6
2 6 4
6 2 47 3
6 4 21 7
4 6 2
4 2 6
4 5 3
4 3 5
4 15 73
4 6 2
4 7 1
6 1 5
6 2 4
6 4 2
6 5 1
</pre>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.