Department numbers: Difference between revisions

m
m (syntax highlighting fixup automation)
 
(27 intermediate revisions by 13 users not shown)
Line 198:
</pre>
 
 
=={{header|ABC}}==
{{Trans|ALGOL 68}}
<syntaxhighlight lang="abc">
PUT 7 IN max.department.number
PUT 12 IN department.sum
WRITE "police sanitation fire" /
PUT 2 IN police
WHILE police <= max.department.number:
FOR sanitation IN { 1 .. max.department.number }:
IF sanitation <> police:
PUT ( department.sum - police ) - sanitation IN fire
IF fire > 0 AND fire <= max.department.number AND fire <> sanitation AND fire <> police:
WRITE police>>6, sanitation>>11, fire>>5 /
PUT police + 2 IN police</syntaxhighlight>
{{out}}
<pre>
police sanitation fire
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
</pre>
 
=={{header|Action!}}==
Line 877 ⟶ 910:
6 4 2
6 5 1</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">10 CLS
20 PRINT "--police-- --sanitation-- --fire--"
30 FOR police = 2 TO 7 STEP 2
40 FOR fire = 1 TO 7
50 sanitation = 12-police-fire
60 IF sanitation >= 1 AND sanitation <= 7 THEN
70 PRINT TAB (5)police TAB (18)fire TAB (30)sanitation
80 endif
90 NEXT fire
100 NEXT police</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">print "P S F"
 
for p = 2 to 7 step 2
 
for s = 1 to 7
 
if s <> p then
 
let f = (12 - p) - s
 
if f > 0 and f <= 7 and f <> s and f <> p then
 
print p, " ", s, " ", f
 
endif
 
endif
 
next s
 
next p
 
end</syntaxhighlight>
{{out| Output}}<pre>P S F
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</pre>
 
==={{header|Run BASIC}}===
Line 941 ⟶ 1,029:
==={{header|Minimal BASIC}}===
{{trans|Sinclair ZX81 BASIC}}
<syntaxhighlight lang="gwbasicbasic">
10 REM Department numbers
20 PRINT "POLICE SANITATION FIRE"
30 FOR P = 2 TO 7 STEP 2
40 FOR S = 1 TO 7
50 IF S = P THEN 90120
60 LET F = (12-P)-S
70 IF F <= 0 OR F > 7 OR F = S OR F = P THEN 90120
80 IF F > 7 THEN 120
80 PRINT TAB(3); P; TAB(11); S; TAB(19); F
90 NEXTIF F = S THEN 120
100 NEXTIF F = P THEN 120
110 PRINT TAB(3); P; TAB(11); S; TAB(19); F
110 END
120 NEXT S
130 NEXT P
140 END
</syntaxhighlight>
 
==={{header|Quite BASIC}}===
{{trans|Sinclair ZX81 BASIC}}
<syntaxhighlight lang="basic">
10 REM Department numbers
20 PRINT "POLICE SANITATION FIRE"
30 FOR P = 2 TO 7 STEP 2
40 FOR S = 1 TO 7
50 IF S = P THEN 80
60 LET F = (12-P)-S
70 IF F > 0 AND F <= 7 AND F <> S AND F <> P THEN PRINT " ";P;" ";S;" ";F
80 NEXT S
90 NEXT P
100 END
</syntaxhighlight>
{{out}}
<pre>
POLICE SANITATION FIRE
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
</pre>
 
==={{header|Sinclair ZX81 BASIC}}===
Line 1,456 ⟶ 1,580:
6 4 2
6 5 1</pre>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight lang="easylang">
numfmt 0 3
for pol = 2 step 2 to 6
for san = 1 to 7
for fire = 1 to 7
if pol <> san and san <> fire and fire <> pol
if pol + fire + san = 12
print pol & san & fire
.
.
.
.
.
</syntaxhighlight>
 
=={{header|Elixir}}==
Line 1,926 ⟶ 2,067:
6 4 2
6 5 1 </pre>
 
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
void local fn DepartmentNumbers
long police, sanitation, fire
printf @"Police Sanitation Fire"
printf @"-------------------------------"
for police = 2 to 7 step 2
for fire = 1 to 7
if ( fire = police ) then continue
sanitation = 12 - police - fire
if ( sanitation == fire ) or ( sanitation == police ) then continue
if ( sanitation >= 1 ) and ( sanitation <= 7 )
printf @"%4d%12d%13d", police, fire, sanitation
end if
next
next
end fn
 
