Department numbers: Difference between revisions
m (→{{header|REXX}}: added the REXX language.) |
m (→{{header|REXX}}: added/changed comments, changed header information.) |
||
Line 53: | Line 53: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
A little extra code was added to allow the specification for the high department number as well as the sum. |
|||
⚫ | |||
parse arg high sum . |
|||
⚫ | |||
⚫ | |||
⚫ | |||
Lpd=length(@pd); Lfd=length(@fd); Lsd=length(@sd) |
|||
Also, extra code was added to nicely format a title (header) for the output, as well as displaying the number of solutions found. |
|||
do PD=2 by 2 to high |
|||
⚫ | |||
do FD=1 for high |
|||
parse arg high sum . /*obtain optional arguments from the CL*/ |
|||
if FD==PD then iterate |
|||
⚫ | |||
if FD+PD>sum-1 then iterate PD |
|||
⚫ | |||
$2=PD+FD |
|||
⚫ | |||
⚫ | |||
@dept= ' department '; L=length(@dept) /*literal; and also its length*/ |
|||
if SD==PD then iterate |
|||
#=0 /*initialize the number of solutions. */ |
|||
if SD==FD then iterate |
|||
do PD=2 by 2 to high /*try numbers for the police department*/ |
|||
$3=$2+SD |
|||
do FD=1 for high /* " " " " fire " */ |
|||
if $3> sum then iterate FD |
|||
if FD==PD then iterate /*Same FD# & PD#? They must be unique.*/ |
|||
if FD+PD>sum-1 then iterate PD /*Is sum too large? Try another PD#. */ |
|||
#=# + 1 |
|||
do SD=1 for high /*try numbers for the sanitation dept. */ |
|||
if SD==PD | SD==FD then iterate /*Is SD# ¬unique? They must be unique,*/ |
|||
⚫ | |||
$=PD+FD+SD /*compute sum of department numbers. */ |
|||
if $> sum then iterate FD /*Is the sum too high? Try another FD#*/ |
|||
⚫ | |||
if $\==sum then iterate /*Is the sum ¬correct? " " SD#*/ |
|||
⚫ | |||
#=# + 1 /*bump the number of solutions (so far)*/ |
|||
end /*PD*/ |
|||
if #==1 then do /*Is this the 1st solution? Show hdr.*/ |
|||
say |
|||
say center(@pd, L) center(@fd, L) center(@sd, L) |
|||
if #==0 then #='no' |
|||
say copies(center( @dept, L)' ', 3) |
|||
say # "solutions found."</lang> |
|||
say copies(center('number', L)' ', 3) |
|||
⚫ | |||
⚫ | |||
say center(PD, L) center(FD, L) center(SD, L) /*display a solution.*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
say /*display a blank line before the #sols*/ |
|||
if #==0 then #= 'no' /*use a better word for bupkis. */ |
|||
say # "solutions found." /*stick a fork in it, we're all done. */</lang> |
|||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
police fire sanitation |
police fire sanitation |
||
department department department |
|||
══════ ════ ══════════ |
|||
number number number |
|||
════════════ ════════════ ════════════ |
|||
⚫ | |||
2 |
2 3 7 |
||
2 |
2 4 6 |
||
2 6 4 |
|||
2 7 3 |
|||
4 |
4 1 7 |
||
4 |
4 2 6 |
||
4 |
4 3 5 |
||
4 |
4 5 3 |
||
6 |
4 6 2 |
||
4 7 1 |
|||
6 |
6 1 5 |
||
6 |
6 2 4 |
||
6 4 2 |
|||
6 5 1 |
|||
14 solutions found. |
14 solutions found. |
Revision as of 04:02, 23 May 2017
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
A little extra code was added to allow the specification for the high department number as well as the sum.
Also, extra code was added to nicely format a title (header) for the output, as well as displaying the number of solutions found. <lang rexx>/*REXX program finds/displays all possible variants of (3) department numbering puzzle.*/ parse arg high sum . /*obtain optional arguments from the CL*/ if high== | high=="," then high= 7 /*Not specified? Then use the default.*/ if sum== | sum=="," then sum=12 /* " " " " " " */ @pd= ' police '; @fd= " fire " ; @sd= ' sanitation ' /*define names of departments.*/ @dept= ' department '; L=length(@dept) /*literal; and also its length*/
- =0 /*initialize the number of solutions. */
do PD=2 by 2 to high /*try numbers for the police department*/ do FD=1 for high /* " " " " fire " */ if FD==PD then iterate /*Same FD# & PD#? They must be unique.*/ if FD+PD>sum-1 then iterate PD /*Is sum too large? Try another PD#. */ do SD=1 for high /*try numbers for the sanitation dept. */ if SD==PD | SD==FD then iterate /*Is SD# ¬unique? They must be unique,*/ $=PD+FD+SD /*compute sum of department numbers. */ if $> sum then iterate FD /*Is the sum too high? Try another FD#*/ if $\==sum then iterate /*Is the sum ¬correct? " " SD#*/ #=# + 1 /*bump the number of solutions (so far)*/ if #==1 then do /*Is this the 1st solution? Show hdr.*/ say center(@pd, L) center(@fd, L) center(@sd, L) say copies(center( @dept, L)' ', 3) say copies(center('number', L)' ', 3) say center(, L, "═") center(, L, "═") center(, L, "═") end say center(PD, L) center(FD, L) center(SD, L) /*display a solution.*/ end /*SD*/ end /*FD*/ end /*PD*/
say /*display a blank line before the #sols*/ if #==0 then #= 'no' /*use a better word for bupkis. */ say # "solutions found." /*stick a fork in it, we're all done. */</lang>
- output when using the default inputs:
police fire sanitation department department department number number number ════════════ ════════════ ════════════ 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