Exactly three adjacent 3 in lists: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
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 446:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdbool.h>
 
Line 482:
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>9 3 3 3 2 1 7 8 5 -> true
Line 491:
 
=={{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:
end
end
end start_up</langsyntaxhighlight>
{{out}}
<pre>9 3 3 3 2 1 7 8 5 -> true
Line 543:
 
=={{header|Draco}}==
<langsyntaxhighlight lang="draco">proc nonrec three_adjacent([*]int arr) bool:
word i, n;
i := 0;
Line 571:
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 580:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Exactly three adjacent 3 in lists. Nigel Galloway: December 8th., 2021
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]]
n|>List.iter(fun n->printfn "%A" (n|>List.windowed 3|>List.exists(fun(n::g::l::_)->n=3 && g=3 && l=3)))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 595:
 
=={{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:
print i;" ";
if c = 3 and pass then print true else print false
next i</langsyntaxhighlight>
{{out}}<pre>
1 true
Line 628:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 666:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 712:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Bifunctor (bimap)
import Data.List (span)
 
Line 736:
[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 745:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 786:
 
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>[9,3,3,3,2,1,7,8,5] -> true
Line 801:
 
'''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:
(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:
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:
</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 879:
=={{header|Perl}}==
===Specific===
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
use strict; # https://rosettacode.org/wiki/Exactly_three_adjacent_3_in_lists
Line 897:
@n == 3 && $n[0] == $n[1] - 1 && $n[1] == $n[2] - 1 ? 'true' : 'false',
"\n";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 906:
4 6 8 7 2 3 3 3 1 => true</pre>
===General===
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 928:
}
print "\n";
}</langsyntaxhighlight>
{{out}}
<pre> 0x0 1x1 2x2 3x3 4x4
Line 941:
 
=={{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:
<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:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">'''N instances of N and all contiguous'''
 
from itertools import dropwhile, takewhile
Line 1,014:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>[9, 3, 3, 3, 2, 1, 7, 8, 5] -> True
Line 1,024:
=={{header|Raku}}==
Generalized
<syntaxhighlight lang="raku" perl6line>for 1 .. 4 -> $n {
 
say "\nExactly $n {$n}s, and they are consecutive:";
Line 1,037:
[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:
=={{header|Ring}}==
 
<langsyntaxhighlight lang="ring">
see "working..." + nl
 
Line 1,126:
txt = txt + "]"
see txt
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,140:
=={{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:
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,194:
</pre>
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func contains_n_consecutive_objs(arr, n, obj) {
 
# In Sidef >= 3.99, we can also say:
Line 1,218:
lists.each {|list|
say (list, " => ", contains_n_consecutive_objs(list, 3, 3))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,230:
=={{header|Vlang}}==
{{trans|go}}
<langsyntaxhighlight lang="vlang">
fn main() {
lists := [
Line 1,265:
println('')
}
}</langsyntaxhighlight>
{{out}}
<pre>Exactly three adjacent 3's:
Line 1,279:
=={{header|Wren}}==
{{libheader|Wren-seq}}
<langsyntaxhighlight lang="ecmascript">import "./seq" for Lst
 
var lists = [
Line 1,295:
var condition = list.count { |n| n == 3 } == 3 && Lst.isSliceOf(list, [3, 3, 3])
System.print("%(list) -> %(condition)")
}</langsyntaxhighlight>
 
{{out}}
Line 1,310:
</pre>
Or, more generally, replacing everything after 'lists' with the following:
<langsyntaxhighlight lang="ecmascript">for (d in 1..4) {
System.print("Exactly %(d) adjacent %(d)'s:")
for (list in lists) {
Line 1,317:
}
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 1,363:
 
=={{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,384:
CrLf(0);
];
]</langsyntaxhighlight>
 
{{out}}
10,327

edits