Department numbers: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 28:
=={{header|11l}}==
{{trans|C}}
<
print(‘----------------------------------’)
Line 35:
L(fire) 1..7
I police!=sanitation & sanitation!=fire & fire!=police & police+fire+sanitation==12
print(police"\t\t"sanitation"\t\t"fire)</
{{Output}}
<pre>
Line 57:
=={{header|8080 Assembly}}==
<
lxi h,obuf ; HL = output buffer
mvi b,2 ; B = police
Line 105:
ret
ohdr: db 'P S F',13,10
obuf: equ $ ; Output buffer goes after program</
{{out}}
Line 128:
=={{header|8086 Assembly}}==
<
bits 16
org 100h
Line 177:
section .data
ohdr: db 'P S F',13,10 ; Header
obuf: equ $ ; Place to write output</
{{out}}
Line 200:
=={{header|Action!}}==
<
BYTE p,s,f
Line 216:
OD
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Department_numbers.png Screenshot from Atari 8-bit computer]
Line 238:
=={{header|Ada}}==
<
procedure Department_Numbers is
Line 259:
end loop;
end loop;
end Department_Numbers;</
{{out}}
Line 281:
=={{header|Aime}}==
<
p = 0;
Line 294:
}
}
}</
=={{header|ALGOL 68}}==
As noted in the Fortran sample, once the police and sanitation departments are posited, the fire department value is fixed
<
# show possible department number allocations for police, sanitation and fire departments #
# the police department number must be even, all department numbers in the range 1 .. 7 #
Line 323:
OD
OD
END</
{{out}}
<pre>
Line 345:
=={{header|ALGOL W}}==
{{Trans|ALGOL 68}}
<
% show possible department number allocations for police, sanitation and fire departments %
% the police department number must be even, all department numbers in the range 1 .. 7 %
Line 364:
end for_sanitation
end for_police
end.</
{{out}}
<pre>
Line 386:
=={{header|APL}}==
<
This prints each triplet of numbers from 1 to 7 for which:
Line 415:
Briefly, composing a solution from generic functions:
<
script
on |λ|(x)
Line 479:
on unlines(xs)
intercalate(linefeed, xs)
end unlines</
{{Out}}
<pre>237
Line 499:
{{Trans|JavaScript}}
{{Trans|Haskell}}
<
-- options :: Int -> Int -> Int -> [(Int, Int, Int)]
Line 672:
on unlines(xs)
intercalate(linefeed, xs)
end unlines</
{{Out}}
<pre>(Police, Sanitation, Fire)
Line 695:
=={{header|Arturo}}==
<
loop 1..7 'y [
loop 1..7 'z [
Line 705:
]
]
]</
{{out}}
Line 725:
=={{header|Asymptote}}==
<
for(int police = 2; police < 6; police += 2) {
Line 737:
}
}
}</
=={{header|AutoHotkey}}==
<
res := IsObject(res) ? res : [], dup := IsObject(dup) ? dup : []
if (n > j)
Line 755:
Sort, res, D`n
return StrReplace(res, "`n", Delim)
}</
Example:<
for k, v in perm(elements, n)
if (SubStr(v, 1, 1) + SubStr(v, 2, 1) + SubStr(v, 3, 1) = 12) && (SubStr(v, 1, 1) / 2 = Floor(SubStr(v, 1, 1)/2))
Line 762:
MsgBox, 262144, , % res4
return</
Outputs:<pre>237
246
Line 779:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f DEPARTMENT_NUMBERS.AWK
BEGIN {
Line 800:
return(stmt1 stmt2 stmt3)
}
</syntaxhighlight>
{{out}}
<pre>
Line 821:
=={{header|BCPL}}==
<
let start() be
Line 828:
for f=1 to 7
if p~=s & s~=f & p~=f & p+s+f=12 then
writef("%N %N %N*N", p, s, f)</
{{out}}
<pre>2 3 7
Line 849:
The [[#Sinclair_ZX81_BASIC|Sinclair ZX81 BASIC]] solution works without any changes.
==={{header|BASIC256}}===
<
for police = 2 to 7 step 2
Line 860:
end if
next fire
next police</
{{out}}
<pre>--police-- --sanitation-- --fire--
Line 879:
==={{header|Run BASIC}}===
<
for police = 2 to 7 step 2
Line 889:
[cont]
next fire
next police</
==={{header|PureBasic}}===
<
PrintN("--police-- --sanitation-- --fire--")
Line 909:
Input()
CloseConsole()</
{{out}}
<pre>Same as BASIC256 entry.</pre>
==={{header|Yabasic}}===
<
for police = 2 to 7 step 2
Line 923:
if sanitation >= 1 and sanitation <= 7 print police using "######", fire using "############", sanitation using "###########"
next fire
next police</
{{out}}
<pre>Same as BASIC256 entry.</pre>
Line 929:
==={{header|IS-BASIC}}===
<
110 FOR P=2 TO 7 STEP 2
120 FOR S=1 TO 7
Line 937:
141 END IF
150 NEXT
160 NEXT</
==={{header|Minimal BASIC}}===
{{trans|Sinclair ZX81 BASIC}}
<
10 REM Department numbers
20 PRINT "POLICE SANITATION FIRE"
Line 953:
100 NEXT P
110 END
</syntaxhighlight>
==={{header|Sinclair ZX81 BASIC}}===
Works with 1k of RAM. This program ought not to need more than minimal changes to be compatible with any old-style BASIC dialect.
<
20 FOR P=2 TO 7 STEP 2
30 FOR S=1 TO 7
Line 964:
60 IF F>0 AND F<=7 AND F<>S AND F<>P THEN PRINT " ";P;" ";S;" ";F
70 NEXT S
80 NEXT P</
{{out}}
<pre>POLICE SANITATION FIRE
Line 984:
==={{header|BBC BASIC}}===
{{trans|ALGOL 68}}
<
max_dept_num% = 7
dept_sum% = 12
Line 996:
NEXT
NEXT
END</
{{out}}
<pre>police sanitation fire
Line 1,015:
==={{header|QBasic}}===
<
FOR police = 2 TO 7 STEP 2
Line 1,027:
cont:
NEXT fire
NEXT police</
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<
DECLARE FUNCTION Entry ()
Line 1,050:
NEXT police
END FUNCTION
END PROGRAM </
=={{header|C}}==
Weird that such a simple task was still not implemented in C, would be great to see some really creative ( obfuscated ) solutions for this one.
<syntaxhighlight lang="c">
#include<stdio.h>
Line 1,076:
return 0;
}
</syntaxhighlight>
Output:
<pre>
Line 1,098:
=={{header|C sharp|C#}}==
<
public class Program
{
Line 1,113:
}
}
}</
{{out}}
<pre>
Line 1,133:
=={{header|C++}}==
<
#include <iostream>
#include <iomanip>
Line 1,152:
}
return 0;
}</
{{out}}
<pre>
Line 1,173:
=={{header|Clojure}}==
<
(for [police n
sanitation n
Line 1,180:
:when (even? police)
:when (= 12 (+ police sanitation fire))]
(println police sanitation fire)))</
{{Out}}
<pre>
Line 1,200:
=={{header|CLU}}==
<
po: stream := stream$primary_output()
Line 1,219:
end
end
end start_up</
{{out}}
<pre>P S F
Line 1,239:
=={{header|COBOL}}==
<
PROGRAM-ID. DEPARTMENT-NUMBERS.
Line 1,275:
AND SANITATION IS NOT EQUAL TO FIRE
AND TOTAL IS EQUAL TO 12,
DISPLAY COMBINATION.</
{{out}}
<pre>POLICE SANITATION FIRE
Line 1,294:
=={{header|Cowgol}}==
<
typedef Dpt is int(1, 7);
Line 1,323:
end loop;
pol := pol + 2;
end loop;</
{{out}}
Line 1,346:
{{Trans|C++}}
<syntaxhighlight lang="d">
import std.stdio, std.range;
Line 1,362:
}
}
</syntaxhighlight>
Output:
<pre>
Line 1,384:
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Department_numbers;
Line 1,420:
writeln(#10, count, ' valid combinations');
readln;
end.</
=={{header|Draco}}==
<
byte police, sanitation, fire;
Line 1,439:
od
od
corp</
{{out}}
<pre>Police Sanitation Fire
Line 1,458:
=={{header|Elixir}}==
<
IO.puts("P - F - S")
for p <- [2,4,6],
Line 1,468:
|> Enum.each(&IO.puts/1)
</syntaxhighlight>
<
P - F - S
2 - 3 - 7
Line 1,485:
6 - 4 - 2
6 - 5 - 1
</syntaxhighlight>
=={{header|Excel}}==
Line 1,495:
{{Works with|Office 365 betas 2021}}
<
=validRows(
LAMBDA(ab,
Line 1,519:
)
)
)</
and also assuming the following generic bindings in the Name Manager for the WorkBook:
<
=LAMBDA(xs,
LAMBDA(ys,
Line 1,565:
)
)
)</
{{Out}}
Line 1,669:
=={{header|F_Sharp|F#}}==
<
// A function to generate department numbers. Nigel Galloway: May 2nd., 2018
type dNum = {Police:int; Fire:int; Sanitation:int}
let fN n=n.Police%2=0&&n.Police+n.Fire+n.Sanitation=12&&n.Police<>n.Fire&&n.Police<>n.Sanitation&&n.Fire<>n.Sanitation
List.init (7*7*7) (fun n->{Police=n%7+1;Fire=(n/7)%7+1;Sanitation=(n/49)+1})|>List.filter fN|>List.iter(printfn "%A")
</syntaxhighlight>
{{out}}
<pre>
Line 1,722:
=={{header|Factor}}==
<
sequences sets ;
IN: rosetta-code.department-numbers
Line 1,730:
"{ Police, Sanitation, Fire }" print nl
[ "%[%d, %]\n" printf ] each</
{{out}}
<pre>
Line 1,751:
</pre>
=={{header|Fermat}}==
<
!!'------|----------|----';
for p = 2 to 6 by 2 do
Line 1,757:
for f = 1 to 7 do
if p+f+s=12 and f<>p and f<>s and s<>p then !!(' ',p,' ',s,' ',f);
fi od od od;</
{{out}}<pre>
Police Sanitation Fire
Line 1,778:
=={{header|FOCAL}}==
<
01.20 Q
Line 1,786:
02.40 I (P+S+G-12)2.6,2.5,2.6
02.50 T %1,P,S,G,!
02.60 R</
{{out}}
<pre>= 2= 3= 7
Line 1,804:
=={{header|Forth}}==
<
: fire ( pol san fir -- )
2dup = if 2drop drop exit then
Line 1,826:
\ tries to assign numbers with police = 2, 4, or 6
: departments cr \ leave input line
8 2 do i police 2 +loop ;</
{{out}}
Line 1,855:
Since the modernisers of Fortran made a point of specifying that it does not specify the manner of evaluation of compound boolean expressions, specifically, that there is to be no reliance on [[Short-circuit_evaluation]], both parts of the compound expression of the line labelled 5 "may" be evaluated even though the first may have determined the result. Prior to the introduction of LOGICAL variables with F66, one employed integer arithmetic as is demonstrated in the arithmetic-IF test of the line labelled 6. On the B6700, this usage ran faster than the corresponding boolean expression - possibly because there was no test for short-circuiting the expression when the first part of a multiply was zero...
Note that the syntax enables ''two'' classes of labels: the old-style numerical label in columns one to five, and the special label-like prefix of a DO-loop that is not in columns one to five. And yes, a line can have both. <
1 PP:DO P = 2,7,2 !The police demand an even number. They're special and use violence.
2 SS:DO S = 1,7 !The sanitation department accepts any value.
Line 1,865:
8 END DO SS !Next S
9 END DO PP !Next P.
END !Well, that was straightforward.</
Output:
<pre>
Line 1,885:
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 1,908:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>police fire sanitation
Line 1,929:
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=cdfeadc26aaeb0e23f4523626b6fe7c9 Click this link to run this code]'''
<
Dim siC0, siC1, siC2 As Short
Dim sOut As New String[]
Line 1,952:
Next
End</
Output:
<pre>
Line 1,974:
=={{header|Go}}==
{{trans|Kotlin}}
<
import "fmt"
Line 1,994:
}
fmt.Printf("\n%d valid combinations\n", count)
}</
{{out}}
Line 2,020:
=={{header|Groovy}}==
{{trans|Java}}
<
static void main(String[] args) {
println("Police Sanitation Fire")
Line 2,039:
println("$count valid combinations")
}
}</
{{out}}
<pre>Police Sanitation Fire
Line 2,061:
=={{header|GW-BASIC}}==
<
20 PRINT "------|----------|----"
30 FOR P = 2 TO 7 STEP 2
Line 2,071:
90 NEXT F
100 NEXT S
110 NEXT P</
=={{header|Haskell}}==
Bare minimum:
<
main =
mapM_ print $
Line 2,086:
case y /= z && 1 <= z && z <= 7 of
True -> [(x, y, z)]
_ -> []</
or, resugaring this into list comprehension format:
<
main =
mapM_
Line 2,096:
, y <- [1 .. 7]
, z <- [12 - (x + y)]
, y /= z && 1 <= z && z <= 7 ]</
Do notation:
<
main =
mapM_ print $
Line 2,106:
if y /= z && 1 <= z && z <= 7
then [(x, y, z)]
else []</
Unadorned brute force – more than enough at this small scale:
<
main :: IO ()
Line 2,121:
\z ->
[ (x, y, z)
| even x && 3 == length (nub [x, y, z]) && 12 == sum [x, y, z] ]</
{{Out}}
<pre>(2,3,7)
Line 2,139:
Or, more generally:
<
options :: Int -> Int -> Int -> [(Int, Int, Int)]
Line 2,166:
[ "\nNumber of options: ",
show (length xs)
]</
Reaching again for a little more syntactic sugar, the options function above could also be re-written either as a list comprehension,
<
options lo hi total =
let ds = [lo .. hi]
Line 2,176:
, y <- filter (/= x) ds
, let z = total - (x + y)
, y /= z && lo <= z && z <= hi ]</
or in Do notation:
<
options :: Int -> Int -> Int -> [(Int, Int, Int)]
Line 2,188:
let z = total - (x + y)
guard $ y /= z && lo <= z && z <= hi
return (x, y, z)</
{{Out}}
<pre>(Police, Sanitation, Fire)
Line 2,212:
=={{header|J}}==
'''Solution:'''
<
permfrom=: ,/@(perm@[ {"_ 1 comb) NB. get permutations of length x from y possible items
Line 2,223:
Validnums=: >: i.7 NB. valid Department numbers
getDeptNums=: [: (#~ conditions"1) Validnums {~ permfrom</
'''Example usage:'''
<
4 1 7
4 7 1
Line 2,239:
6 4 2
4 3 5
4 5 3</
===Alternate approach===
<
4 1 7
6 1 5
2 3 7
2 4 6
4 3 5</
Note that we are only showing the distinct valid [[combinations]] here, not all valid [[permutations]] of those combinations.
Line 2,254:
=={{header|Java}}==
{{trans|Kotlin}}
<
public static void main(String[] args) {
System.out.println("Police Sanitation Fire");
Line 2,272:
System.out.printf("\n%d valid combinations", count);
}
}</
{{out}}
<pre>Police Sanitation Fire
Line 2,294:
===ES5===
Briefly:
<
'use strict';
Line 2,314:
.map(JSON.stringify)
.join('\n');
})();</
{{Out}}
<pre>(Police, Sanitation, Fire)
Line 2,334:
Or, more generally:
{{Trans|Haskell}}
<
'use strict';
Line 2,412:
return '(Police, Sanitation, Fire)\n\n' +
unlines(map(show, xs)) + '\n\nNumber of options: ' + length(xs);
})();</
{{Out}}
<pre>(Police, Sanitation, Fire)
Line 2,435:
===ES6===
Briefly:
<
"use strict";
Line 2,456:
return `${label}\n${solutions}`;
})();</
{{Out}}
<pre>(Police, Sanitation, Fire)
Line 2,476:
Or, more generally, by composition of generic functions:
{{Trans|Haskell}}
<
"use strict";
Line 2,526:
// MAIN ---
return main();
})();</
{{Out}}
<pre>(Police, Sanitation, Fire)
Line 2,563:
'''Nested for-loop'''
<
(fire != police) and (fire != sanitation) and (police != sanitation)
and (fire + police + sanitation == 12)
Line 2,572:
| range(1;8) as $sanitation
| select( check($fire; $police; $sanitation) )
| {$fire, $police, $sanitation}</
'''In Brief'''
<
| select( .fire != .police and .fire != .sanitation and .police != .sanitation
and .fire + .police + .sanitation == 12
and .police % 2 == 0 )</
'''combinations'''
<syntaxhighlight lang="jq">
[range(1;8)]
| combinations(3)
| select( add == 12 and .[1] % 2 == 0)
| {fire: .[0], police: .[1], sanitation: .[2]}</
=={{header|Julia}}==
<
function findsolution(rng=1:7)
Line 2,609:
printsolutions(findsolution())
</syntaxhighlight>
{{out}}
Line 2,630:
=={{header|Kotlin}}==
<
fun main(args: Array<String>) {
Line 2,648:
}
println("\n$count valid combinations")
}</
{{out}}
Line 2,673:
=={{header|Lua}}==
<
print( "Fire", "Police", "Sanitation" )
sol = 0
Line 2,686:
end
print( string.format( "\n%d solutions found", sol ) )
</syntaxhighlight>
{{out}}
<pre>
Line 2,709:
=={{header|MAD}}==
<
PRINT COMMENT $ POLICE SANITATION FIRE$
THROUGH LOOP, FOR P=2, 2, P.G.7
Line 2,718:
LOOP CONTINUE
VECTOR VALUES OCC = $I6,S2,I10,S2,I4*$
END OF PROGRAM</
{{out}}
<pre>POLICE SANITATION FIRE
Line 2,737:
=={{header|Maple}}==
<
exclusive_numbers := proc(i, j, k)
if (i = j) or (i = k) or (j = k) then
Line 2,760:
department_numbers();
</syntaxhighlight>
{{out|Output}}
<pre>
Line 2,781:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
{{out}}
<pre>{{2, 3, 7}, {2, 4, 6}, {2, 6, 4}, {2, 7, 3}, {4, 1, 7}, {4, 2, 6}, {4, 3, 5}, {4, 5, 3},
Line 2,787:
=={{header|Modula-2}}==
<
FROM Conversions IMPORT IntToStr;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 2,831:
ReadChar;
END DepartmentNumbers.</
=={{header|Mercury}}==
<
:- interface.
Line 2,862:
Police \= Fire,
Sanitation \= Fire,
Police + Sanitation + Fire = 12.</
{{out}}
<pre>P S F
Line 2,881:
=={{header|Nim}}==
<
iterator solutions(max, total: Positive): Solution =
Line 2,893:
echo "P S F"
for sol in solutions(7, 12):
echo sol.p, " ", sol.s, " ", sol.f</
{{out}}
Line 2,914:
=={{header|Objeck}}==
{{Trans|C++}}
<
function : Main(args : String[]) ~ Nil {
sol := 1;
Line 2,929:
};
}
}</
Output:
Line 2,951:
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">(*
* Caution: This is my first Ocaml program and anyone with Ocaml experience probably thinks it's horrible
* So please don't use this as an example for "good ocaml code" see it more as
Line 3,016:
print_sfp_list result;
</syntaxhighlight>
{{out}}
<pre>S F P
Line 3,037:
=={{header|PARI/GP}}==
<
{{out}}
<pre>2 3 7
Line 3,056:
=={{header|Perl}}==
<syntaxhighlight lang="perl">
#!/usr/bin/perl
Line 3,087:
}
}
</syntaxhighlight>
Above Code cleaned up and shortened
<syntaxhighlight lang="perl">
#!/usr/bin/perl
Line 3,112:
}
}
</syntaxhighlight>
Output:
<syntaxhighlight lang="perl">
Police - Fire - Sanitation
2 - 3 - 7
Line 3,131:
6 - 4 - 2
6 - 5 - 1
</syntaxhighlight>
===Alternate with Regex===
<
use strict; # https://rosettacode.org/wiki/Department_numbers
Line 3,143:
/(.).* \s .*?(?!\1)(.).* \s .*(?!\1)(?!\2)(.)
(??{$1+$2+$3!=12})
(?{ print "@{^CAPTURE}\n" })(*FAIL)/x;</
{{out}}
<pre>
Line 3,164:
</pre>
===Alternate with Glob===
<
use strict; # https://rosettacode.org/wiki/Department_numbers
Line 3,173:
print tr/+/ /r, "\n" for
grep !/(\d).*\1/ && 12 == eval,
glob '{2,4,6}' . '+{1,2,3,4,5,6,7}' x 2;</
Output same as with Regex
=={{header|Phix}}==
<!--<
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Police Sanitation Fire\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"------ ---------- ----\n"</span><span style="color: #0000FF;">)</span>
Line 3,196:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n%d solutions found\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">solutions</span><span style="color: #0000FF;">)</span>
<!--</
{{out}}
<pre>
Line 3,221:
=={{header|PHP}}==
<
$valid = 0;
Line 3,233:
}
}
echo $valid, ' valid combinations found.', PHP_EOL;</
{{out}}
Line 3,255:
===Constraint model===
<
go ?=>
Line 3,276:
Police + Sanitation + Fire #= 12,
Police mod 2 #= 0,
solve([Police,Sanitation,Fire]).</
{{out}}
Line 3,298:
===Loop===
<
println(" P S F"),
foreach(P in 1..N, P mod 2 == 0)
Line 3,306:
end
end
end.</
===List comprehension===
<
department_numbers3(N) =>
Line 3,317:
F in 1..N,
F != P, F != S, P + S + F == 12],
println(map(L,join).join("\n")).</
===Prolog style===
{{trans|Prolog}}
<
println("P F S"),
assign(Police, Fire, Sanitation),
Line 3,337:
police(A), fire(B), san(C),
A != B, A != C, B != C,
12 is A + B + C.</
=={{header|PicoLisp}}==
<
(co 'numbers
(let N 7
Line 3,359:
(<> 12 (apply + L))
(println L) ) ) ) )
(departments)</
{{out}}
<pre>
Line 3,379:
===Pilog===
<
(member @Pol (2 4 6))
(for @Fire 1 7)
Line 3,388:
(^ @
(= 12
(+ (-> @Pol) (-> @Fire) (-> @San)) ) ) )</
{{out}}
<pre>
Line 3,410:
=={{header|Prolog}}==
<
dept(X) :- between(1, 7, X).
Line 3,428:
?- main.
</syntaxhighlight>
{{Out}}
<pre>
Line 3,450:
=={{header|Python}}==
===Procedural===
<
def solve():
Line 3,462:
if __name__ == '__main__':
solve()</
{{out}}
Line 3,484:
Expressing the options directly and declaratively in terms of a ''bind'' operator, without importing ''permutations'':
{{Works with|Python|3}}
<
from itertools import (chain)
Line 3,553:
if __name__ == '__main__':
main()</
{{Out}}
<pre>('Police', 'Sanitation', 'Fire')
Line 3,577:
Nested ''bind'' (or ''concatMap'') expressions (like those above) can also be translated into list comprehension notation:
{{Works with|Python|3.7}}
<
from operator import ne
Line 3,653:
if __name__ == '__main__':
main()</
{{Out}}
<pre>('Police', 'Sanitation', 'Fire')
Line 3,674:
===Adapted from C# Example===
<
# We start with the Police Department.
# Range is the start, stop, and step. This returns only even numbers.
Line 3,690:
print("Police: ", p, " Sanitation:", f, " Fire: ", s)
</syntaxhighlight>
{{Out}}
<pre>
Line 3,713:
{{trans|Forth}}
<
[ 2drop drop ] done
dip over swap over = iff
Line 3,738:
witheach police ] is departments ( --> )
departments</
{{out}}
Line 3,759:
=={{header|R}}==
We solve this task in two lines. The rest of the code is to make the result look nice.
<
solution <- allPermutations[which(rowSums(allPermutations)==12 & apply(allPermutations, 1, function(x) !any(duplicated(x)))),]
solution <- solution[order(solution$Police, solution$Sanitation),]
row.names(solution) <- paste0("Solution #", seq_len(nrow(solution)), ":")
print(solution)</
{{out}}
Line 3,786:
We filter the Cartesian product of the lists of candidate department numbers.
<
(cons '(police fire sanitation)
(filter (λ (pfs) (and (not (check-duplicates pfs))
Line 3,792:
pfs))
(cartesian-product (range 2 8 2) (range 1 8) (range 1 8))))
</syntaxhighlight>
{{out}}
Line 3,814:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
for .permutations\ .grep(*.[0] %% 2) {
say <police fire sanitation> Z=> .list;
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 3,840:
=={{header|REXX}}==
This REXX example essentially uses brute force approach for so simple a puzzle.
<
say 'police sanitation fire' /*display simple title for the output*/
say '══════ ══════════ ════' /* " head separator " " " */
Line 3,856:
say '══════ ══════════ ════' /* " head separator " " " */
say /*stick a fork in it, we're all done. */
say # ' solutions found.' /*also, show the # of solutions found. */</
{{out|output|text= when using the default inputs:}}
<pre>
Line 3,881:
=={{header|Ring}}==
<
sanitation= 0
see "police fire sanitation" + nl
Line 3,899:
next
next
</syntaxhighlight>
Output:
<pre>
Line 3,920:
=={{header|Ruby}}==
<
(1..7).to_a.permutation(3){|p| puts p.join if p.first.even? && p.sum == 12 }
</syntaxhighlight>
{{Output}}
<pre>237
Line 3,942:
=={{header|Rust}}==
{{trans|C}}
<
fn main() {
println!("Police Sanitation Fire");
Line 3,960:
}
}
}</
=={{header|Scala}}==
<
(1 to 7).permutations.map{ n => (n(0),n(1),n(2)) }.toList.distinct // All permutations of possible department numbers
.filter{ n => n._1 % 2 == 0 } // Keep only even numbers favored by Police Chief
Line 3,973:
println( depts.mkString("\n") )
}
</syntaxhighlight>
{{output}}
<pre>(Police, Sanitation, Fire)
Line 3,994:
=={{header|Sidef}}==
{{trans|Raku}}
<
a.sum == 12 || next
a.permutations {|*b|
Line 4,000:
say (%w(police fire sanitation) ~Z b -> join(" "))
}
})</
{{out}}
<pre>
Line 4,023:
Functional approach:
<
return (1...7)
.filter({ $0 != x })
Line 4,039:
for result in res {
print(result)
}</
Iterative approach:
<
for x in [2, 4, 6] {
Line 4,059:
for result in res {
print(result)
}</
{{out}}
Line 4,082:
<b>VERSION A</b> - using procedures and list operations
<
# Procedure named ".." returns list of integers from 1 to max.
proc .. max {
Line 4,137:
}
puts "$valid valid combinations found."
</syntaxhighlight>
<b>VERSION B</b> - using simple for loops with number literals
<
set valid 0
for {set police 2} {$police <= 6} {incr police 2} {
Line 4,154:
}
puts "$valid valid combinations found."
</syntaxhighlight>
<b>VERSION C</b> - using simple for loops with number variables
<
set min 1
set max 7
Line 4,175:
puts "$valid valid combinations found."
</syntaxhighlight>
<b>VERSION D</b> - using list filter with lambda expressions
<
# Procedure named ".." returns list of integers from 1 to max.
proc .. max {
Line 4,231:
puts [join $numbersOk \n]
puts "[llength $numbersOk] valid combinations found."
</syntaxhighlight>
{{output}}
All four versions (A, B, C and D) produce the same result:
Line 4,253:
=={{header|Tiny BASIC}}==
<
PRINT "------|----------|----"
10 LET P = P + 2
Line 4,266:
IF F < 7 THEN GOTO 30
IF S < 7 THEN GOTO 20
IF P < 6 THEN GOTO 10</
=={{header|Transd}}==
<
MainModule : {
Line 4,279:
(if (eq (+ i j k) 12) (lout i " " j " " k)))))
)
}</
<pre>
Police | Sanit. | Fire
Line 4,301:
== {{header|TypeScript}} ==
{{trans|Sinclair ZX81 BASIC}}
<
// Department numbers
console.log(`POLICE SANITATION FIRE`);
Line 4,314:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 4,336:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Sub Main()
Line 4,357:
End Sub
End Module</
{{out}}
<pre>Police:2, Sanitation:3, Fire:7
Line 4,376:
=={{header|Vlang}}==
{{trans|Go}}
<
println("Police Sanitation Fire")
println("------ ---------- ----")
Line 4,392:
}
println("\n$count valid combinations")
}</
{{out}}
Line 4,417:
=={{header|VTL-2}}==
<
20 S=1
30 F=1
Line 4,432:
140 #=S<8*30
150 P=P+2
160 #=P<8*20</
{{out}}
<pre>2 3 7
Line 4,451:
=={{header|Wren}}==
{{trans|Kotlin}}
<
System.print("------ ---------- ----")
var count = 0
Line 4,467:
}
}
System.print("\n%(count) valid combinations")</
{{out}}
Line 4,494:
{{trans|Sinclair ZX81 BASIC}}
{{works with|EXPL-32}}
<
\Department numbers
code CrLf=9, IntIn=10, IntOut=11, Text=12;
Line 4,520:
end;
end
</syntaxhighlight>
{{out}}
<pre>
Line 4,541:
=={{header|zkl}}==
<
.filter(fcn(numbers){ numbers.sum(0)==12 }) // which all sum to 12 (==5)
.println();</
{{output}}
<pre>
Line 4,553:
For a table with repeated solutions using nested loops:
<
foreach p,f,s in ([2..7,2], [1..7], [1..7])
{ if((p!=s!=f) and p+f+s==12) println(p,"\t",f,"\t",s) }</
{{out}}
<pre>
|