window 1
 
fn DepartmentNumbers
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Police Sanitation Fire
-------------------------------
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
</pre>
 
 
 
=={{header|Gambas}}==
Line 2,241 ⟶ 2,434:
4 5 3</syntaxhighlight>
 
===Alternate approachapproaches===
 
<syntaxhighlight lang="j"> (/:"#. 2|]) (#~ 12=+/"1) 1+3 comb 7 [ load'stats'
Line 2,250 ⟶ 2,443:
4 3 5</syntaxhighlight>
 
Note that we are only showing the distinct valid [[combinations]] here, not all valid [[permutations]] of those combinations. (Valid permutations would be: swapping the last two values in a combination, and all permutations of 2 4 6.)
 
Another variation would be more constraint based, blindly implementing the rules of the task and yielding all valid permutations. (Shown here with the number of possibilities at each step):
 
<syntaxhighlight lang=J> NB. 3 departments, 1..7 in each
#rule1=. >,{3#<1+i.7
343
NB. total must be 12, numbers must be unique
#rule2=. (#~ ((3=#@~.) * 12=+/)"1) rule1
30
NB. no odd numbers in police department (first department)
#rule3=. (#~ 0=2|{."1) rule2
14
rule3
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</syntaxhighlight>
 
=={{header|Java}}==
Line 2,785 ⟶ 3,005:
<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},
{4, 6, 2}, {4, 7, 1}, {6, 1, 5}, {6, 2, 4}, {6, 4, 2}, {6, 5, 1}}</pre>
 
 
=={{header|MATLAB}}==
<syntaxhighlight lang="MATLAB">
% Execute the functions
clear all;close all;clc;
sol = findsolution();
disp(table(sol(:, 1), sol(:, 2), sol(:, 3), 'VariableNames',{'Pol.','Fire','San.'}))
 
function sol = findsolution()
rng = 1:7;
sol = [];
for p = rng
for f = rng
for s = rng
if p ~= s && s ~= f && f ~= p && p + s + f == 12 && mod(p, 2) == 0
sol = [sol; p s f];
end
end
end
end
end
</syntaxhighlight>
{{out}}
<pre>
Pol. Fire San.
____ ____ ____
 
2 7 3
2 6 4
2 4 6
2 3 7
4 7 1
4 6 2
4 5 3
4 3 5
4 2 6
4 1 7
6 5 1
6 4 2
6 2 4
6 1 5
</pre>
 
=={{header|MiniScript}}==
{{trans|Kotlin}}
<syntaxhighlight lang="miniscript">print "Police Sanitation Fire"
print "------ ---------- ----"
count = 0
for h in range(1, 3)
i = h * 2
for j in range(1, 7)
if j != i then
for k in range(1, 7)
if k != i and k != j and i + j + k == 12 then
print " " + i + " " + j + " " + k
count += 1
end if
end for
end if
end for
end for
print char(10) + count + " valid combinations"</syntaxhighlight>
 
{{out}}
<pre>Police Sanitation Fire
------ ---------- ----
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 valid combinations
</pre>
 
=={{header|Modula-2}}==
Line 3,446 ⟶ 3,750:
6 4 2
6 5 1
</pre>
 
=={{header|PL/M}}==
{{Trans|ALGOL W}}
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="plm">
100H: /* SHOW POSSIBLE DEPARTMENT NUMBERS FOR POLICE, SANITATION AND FIRE */
/* THE POLICE DEPARTMENT NUMBER MUST BE EVEN, ALL DEPARTMENT NUMBERS */
/* MUST BE IN THE RANGE 1 .. 7 AND THE NUMBERS MUST SUM TO 12 */
 
/* CP/M SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
PR$NUMBER: PROCEDURE( N ); /* PRINTS A NUMBER IN THE MINIMUN FIELD WIDTH */
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR ( 6 )BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
 
