Exactly three adjacent 3 in lists: Difference between revisions

Added Easylang
(Add Draco)
(Added Easylang)
 
(19 intermediate revisions by 16 users not shown)
Line 12:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">V lists = [[9,3,3,3,2,1,7,8,5],
[5,2,9,3,3,7,8,4,1],
[1,4,3,6,7,3,8,3,2],
Line 25:
L.break
L.was_no_break
print(‘False’)</langsyntaxhighlight>
 
{{out}}
Line 37:
 
=={{header|8080 Assembly}}==
<langsyntaxhighlight lang="asm"> org 100h
jmp demo
;;; See if the list at [HL] with length DE has three
Line 91:
list3: db 1,4,3,6,7,3,8,3,2
list4: db 1,2,3,4,5,6,7,8,9
list5: db 4,6,8,7,2,3,3,3,1</langsyntaxhighlight>
{{out}}
<pre>true false false false true</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io; use Ada.Text_Io;
 
procedure Exactly_3 is
Line 156:
Test ((1 => 3)); -- One element
Test ((1 .. 0 => <>)); -- No elements
end Exactly_3;</langsyntaxhighlight>
{{out}}
<pre>
Line 176:
=={{header|ALGOL 68}}==
Including the extra test cases from the Raku and Wren samples.
<langsyntaxhighlight lang="algol68">BEGIN # test lists contain exactly 3 threes and that they are adjacent #
[]INT list1 = ( 9, 3, 3, 3, 2, 1, 7, 8, 5 ); # task test case #
[]INT list2 = ( 5, 2, 9, 3, 3, 7, 8, 4, 1 ); # " " " #
Line 208:
print( ( " ] -> ", IF list ok THEN "true" ELSE "false" FI, newline ) )
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 222:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">------- EXACTLY N INSTANCES OF N AND ALL CONTIGUOUS ------
 
-- nnPeers :: Int -> [Int] -> Bool
Line 385:
set my text item delimiters to dlm
s
end unlines</langsyntaxhighlight>
{{Out}}
<pre>[9, 3, 3, 3, 2, 1, 7, 8, 5] -> true
Line 394:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">lists := [[9, 3, 3, 3, 2, 1, 7, 8, 5]
, [5, 2, 9, 3, 3, 7, 8, 4, 1]
, [1, 4, 3, 6, 7, 3, 8, 3, 2]
Line 412:
result .= "[" L[i] "] : " (cnsctv && c=3 ? "true" : "false") "`n"
}
MsgBox % result</langsyntaxhighlight>
{{out}}
<pre>[9, 3, 3, 3, 2, 1, 7, 8, 5] : true
Line 421:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f EXACTLY_THREE_ADJACENT_3_IN_LISTS.AWK
BEGIN {
Line 435:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 444:
T 4,6,8,7,2,3,3,3,1
</pre>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|BASIC256}}===
<syntaxhighlight lang="vb">arraybase 1
dim list(5, 9)
list = {{9,3,3,3,2,1,7,8,5}, {5,2,9,3,3,7,8,4,1},{1,4,3,6,7,3,8,3,2}, {1,2,3,4,5,6,7,8,9},{4,6,8,7,2,3,3,3,1}}
 
