Department numbers
You are encouraged to solve this task according to the task description, using any language you may know.
There is a highly organized city that has decided to assign a number to each of their departments. Police, Sanitation and Fire Department.
Each Department can have a number between 1 and 7.
The 3 numbers have to be different and have to add up to the number 12.
For some reason the Chief of the Police Department doesn't like odd numbers and wants to have an even number for his department.
Write a programm which outputs all valid combinations.
Possible Output:
1 2 9
5 3 4
Perl
<lang Perl>
- !/usr/bin/perl
my @even_numbers;
for (1..7) {
if ( $_ % 2 == 0) { push @even_numbers, $_; }
}
print "Police\tFire\tSanitation\n";
foreach my $police_number (@even_numbers) {
for my $fire_number (1..7) { for my $sanitation_number (1..7) { if ( $police_number + $fire_number + $sanitation_number == 12 && $police_number != $fire_number && $fire_number != $sanitation_number && $sanitation_number != $police_number) { print "$police_number\t$fire_number\t$sanitation_number\n"; } } }
} </lang>
REXX
<lang rexx>/*REXX program finds/displays all possible variants of (3) department numbering puzzle.*/ parse arg high sum . if high== | high=="," then high= 7 if sum== | sum=="," then sum=12
- =0; @pd='police'; @fd="fire"; @sd='sanitation'
Lpd=length(@pd); Lfd=length(@fd); Lsd=length(@sd)
do PD=2 by 2 to high do FD=1 for high if FD==PD then iterate if FD+PD>sum-1 then iterate PD $2=PD+FD do SD=1 for high if SD==PD then iterate if SD==FD then iterate $3=$2+SD if $3> sum then iterate FD if $3\==sum then iterate #=# + 1 if #==1 then say @pd @fd @sd if #==1 then say center(,Lpd,"═") center(,Lfd,"═") center(,Lsd,"═") say center(PD,Lpd) center(FD,Lfd) center(SD,Lsd) end /*SD*/ end /*FD*/ end /*PD*/
say if #==0 then #='no' say # "solutions found."</lang>
- output when using the default inputs:
police fire sanitation ══════ ════ ══════════ 2 3 7 2 4 6 2 6 4 2 7 3 4 1 7 4 2 6 4 3 5 4 5 3 4 6 2 4 7 1 6 1 5 6 2 4 6 4 2 6 5 1 14 solutions found.
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:
L(L(1,4,7),L(1,5,6),L(2,3,7),L(2,4,6),L(3,4,5))
For a table with repeated solutions: <lang zkl>ns:=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 .pump(List,Utils.Helpers.permute) // expand 5 results --> list of lists .flatten() // ( (),()..) --> () .filter(fcn([(p,_,_)]){ p.isEven }); // with even first number
println("Police Fire Sanitation"); foreach pfs in (ns){ "%d\t%d\t%d".fmt(pfs.xplode()).println() }</lang>
- Output:
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 4 6 4 2 4 6 2 4 2 6 4 5 3 4 3 5