Department numbers: Difference between revisions

m
m (→‎Using a constraint solver: use tuples instead of lists)
 
(20 intermediate revisions by 11 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 1,492 ⟶ 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,962 ⟶ 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,277 ⟶ 2,434:
4 5 3</syntaxhighlight>
 
===Alternate approachapproaches===
 
<syntaxhighlight lang="j"> (/:"#. 2|]) (#~ 12=+/"1) 1+3 comb 7 [ load'stats'
Line 2,287 ⟶ 2,444:
 
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,821 ⟶ 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 4,064 ⟶ 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,564 ⟶ 4,857:
Police:6, Sanitation:4, Fire:2</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="go">fn main() {
Line 4,641 ⟶ 4,934:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">System.print("Police Sanitation Fire")
System.print("------ ---------- ----")
var count = 0
Line 4,729 ⟶ 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,028

edits