for i = 1 to list[?][]
go = false
pass = true
c = 0
for j = 1 to list[][?]
if list[i, j] = 3 then
c+=1
go = true
else
if go = true and c <> 3 then pass = false
go = false
end if
next j
print i; " ";
if c = 3 and pass then print "true" else print "false"
next i</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">100 cls
110 data 9,3,3,3,2,1,7,8,5
120 data 5,2,9,3,3,7,8,4,1
130 data 1,4,3,6,7,3,8,3,2
140 data 1,2,3,4,5,6,7,8,9
150 data 4,6,8,7,2,3,3,3,1
160 dim lista(5,9)
170 for i = 1 to ubound(lista)
180 for j = 1 to ubound(lista,2)
190 read lista(i,j)
200 next j
210 next i
220 for i = 1 to ubound(lista)
230 go = false
240 pass = true
250 c = 0
260 for j = 1 to ubound(lista,2)
270 if lista(i,j) = 3 then
280 c = c+1
290 go = true
300 else
310 if go = true and c <> 3 then pass = false
320 go = false
330 endif
340 next j
350 print i;" ";
360 if c = 3 and pass then print "True" else print "False"
370 next i
380 end</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
==={{header|GW-BASIC}}===
{{works with|Applesoft BASIC}}
{{works with|Chipmunk Basic}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
{{works with|Quite BASIC}}
<syntaxhighlight lang="qbasic">100 CLS : rem 100 HOME for Applesoft BASIC
110 LET f = 0
115 LET t = 1
120 DATA 9,3,3,3,2,1,7,8,5
130 DATA 5,2,9,3,3,7,8,4,1
140 DATA 1,4,3,6,7,3,8,3,2
150 DATA 1,2,3,4,5,6,7,8,9
160 DATA 4,6,8,7,2,3,3,3,1
170 DIM l(5,9)
180 FOR i = 1 TO 5
190 FOR j = 1 TO 9
200 READ l(i,j)
210 NEXT j
220 NEXT i
230 FOR i = 1 TO 5
240 LET g = f
250 LET p = t
260 LET c = 0
270 FOR j = t TO 9
280 IF l(i,j) = 3 THEN LET c = c+1
281 IF l(i,j) = 3 THEN LET g = t
282 IF l(i,j) <> 3 THEN GOSUB 340
283 IF l(i,j) <> 3 THEN LET g = f
290 NEXT j
300 PRINT i; " ";
310 IF c = 3 AND p = t THEN PRINT "true"
315 IF c <> 3 OR p <> t THEN PRINT "false"
320 NEXT i
330 END
340 IF g = t AND c <> 3 THEN LET p = f
350 RETURN</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
==={{header|Minimal BASIC}}===
{{works with|QBasic}}
{{works with|QuickBasic}}
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|MSX BASIC|any}}
{{trans|Chipmunk Basic}}
<syntaxhighlight lang="qbasic">100 LET F = 0
110 LET T = 1
120 DATA 9,3,3,3,2,1,7,8,5
130 DATA 5,2,9,3,3,7,8,4,1
140 DATA 1,4,3,6,7,3,8,3,2
150 DATA 1,2,3,4,5,6,7,8,9
160 DATA 4,6,8,7,2,3,3,3,1
170 DIM L(5,9)
180 FOR I = 1 TO 5
190 FOR J = 1 TO 9
200 READ L(I,J)
210 NEXT J
220 NEXT I
230 FOR I = 1 TO 5
240 LET G = F
250 LET P = T
260 LET C = 0
270 FOR J = T TO 9
280 IF L(I,J) = 3 THEN 300
290 IF L(I,J) <> 3 THEN 330
300 LET C = C + 1
310 LET G = T
320 GOTO 390
330 IF G = T THEN 360
340 LET G = F
350 GOTO 390
360 IF C <> 3 THEN 380
370 GOTO 390
380 LET P = F
390 NEXT J
400 PRINT I; " ";
410 IF C = 3 THEN 430
420 GOTO 440
430 IF P = T THEN 460
440 IF C <> 3 THEN 480
450 IF P <> T THEN 480
460 PRINT "TRUE"
470 GOTO 490
480 PRINT "FALSE"
490 NEXT I
500 END</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
<syntaxhighlight lang="qbasic">100 CLS
110 false = 0 : true = 1
120 DATA 9,3,3,3,2,1,7,8,5
130 DATA 5,2,9,3,3,7,8,4,1
140 DATA 1,4,3,6,7,3,8,3,2
150 DATA 1,2,3,4,5,6,7,8,9
160 DATA 4,6,8,7,2,3,3,3,1
170 DIM lis(5,9)
180 FOR i = 1 TO 5
190 FOR j = 1 TO 9
200 READ lis(i,j)
210 NEXT j
220 NEXT i
230 FOR i = 1 TO 5
240 go = false
250 pass = true
260 c = 0
270 FOR j = true TO 9
280 IF lis(i,j) = 3 THEN c = c+1 : go = true ELSE GOSUB 340 : go = false
290 NEXT j
300 PRINT i;" ";
310 IF c = 3 AND pass = true THEN PRINT "true" ELSE PRINT "false"
320 NEXT i
330 END
340 IF go = true AND c <> 3 THEN pass = false
350 RETURN</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="vb">OpenConsole()
 
Dim lista.i(5, 9)
Define.b go, pass
Define.i i, j, c
 
For i = 1 To ArraySize(lista())
For j = 1 To ArraySize(lista(),2)
Read.i lista(i,j)
Next j
Next i
 