/* TASK */
DECLARE MAX$DEPARTMENT$NUMBER LITERALLY '7';
DECLARE DEPARTMENT$SUM LITERALLY '12';
DECLARE ( POLICE, SANITATION, FIRE ) BYTE;
 
CALL PR$STRING( .'POLICE SANITATION FIRE$' );
CALL PR$NL;
 
DO POLICE = 2 TO MAX$DEPARTMENT$NUMBER BY 2;
DO SANITATION = 1 TO MAX$DEPARTMENT$NUMBER;
IF SANITATION <> POLICE THEN DO;
FIRE = ( DEPARTMENT$SUM - POLICE ) - SANITATION;
IF FIRE <= MAX$DEPARTMENT$NUMBER
AND FIRE <> SANITATION
AND FIRE <> POLICE
THEN DO;
CALL PR$STRING( .' $' ); CALL PR$NUMBER( POLICE );
CALL PR$STRING( .' $' ); CALL PR$NUMBER( SANITATION );
CALL PR$STRING( .' $' ); CALL PR$NUMBER( FIRE );
CALL PR$NL;
END;
END;
END;
END;
 
EOF
</syntaxhighlight>
{{out}}
<pre>
POLICE SANITATION FIRE
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
</pre>
 
Line 3,708 ⟶ 4,084:
Police: 6 Sanitation: 4 Fire: 2
</pre>
 
 
===Using a constraint solver===
A problem this trivial is amenable to brute-force solutions such as the above, but it is a good example of the type of problem for which a constraint solver can be useful. This is how one could solve it using the `python-constraint` library:
 
{{libheader|python-constraint}}
<syntaxhighlight lang="python">import constraint
 
depts = ( 'police', 'sanitation', 'fire' )
 
p = constraint.Problem()
 
for var in depts:
p.addVariable(var, range(1,8))
 
p.addConstraint(constraint.AllDifferentConstraint())
p.addConstraint(lambda *vars: sum(vars)==12, depts)
p.addConstraint(lambda p: p%2==0, ('police',))
 
for s in p.getSolutions():
print(s)</syntaxhighlight>
 
{{Out}}
<pre>{'police': 6, 'fire': 5, 'sanitation': 1}
{'police': 6, 'fire': 4, 'sanitation': 2}
{'police': 6, 'fire': 2, 'sanitation': 4}
{'police': 6, 'fire': 1, 'sanitation': 5}
{'police': 4, 'fire': 6, 'sanitation': 2}
{'police': 4, 'fire': 7, 'sanitation': 1}
{'police': 4, 'fire': 5, 'sanitation': 3}
{'police': 4, 'fire': 3, 'sanitation': 5}
{'police': 4, 'fire': 2, 'sanitation': 6}
{'police': 4, 'fire': 1, 'sanitation': 7}
{'police': 2, 'fire': 4, 'sanitation': 6}
{'police': 2, 'fire': 6, 'sanitation': 4}
{'police': 2, 'fire': 7, 'sanitation': 3}
{'police': 2, 'fire': 3, 'sanitation': 7}</pre>
 
=={{header|Quackery}}==
Line 3,919 ⟶ 4,332:
</pre>
 
=={{header|RPL}}==
≪ { }
<span style="color:red">2 6</span> '''FOR''' police
<span style="color:red">1 7</span> '''FOR''' sanitation
'''IF''' police sanitation ≠ '''THEN'''
<span style="color:red">12</span> police - sanitation -
'''IF''' DUP <span style="color:red">0</span> >
OVER <span style="color:red">7</span> ≤ AND
OVER police ≠ AND
OVER sanitation ≠ AND
'''THEN'''
police sanitation ROT
<span style="color:red">3</span> →ARRY +
'''ELSE''' DROP '''END'''
'''END'''
'''NEXT'''
<span style="color:red">2</span> '''STEP'''
DUP SIZE
≫ '<span style="color:blue">PSF</span>' STO
 
