Department numbers: Difference between revisions

m
m (→‎{{header|Zig}}: writer.print() is void (no return))
 
(8 intermediate revisions by 5 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 2,972 ⟶ 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,215 ⟶ 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,792 ⟶ 4,934:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">System.print("Police Sanitation Fire")
System.print("------ ---------- ----")
var count = 0
Line 4,915 ⟶ 5,057:
6 4 2
6 5 1</pre>
===Creating andZig 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");</syntaxhighlight>
const std = @import("std");
<syntaxhighlight lang="zig">pub fn main() !void {
 
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
 
Line 4,924 ⟶ 5,069:
try stdout.writeAll("------ ---------- ----\n");
 
var it = SolutionIterator{}.init();
while (it.next()) |solution| {
try stdout.print(
Line 4,931 ⟶ 5,076:
);
}
}
}</syntaxhighlight>
 
<syntaxhighlight lang="zig">const SolutionIterator = struct {
/// 53 bit unsigned (u5u3) allowslimits 0 addition<= updepartment to<= 317
const Departments = packed struct {
p: u5 = 2,
spolice: u5 = 1u3,
fsanitation: u5 = 0u3,
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 {
/// 3 bit unsigned (u3) limits 0 <= department <= 7
if (self.u.together == 0) return null; // already completed
fn next(self: *SolutionIterator) ?struct { police: u3, sanitation: u3, fire: u3 } {
if (self.p > 7) return null; // already completed
 
while (true) {
self.fconst ov += @addWithOverflow(self.u.together, 1); // fire
if (self.fov[1] >== 71) {
self.fu.together = 10;
self.sreturn += 1null; // sanitationoverflowed, completed
} if (self.s > 7)else {
self.su.together = 1ov[0];
// None can be self.p += 2; // policezero
if (self.pu.departments.police >== 70) {self.u.departments.police = 2; // even
if (self.u.departments.sanitation == 0) self.u.departments.sanitation = 1;
return null; // completed
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;
}
if (self.p + self.f + self.s == 12)
return .{
.police = @truncate(self.p),
.sanitation = @truncate(self.s),
.fire = @truncate(self.f),
};
}
}
};
};</syntaxhighlight>
</syntaxhighlight>
 
=={{header|zkl}}==
3,026

edits