For i = 1 To ArraySize(lista())
go = #False
pass = #True
c = 0
For j = 1 To ArraySize(lista(),2)
If lista(i, j) = 3:
c + 1
go = #True
Else
If go = #True And c <> 3:
pass = #False
EndIf
go = #False
EndIf
Next j
Print(Str(i) + #TAB$)
If c = 3 And pass = #True:
PrintN("True")
Else
PrintN("False")
EndIf
Next i
 
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input()
CloseConsole()
 
DataSection
Data.i 9,3,3,3,2,1,7,8,5
Data.i 5,2,9,3,3,7,8,4,1
Data.i 1,4,3,6,7,3,8,3,2
Data.i 1,2,3,4,5,6,7,8,9
Data.i 4,6,8,7,2,3,3,3,1
EndDataSection</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">CONST False = 0: True = NOT False
 
DATA 9,3,3,3,2,1,7,8,5
DATA 5,2,9,3,3,7,8,4,1
DATA 1,4,3,6,7,3,8,3,2
DATA 1,2,3,4,5,6,7,8,9
DATA 4,6,8,7,2,3,3,3,1
DIM lista(1 TO 5, 1 TO 9) AS INTEGER
FOR i = 1 TO UBOUND(lista)
FOR j = 1 TO UBOUND(lista, 2)
READ lista(i, j)
NEXT j
NEXT i
 
FOR i = 1 TO UBOUND(lista)
go = False
pass = True
c = 0
FOR j = 1 TO UBOUND(lista, 2)
IF lista(i, j) = 3 THEN
c = c + 1
go = True
ELSE
IF go = True AND c <> 3 THEN pass = False
go = False
END IF
NEXT j
PRINT i; " ";
IF c = 3 AND pass THEN PRINT "True" ELSE PRINT "False"
NEXT i</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
==={{header|Quite BASIC}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">DIM lista(5, 9)
DATA 9, 3, 3, 3, 2, 1, 7, 8, 5
DATA 5, 2, 9, 3, 3, 7, 8, 4, 1
DATA 1, 4, 3, 6, 7, 3, 8, 3, 2
DATA 1, 2, 3, 4, 5, 6, 7, 8, 9
DATA 4, 6, 8, 7, 2, 3, 3, 3, 1
FOR i = 1 TO UBOUND(lista,1)
FOR j = 1 TO UBOUND(lista,2)
READ lista(i, j)
NEXT j
NEXT i
 
FOR i = 1 TO UBOUND(lista,1)
LET go = 0
LET pass = 1
LET c = 0
FOR j = 1 TO UBOUND(lista,2)
IF lista(i, j) = 3 THEN
LET c = c + 1
LET go = 1
ELSE
IF go = 1 AND c <> 3 THEN LET pass = 0
LET go = 0
END IF
NEXT j
PRINT i; " ";
IF c = 3 AND pass <> 0 THEN PRINT "True" ELSE PRINT "False"
NEXT i
END</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="vb">dim lista(5, 9)
data 9,3,3,3,2,1,7,8,5
data 5,2,9,3,3,7,8,4,1
data 1,4,3,6,7,3,8,3,2
data 1,2,3,4,5,6,7,8,9
data 4,6,8,7,2,3,3,3,1
 
for i = 1 to arraysize(lista(),1)
for j = 1 to arraysize(lista(),2)
read lista(i,j)
next j
next i
 
for i = 1 to arraysize(lista(),1)
go = false
pass = true
c = 0
for j = 1 to arraysize(lista(),2)
if lista(i, j) = 3 then
c = c + 1
go = true
else
if go = true and c <> 3 pass = false
go = false
end if
next j
print i, " ";
if c = 3 and pass then print "True" else print "False" : fi
next i</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdbool.h>
 
Line 482 ⟶ 822:
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>9 3 3 3 2 1 7 8 5 -> true
Line 491 ⟶ 831:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">% See if a sequence has three consecutive 3s in it
% Works for any type that can be iterated over
three_3s = proc [T: type] (seq: T) returns (bool)
Line 534 ⟶ 874:
end
end
end start_up</langsyntaxhighlight>
{{out}}
<pre>9 3 3 3 2 1 7 8 5 -> true
Line 541 ⟶ 881:
1 2 3 4 5 6 7 8 9 -> false
4 6 8 7 2 3 3 3 1 -> true</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
var ThreeList: array [0..4,0..8] of integer = (
(9,3,3,3,2,1,7,8,5),
(5,2,9,3,3,7,8,4,1),
(1,4,3,6,7,3,8,3,2),
(1,2,3,4,5,6,7,8,9),
(4,6,8,7,2,3,3,3,1));
 
 
function CountThrees(TA: array of integer): integer;
{Count the number threes in array}
var I,Cnt: integer;
begin
Result:=0;
for I:=0 to High(TA) do
if TA[I]=3 then
begin
Inc(Result);
if Result=3 then exit;
end
else Result:=0;
end;
 
 
procedure TestThreeArrays(Memo: TMemo);
var I,J: integer;
var B: boolean;
var S: string;
begin
for I:=0 to High(ThreeList) do
begin
S:='';
for J:=0 to High(ThreeList[I]) do
begin
if J>0 then S:=S+',';
S:=S+IntToStr(ThreeList[I][J])
end;
if CountThrees(ThreeList[I])=3 then S:=S+' True'
else S:=S+' False';
Memo.Lines.Add(S);
end;
end;
 
</syntaxhighlight>
{{out}}
<pre>
9,3,3,3,2,1,7,8,5 True
5,2,9,3,3,7,8,4,1 False
1,4,3,6,7,3,8,3,2 False
1,2,3,4,5,6,7,8,9 False
4,6,8,7,2,3,3,3,1 True
</pre>
 
=={{header|Draco}}==
<langsyntaxhighlight lang="draco">proc nonrec three_adjacent([*]int arr) bool:
word i, n;
i := 0;
Line 571 ⟶ 970:
if three_adjacent(list[i]) then "true" else "false" fi)
od
corp</langsyntaxhighlight>
{{out}}
<pre> 9 3 3 3 2 1 7 8 5 -> true
Line 578 ⟶ 977:
1 2 3 4 5 6 7 8 9 -> false
4 6 8 7 2 3 3 3 1 -> true</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
lists[][] = [ [ 9 3 3 3 2 1 7 8 5 ] [ 5 2 9 3 3 7 8 4 1 ] [ 1 4 3 6 7 3 8 3 2 ] [ 1 2 3 4 5 6 7 8 9 ] [ 4 6 8 7 2 3 3 3 1 ] ]
func has3adj3 l[] .
for v in l[]
if v = 3
cnt += 1
else
if cnt = 3
break 1
.
cnt = 0
.
.
return if cnt = 3
.
for i to len lists[][]
write has3adj3 lists[i][] & " "
.
</syntaxhighlight>
{{out}}
<pre>
1 0 0 0 1
</pre>
 
=={{header|F_Sharp|F#}}==
{{trans|OCaml}}
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Exactly three adjacent 3 in lists. Nigel Galloway: December 8th., 2021
let has_adjacent n x =
let n=[[9;3;3;3;2;1;7;8;5];[5;2;9;3;3;7;8;4;1];[1;4;3;6;7;3;8;3;2];[1;2;3;4;5;6;7;8;9];[4;6;8;7;2;3;3;3;1]]
let rec loop c = function
n|>List.iter(fun n->printfn "%A" (n|>List.windowed 3|>List.exists(fun(n::g::l::_)->n=3 && g=3 && l=3)))
h :: t when h = x -> loop (c+1) t
</lang>
|_ :: t -> c = n || loop 0 t
|_ -> c = n
in loop 0
[[9;3;3;3;2;1;7;8;5];[5;2;9;3;3;7;8;4;1];[1;4;3;6;7;3;8;3;2];[1;2;3;4;5;6;7;8;9];[4;6;8;7;2;3;3;3;1]]|>List.iter((has_adjacent 3 3)>>printfn "%A")
</syntaxhighlight>
{{out}}
<pre>
true
false
false
false
true
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2022-04-03}}
<syntaxhighlight lang=factor>USING: formatting generalizations kernel math.statistics
sequences.extras ;
 
: adjacent? ( seq -- ? )
[ 3 = ] arg-where differences V{ 1 1 } = ;
 
{ 9 3 3 3 2 1 7 8 5 }
{ 5 2 9 3 3 7 8 4 1 }
{ 1 4 3 6 7 3 8 3 2 }
{ 1 2 3 4 5 6 7 8 9 }
{ 4 6 8 7 2 3 3 3 1 }
 
[ dup adjacent? "%u -> %u\n" printf ] 5 napply</syntaxhighlight>
{{out}}
<pre>
{ 9 3 3 3 2 1 7 8 5 } -> t
{ 5 2 9 3 3 7 8 4 1 } -> f
{ 1 4 3 6 7 3 8 3 2 } -> f
{ 1 2 3 4 5 6 7 8 9 } -> f
{ 4 6 8 7 2 3 3 3 1 } -> t
</pre>
A somewhat simpler implementation without the fancy statistics and generalizations vocabs.
<syntaxhighlight lang=factor>USING: io kernel sequences ;
{
{ 9 3 3 3 2 1 7 8 5 }
{ 5 2 9 3 3 7 8 4 1 }
{ 1 4 3 6 7 3 8 3 2 }
{ 1 2 3 4 5 6 7 8 9 }
{ 4 6 8 7 2 3 3 3 1 }
}
[
[ [ 3 = ] count 3 = ]
[ { 3 3 3 } subseq-of? ]
bi and "true" "false" ? print
] each</syntaxhighlight>
{{out}}
<pre>
Line 595 ⟶ 1,070:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">dim as integer list(1 to 5, 1 to 9) = {_
{9,3,3,3,2,1,7,8,5}, {5,2,9,3,3,7,8,4,1},_
{1,4,3,6,7,3,8,3,2}, {1,2,3,4,5,6,7,8,9},_
Line 618 ⟶ 1,093:
print i;" ";
if c = 3 and pass then print true else print false
next i</langsyntaxhighlight>
{{out}}<pre>
1 true
Line 626 ⟶ 1,101:
5 true
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
local fn ThreeAdjacentThrees
NSUInteger i, j
CFMutableArrayRef lists = fn MutableArrayNew
MutableArrayInsertObjectAtIndex( lists, @"9,3,3,3,2,1,7,8,5", 0 )
MutableArrayInsertObjectAtIndex( lists, @"5,2,9,3,3,7,8,4,1", 1 )
MutableArrayInsertObjectAtIndex( lists, @"1,4,3,6,7,3,8,3,2", 2 )
MutableArrayInsertObjectAtIndex( lists, @"1,2,3,4,5,6,7,8,9", 3 )
MutableArrayInsertObjectAtIndex( lists, @"4,6,8,7,2,3,3,3,1", 4 )
for i = 0 to len(lists) -1
CFArrayRef tempArr = fn StringComponentsSeparatedByString( lists[i], @"," )
NSUInteger counter = 0, elements = len(tempArr) -1
for j = 0 to elements
if ( counter == 3 ) then NSLog( @"%@: TRUE — contains 3 adjacent 3s.", lists[i] )
if ( counter != 3 ) and ( j == elements )
NSLog( @"%@: FALSE — doesn't contain 3 adjacent 3s.", lists[i] )
end if
if fn StringIsEqual( tempArr[j], @"3" ) == NO then counter = 0 : continue
if fn StringIsEqual( tempArr[j], @"3" ) == YES then counter++ : continue
next
next
end fn
 
fn ThreeAdjacentThrees
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
9,3,3,3,2,1,7,8,5: TRUE — contains 3 adjacent 3s.
9,3,3,3,2,1,7,8,5: FALSE — doesn't contain 3 adjacent 3s.
5,2,9,3,3,7,8,4,1: FALSE — doesn't contain 3 adjacent 3s.
1,4,3,6,7,3,8,3,2: FALSE — doesn't contain 3 adjacent 3s.
1,2,3,4,5,6,7,8,9: FALSE — doesn't contain 3 adjacent 3s.
4,6,8,7,2,3,3,3,1: TRUE — contains 3 adjacent 3s.
</pre>
 
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 666 ⟶ 1,184:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 712 ⟶ 1,230:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Bifunctor (bimap)
import Data.List (span)
 
Line 736 ⟶ 1,254:
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[4, 6, 8, 7, 2, 3, 3, 3, 1]
]</langsyntaxhighlight>
{{Out}}
<pre>[9,3,3,3,2,1,7,8,5] -> True
Line 743 ⟶ 1,261:
[1,2,3,4,5,6,7,8,9] -> False
[4,6,8,7,2,3,3,3,1] -> True</pre>
 
