Department numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
Line 66: Line 66:
</pre>
</pre>


=={{header|Lua}}==
<lang lua>
print( "Fire", "Police", "Sanitation" )
sol = 0
for f = 1, 7 do
for p = 1, 7 do
for s = 1, 7 do
if s + p + f == 12 and p % 2 == 0 and f ~= p and f ~= s and p ~= s then
print( f, p, s ); sol = sol + 1
end
end
end
end
print( string.format( "\n%d solutions found", sol ) )
</lang>
{{out}}
<pre>
Fire Police Sanitation
1 4 7
1 6 5
2 4 6
2 6 4
3 2 7
3 4 5
4 2 6
4 6 2
5 4 3
5 6 1
6 2 4
6 4 2
7 2 3
7 4 1

14 solutions found
</pre>
=={{header|Perl}}==
=={{header|Perl}}==



Revision as of 06:19, 23 May 2017

Task
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 department
  •   sanitation department
  •   fire department


Each department can have a number between 1 and 7   (inclusive).

The three department numbers are to be unique (different from each other) and must add up to the number 12.

The Chief of the Police doesn't like odd numbers and wants to have an even number for his department.


Task

Write a program which outputs all valid combinations.


Possible output:

1 2 9
5 3 4

C++

<lang cpp>

  1. include <iostream>
  2. include <iomanip>

int main( int argc, char* argv[] ) {

   int sol = 1;
   std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n";
   for( int f = 1; f < 8; f++ ) {
       for( int p = 1; p < 8; p++ ) {
           for( int s = 1; s < 8; s++ ) {
               if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) {
               std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 ) 
               << ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p 
               << "\t\t" << std::setw( 6 ) << s << "\n";
               }
           }
       }
   }
   return 0;

} </lang>

Output:
                FIRE            POLICE          SANITATION
SOLUTION # 1:    1                 4                 7
SOLUTION # 2:    1                 6                 5
SOLUTION # 3:    2                 4                 6
SOLUTION # 4:    2                 6                 4
SOLUTION # 5:    3                 2                 7
SOLUTION # 6:    3                 4                 5
SOLUTION # 7:    4                 2                 6
SOLUTION # 8:    4                 6                 2
SOLUTION # 9:    5                 4                 3
SOLUTION #10:    5                 6                 1
SOLUTION #11:    6                 2                 4
SOLUTION #12:    6                 4                 2
SOLUTION #13:    7                 2                 3
SOLUTION #14:    7                 4                 1

Lua

<lang lua> print( "Fire", "Police", "Sanitation" ) sol = 0 for f = 1, 7 do

   for p = 1, 7 do
       for s = 1, 7 do
           if s + p + f == 12 and p % 2 == 0 and f ~= p and f ~= s and p ~= s then
               print( f, p, s ); sol = sol + 1
           end
       end
   end

end print( string.format( "\n%d solutions found", sol ) ) </lang>

Output:
Fire    Police  Sanitation
1       4       7
1       6       5
2       4       6
2       6       4
3       2       7
3       4       5
4       2       6
4       6       2
5       4       3
5       6       1
6       2       4
6       4       2
7       2       3
7       4       1

14 solutions found

Perl

<lang Perl>

  1. !/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*/

  1. =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