{{out}}
<pre>
2: { [ 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 ] }
1: 14
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">
Line 4,333 ⟶ 4,771:
6 5 1
</pre>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
{{works with|Korn Shell}}
{{works with|Z Shell}}
<syntaxhighlight lang="bash">function main {
set -- Police Sanitation Fire
typeset -i pw=${#1} sw=${#2} fw=${#3}
printf '%s' "$1"
shift
printf '\t%s' "$@"
printf '\n'
for (( p=2; p<8; p+=2 )); do
for (( s=1; s<8; ++s )); do
if (( s == p )); then
continue
fi
(( f = 12 - p - s ))
if (( f == s || f == p || f < 1 || f > 7 )); then
continue
fi
printf "%${pw}d\t%${sw}d\t%${fw}d\n" "$p" "$s" "$f"
done
done
}
 
main "$@"</syntaxhighlight>
 
{{Out}}
<pre>Police Sanitation Fire
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</pre>
 
 
=={{header|Visual Basic .NET}}==
Line 4,374 ⟶ 4,857:
Police:6, Sanitation:4, Fire:2</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="go">fn main() {
Line 4,451 ⟶ 4,934:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">System.print("Police Sanitation Fire")
System.print("------ ---------- ----")
var count = 0
Line 4,539 ⟶ 5,022:
6 5 1
</pre>
 
=={{header|Zig}}==
{{works with|Zig|0.11.0dev}}
<syntaxhighlight lang="zig">const std = @import("std");</syntaxhighlight>
<syntaxhighlight lang="zig">pub fn main() !void {
const stdout = std.io.getStdOut().writer();
 
try stdout.writeAll("Police Sanitation Fire\n");
try stdout.writeAll("------ ---------- ----\n");
 
var p: usize = 2;
while (p <= 7) : (p += 2)
for (1..7 + 1) |s|
for (1..7 + 1) |f|
if (p != s and s != f and f != p and p + f + s == 12) {
try stdout.print(" {d} {d} {d}\n", .{ p, s, f });
};
}</syntaxhighlight>
{{out}}
<pre>Police Sanitation Fire
------ ---------- ----
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</pre>
===Zig using an iterator===
{{works with|Zig|0.11.0}}
Using a Zig ''struct'' to create an iterator is a common pattern in Zig.
<syntaxhighlight lang="zig">
const std = @import("std");
 
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
 
try stdout.writeAll("Police Sanitation Fire\n");
try stdout.writeAll("------ ---------- ----\n");
 
var it = SolutionIterator.init();
while (it.next()) |solution| {
try stdout.print(
" {d} {d} {d}\n",
.{ solution.police, solution.sanitation, solution.fire },
);
}
}
 
/// 3 bit unsigned (u3) limits 0 <= department <= 7
const Departments = packed struct {
police: u3,
sanitation: u3,
fire: u3,
};
 
const DepartmentsUnion = packed union {
departments: Departments,
together: u9,
};
 
const SolutionIterator = struct {
// police is initialized to one as adding one is the first operation in next()
// with the result .police == 2 (an even number) on the first pass.
u: DepartmentsUnion = .{ .departments = .{ .police = 1, .sanitation = 1, .fire = 1 } },
 
/// init() returns an initialised structure.
/// Using init() is a common Zig pattern.
fn init() SolutionIterator {
return SolutionIterator{};
}
 
fn next(self: *SolutionIterator) ?Departments {
if (self.u.together == 0) return null; // already completed
 
while (true) {
const ov = @addWithOverflow(self.u.together, 1);
if (ov[1] == 1) {
self.u.together = 0;
return null; // overflowed, completed
} else {
self.u.together = ov[0];
// None can be zero
if (self.u.departments.police == 0) self.u.departments.police = 2; // even
if (self.u.departments.sanitation == 0) self.u.departments.sanitation = 1;
if (self.u.departments.fire == 0) self.u.departments.fire = 1;
// Police must be even
if (self.u.departments.police & 1 == 1)
continue;
// No two can be the same
if (self.u.departments.police == self.u.departments.sanitation) continue;
if (self.u.departments.sanitation == self.u.departments.fire) continue;
if (self.u.departments.fire == self.u.departments.police) continue;
// Must total twelve (maximum sum 7 + 7 + 7 = 21 requires 5 bits)
const p = @as(u5, self.u.departments.police);
const s = @as(u5, self.u.departments.sanitation);
const f = @as(u5, self.u.departments.fire);
if (p + s + f != 12)
continue;
 
return self.u.departments;
}
}
}
};
</syntaxhighlight>
 
=={{header|zkl}}==
3,021

edits