=={{header|J}}==
 
For the given test cases:
 
<syntaxhighlight lang=J>lists=: >cutLF{{)n
9 3 3 3 2 1 7 8 5
5 2 9 3 3 7 8 4 1
1 4 3 6 7 3 8 3 2
1 2 3 4 5 6 7 8 9
4 6 8 7 2 3 3 3 1
}}
 
(,.~ (;:'false true')>@{~'3 3 3' +./@E.&".]) lists
true 9 3 3 3 2 1 7 8 5
false 5 2 9 3 3 7 8 4 1
false 1 4 3 6 7 3 8 3 2
false 1 2 3 4 5 6 7 8 9
true 4 6 8 7 2 3 3 3 1</syntaxhighlight>
 
However, for example, it's not clear what the result should be for an argument of 3 3 3 3 3 3 3 3 3.
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 786 ⟶ 1,325:
 
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>[9,3,3,3,2,1,7,8,5] -> true
Line 801 ⟶ 1,340:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq">def count(s): reduce s as $x (0; .+1);</langsyntaxhighlight>
'''The task'''
<langsyntaxhighlight lang="jq">def lists : [
[9,3,3,3,2,1,7,8,5],
[5,2,9,3,3,7,8,4,1],
Line 821 ⟶ 1,360:
(lists[]
| "\(.) -> \(threeConsecutiveThrees)")
</syntaxhighlight>
</lang>
{{out}}
As for [[#Wren]].
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function onlyconsecutivein(a::Vector{T}, lis::Vector{T}) where T
return any(i -> a == lis[i:i+length(a)-1], 1:length(lis)-length(a)+1) &&
all(count(x -> x == a[i], lis) == count(x -> x == a[i], a) for i in eachindex(a))
Line 850 ⟶ 1,389:
println("$needle in $haystack: ", onlyconsecutivein(needle, haystack))
end
</langsyntaxhighlight>{{out}}
<pre>
[3, 3, 3] in [9, 3, 3, 3, 2, 1, 7, 8, 5]: true
Line 864 ⟶ 1,403:
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">(# -> MemberQ[Partition[#, 3, 1], {3, 3, 3}]) & /@ {{9, 3, 3, 3, 2, 1,
7, 8, 5}, {5, 2, 9, 3, 3, 7, 8, 4, 1}, {1, 4, 3, 6, 7, 3, 8, 3,
2}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {4, 6, 8, 7, 2, 3, 3, 3,
1}} // TableForm</langsyntaxhighlight>
 
{{out}}<pre>
Line 876 ⟶ 1,415:
{4,6,8,7,2,3,3,3,1}->True
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">const Lists = [[9, 3, 3, 3, 2, 1, 7, 8, 5],
[5, 2, 9, 3, 3, 7, 8, 4, 1],
[1, 4, 3, 6, 7, 3, 8, 3, 2],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[4, 6, 8, 7, 2, 3, 3, 3, 1]]
 
func contains3Adjacent3(s: openArray[int]): bool =
if s.len < 3: return false
var count = 0
for i in 0..s.high:
if s[i] == 3:
inc count
if count == 3: return true
else:
count = 0
 
 
for list in Lists:
echo list, ": ", list.contains3Adjacent3()
</syntaxhighlight>
 
{{out}}
<pre>[9, 3, 3, 3, 2, 1, 7, 8, 5]: true
[5, 2, 9, 3, 3, 7, 8, 4, 1]: false
[1, 4, 3, 6, 7, 3, 8, 3, 2]: false
[1, 2, 3, 4, 5, 6, 7, 8, 9]: false
[4, 6, 8, 7, 2, 3, 3, 3, 1]: true
</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let has_adjacent n x =
let rec loop c = function
| h :: t when h = x -> loop (succ c) t
| _ :: t -> c = n || loop 0 t
| _ -> c = n
in loop 0
 
let list = [
[9; 3; 3; 3; 2; 1; 7; 8; 5];
[5; 2; 9; 3; 3; 7; 8; 4; 1];
[1; 4; 3; 6; 7; 3; 8; 3; 2];
[1; 2; 3; 4; 5; 6; 7; 8; 9];
[4; 6; 8; 7; 2; 3; 3; 3; 1]]
 
let () =
List.iter (fun l -> Printf.printf " %B" (has_adjacent 3 3 l)) list</syntaxhighlight>
{{out}}<pre> true false false false true</pre>
 
=={{header|Perl}}==
===Specific===
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
use strict; # https://rosettacode.org/wiki/Exactly_three_adjacent_3_in_lists
Line 897 ⟶ 1,485:
@n == 3 && $n[0] == $n[1] - 1 && $n[1] == $n[2] - 1 ? 'true' : 'false',
"\n";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 906 ⟶ 1,494:
4 6 8 7 2 3 3 3 1 => true</pre>
===General===
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 928 ⟶ 1,516:
}
print "\n";
}</langsyntaxhighlight>
{{out}}
<pre> 0x0 1x1 2x2 3x3 4x4
Line 941 ⟶ 1,529:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
Line 954 ⟶ 1,542:
<span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">}}})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<small>(Agrees with Raku and Wren with a for loop and the three extra tests)</small>
Line 967 ⟶ 1,555:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">'''N instances of N and all contiguous'''
 
