Exactly three adjacent 3 in lists: Difference between revisions

→‎{{header|Wren}}: Simplified both versions.
m (→‎{{header|Perl}}: fixed bug, added test case)
(→‎{{header|Wren}}: Simplified both versions.)
Line 664:
 
=={{header|Wren}}==
{{libheader|Wren-seq}}
<lang ecmascript>varimport lists"./seq" =for [Lst
 
var lists = [
[9,3,3,3,2,1,7,8,5],
[5,2,9,3,3,7,8,4,1],
Line 676 ⟶ 679:
System.print("Exactly three adjacent 3's:")
for (list in lists) {
var threesCountcondition = list.count { |n| n == 3 } == 3 && Lst.isSliceOf(list, [3, 3, 3])
var condition = false
if (threesCount == 3) {
var i = list.indexOf(3)
condition = list[i+1] == 3 && list[i+2] == 3
}
System.print("%(list) -> %(condition)")
}</lang>
Line 697 ⟶ 695:
[3, 3, 3, 3, 3, 4, 4, 4, 4] -> false
</pre>
Or, more generally, replacing theeverything aboveafter 'forlists' statement with thisthe onefollowing:
<lang ecmascript>for (d in 1..4) {
System.print("Exactly %(d) adjacent %(d)'s:")
for (list in lists) {
var dCountcondition = list.count { |n| n == d } == d && Lst.isSliceOf(list, [d] * d)
var condition = false
if (dCount == d) {
if (d == 1) {
condition = true
} else {
var i = list.indexOf(d)
condition = list[i+1] == d
if (d > 2) condition = condition && list[i+2] == d
if (d > 3) condition = condition && list[i+3] == d
}
}
System.print("%(list) -> %(condition)")
}
System.print()}</lang>
}</lang>
 
{{out}}
9,485

edits