Department numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
(added Solution in Perl)
No edit summary
Line 14: Line 14:


1 2 9
1 2 9

5 3 4
5 3 4



Revision as of 14:59, 22 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, 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.


For example:

1 2 9

5 3 4


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 (@even_numbers) {

 my $police_n = $_;
 for my $fire_n ( 1.. 12)
 {
   for my $san_n (1..12)
   {
     if ( $police_n + $fire_n + $san_n == 12 && $police_n != $fire_n && $fire_n != $san_n && $san_n != $police_n)
     {
       print "$police_n\t$fire_n\t$san_n\n";
     }
   }
 }	

} </lang>