from itertools import dropwhile, takewhile
Line 1,014 ⟶ 1,602:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>[9, 3, 3, 3, 2, 1, 7, 8, 5] -> True
Line 1,021 ⟶ 1,609:
[1, 2, 3, 4, 5, 6, 7, 8, 9] -> False
[4, 6, 8, 7, 2, 3, 3, 3, 1] -> True</pre>
 
=={{header|Quackery}}==
 
Includes the extra test cases from the Raku and Wren solutions.
 
<syntaxhighlight lang="Quackery"> [ [] swap witheach
[ 3 = if
[ i^ join ] ]
dup size 3 != iff
[ drop false ]
done
unpack
1 - over != iff
[ 2drop false ]
done
1 - = ] is three-threes ( [ --> b )
 
' [ [ 9 3 3 3 2 1 7 8 5 ]
[ 5 2 9 3 3 7 8 4 1 ]
[ 1 4 3 6 7 3 8 3 2 ]
[ 1 2 3 4 5 6 7 8 9 ]
[ 4 6 8 7 2 3 3 3 1 ]
[ 3 3 3 1 2 4 5 1 3 ]
[ 0 3 3 3 3 7 2 2 6 ]
[ 3 3 3 3 3 4 4 4 4 ] ]
 
witheach
[ dup echo sp
three-threes iff
[ say "true" ]
else [ say "false" ]
cr ]</syntaxhighlight>
 
{{out}}
 
<pre>[ 9 3 3 3 2 1 7 8 5 ] true
[ 5 2 9 3 3 7 8 4 1 ] false
[ 1 4 3 6 7 3 8 3 2 ] false
[ 1 2 3 4 5 6 7 8 9 ] false
[ 4 6 8 7 2 3 3 3 1 ] true
[ 3 3 3 1 2 4 5 1 3 ] false
[ 0 3 3 3 3 7 2 2 6 ] false
[ 3 3 3 3 3 4 4 4 4 ] false
</pre>
 
=={{header|Raku}}==
Generalized
<syntaxhighlight lang="raku" perl6line>for 1 .. 4 -> $n {
 
say "\nExactly $n {$n}s, and they are consecutive:";
Line 1,037 ⟶ 1,669:
[0,3,3,3,3,7,2,2,6],
[3,3,3,3,3,4,4,4,4]
}</langsyntaxhighlight>
{{out}}
<pre>Exactly 1 1s, and they are consecutive:
Line 1,081 ⟶ 1,713:
=={{header|Ring}}==
 
<langsyntaxhighlight lang="ring">
see "working..." + nl
 
Line 1,126 ⟶ 1,758:
txt = txt + "]"
see txt
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,136 ⟶ 1,768:
[4,6,8,7,2,3,3,3,1] > true
done...
</pre>
 
=={{header|RPL}}==
The program below creates a list of the positions of the number 3, then calculates the list of first differences, which must be equal to { 1 1 } if there are exactly 3 adjacent 3 in the input list.
≪ → list
≪ { } 1 list SIZE '''FOR''' j
'''IF''' list j GET 3 == '''THEN''' j + '''END NEXT'''
'''IFERR''' ΔLIST { 1 1 } == '''THEN''' DROP 0 '''END'''
≫ ≫ '<span style="color:blue">ADJ3?</span>' STO
≪ {{9,3,3,3,2,1,7,8,5}
{5,2,9,3,3,7,8,4,1}
{1,4,3,6,7,3,8,3,2}
{1,2,3,4,5,6,7,8,9}
{4,6,8,7,2,3,3,3,1}
{3,3,3,1,2,4,5,1,3}
{0,3,3,3,3,7,2,2,6}
{3,3,3,3,3,4,4,4,4}} → cases
≪ { } 1 cases SIZE FOR j cases j GET <span style="color:blue">ADJ3?’</span> + NEXT ≫ ≫ ‘<span style="color:blue">TASK</span>’ STO
{{out}}
<pre>
1: { 1 0 0 0 1 0 0 0 }
</pre>
 
=={{header|Ruby}}==
Using the Raku/Wren testset:
<langsyntaxhighlight lang="ruby">tests = [[9,3,3,3,2,1,7,8,5],
[5,2,9,3,3,7,8,4,1],
[1,4,3,6,7,3,8,3,2],
Line 1,154 ⟶ 1,808:
tests.each { |t| puts "#{t.inspect} : #{t.count(n)==n && t.each_cons(n).any?{|chunk| chunk == c }}" }
end
</syntaxhighlight>
</lang>
{{out}}
<pre>Contains exactly 1 1s, consecutive:
Line 1,193 ⟶ 1,847:
[3, 3, 3, 3, 3, 4, 4, 4, 4] : true
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func contains_n_consecutive_objs(arr, n, obj) {
 
# In Sidef >= 3.99, we can also say:
# arr.contains(n.of(obj)...)
 
arr.each_cons(n, {|*a|
if (a.all { _ == obj }) {
return true
}
})
 
return false
}
 
var lists = [
[9,3,3,3,2,1,7,8,5],
[5,2,9,3,3,7,8,4,1],
[1,4,3,6,7,3,8,3,2],
[1,2,3,4,5,6,7,8,9],
[4,6,8,7,2,3,3,3,1],
]
 
lists.each {|list|
say (list, " => ", contains_n_consecutive_objs(list, 3, 3))
}</syntaxhighlight>
{{out}}
<pre>
[9, 3, 3, 3, 2, 1, 7, 8, 5] => true
[5, 2, 9, 3, 3, 7, 8, 4, 1] => false
[1, 4, 3, 6, 7, 3, 8, 3, 2] => false
[1, 2, 3, 4, 5, 6, 7, 8, 9] => false
[4, 6, 8, 7, 2, 3, 3, 3, 1] => true
</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">
fn main() {
lists := [
[9, 3, 3, 3, 2, 1, 7, 8, 5],
[5, 2, 9, 3, 3, 7, 8, 4, 1],
[1, 4, 3, 6, 7, 3, 8, 3, 2],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[4, 6, 8, 7, 2, 3, 3, 3, 1],
[3, 3, 3, 1, 2, 4, 5, 1, 3],
[0, 3, 3, 3, 3, 7, 2, 2, 6],
[3, 3, 3, 3, 3, 4, 4, 4, 4],
]
for d := 1; d <= 4; d++ {
println("Exactly $d adjacent $d's:")
for list in lists {
mut indices := []int{}
for i, e in list {
if e == d {
indices << i
}
}
mut adjacent := false
if indices.len == d {
adjacent = true
for i in 1..indices.len {
if indices[i]-indices[i-1] != 1 {
adjacent = false
break
}
}
}
println("$list -> $adjacent")
}
println('')
}
}</syntaxhighlight>
{{out}}
<pre>Exactly three adjacent 3's:
[9, 3, 3, 3, 2, 1, 7, 8, 5] -> true
[5, 2, 9, 3, 3, 7, 8, 4, 1] -> false
[1, 4, 3, 6, 7, 3, 8, 3, 2] -> false
[1, 2, 3, 4, 5, 6, 7, 8, 9] -> false
[4, 6, 8, 7, 2, 3, 3, 3, 1] -> true
[3, 3, 3, 1, 2, 4, 5, 1, 3] -> false
[0, 3, 3, 3, 3, 7, 2, 2, 6] -> false
[3, 3, 3, 3, 3, 4, 4, 4, 4] -> false</pre>
 
=={{header|Wren}}==
{{libheader|Wren-seq}}
<langsyntaxhighlight ecmascriptlang="wren">import "./seq" for Lst
 
var lists = [
Line 1,211 ⟶ 1,950:
var condition = list.count { |n| n == 3 } == 3 && Lst.isSliceOf(list, [3, 3, 3])
System.print("%(list) -> %(condition)")
}</langsyntaxhighlight>
 
{{out}}
Line 1,226 ⟶ 1,965:
</pre>
Or, more generally, replacing everything after 'lists' with the following:
<langsyntaxhighlight ecmascriptlang="wren">for (d in 1..4) {
System.print("Exactly %(d) adjacent %(d)'s:")
for (list in lists) {
Line 1,233 ⟶ 1,972:
}
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 1,279 ⟶ 2,018:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func Check(L); \Return 'true' if three adjacent 3's
int L, C, I, J;
def Size = 9; \number of items in each List
Line 1,300 ⟶ 2,039:
CrLf(0);
];
]</langsyntaxhighlight>
 
{{out}}
2,012

edits