Exactly three adjacent 3 in lists: Difference between revisions
Line 346: | Line 346: | ||
[1,2,3,4,5,6,7,8,9] -> false |
[1,2,3,4,5,6,7,8,9] -> false |
||
[4,6,8,7,2,3,3,3,1] -> true</pre> |
[4,6,8,7,2,3,3,3,1] -> true</pre> |
||
=={{header|jq}}== |
|||
{{works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
The test cases, and the output, are exactly as for entry at [[#Wren]]. |
|||
'''Preliminaries''' |
|||
<lang jq>def count(s): reduce s as $x (0; .+1); |
|||
# Output: a stream of arrays of length $w |
|||
def moving_window($w): |
|||
select(length >= $w) |
|||
| recurse( .[1:]; length >= $w) | .[:$w];</lang> |
|||
'''The task''' |
|||
<lang jq>def 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] |
|||
]; |
|||
def threeConsecutiveThrees: |
|||
count(.[] == 3 // empty) == 3 |
|||
and any( moving_window(3); all(.[]; . == 3)); |
|||
"Exactly three adjacent 3's:", |
|||
(lists[] |
|||
| "\(.) -> \(threeConsecutiveThrees)") |
|||
</lang> |
|||
{{out}} |
|||
As for [[#Wren]]. |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
Revision as of 10:04, 8 December 2021
- Task
Given 5 lists of ints:
list[1] = [9,3,3,3,2,1,7,8,5]
list[2] = [5,2,9,3,3,7,8,4,1]
list[3] = [1,4,3,6,7,3,8,3,2]
list[4] = [1,2,3,4,5,6,7,8,9]
list[5] = [4,6,8,7,2,3,3,3,1]
For each list, print 'true' if the list contains exactly three '3's that form a consecutive subsequence, otherwise print 'false'.
ALGOL 68
Including the extra test cases from the Raku and Wren samples. <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 ); # " " " # []INT list3 = ( 1, 4, 3, 6, 7, 3, 8, 3, 2 ); # " " " # []INT list4 = ( 1, 2, 3, 4, 5, 6, 7, 8, 9 ); # " " " # []INT list5 = ( 4, 6, 8, 7, 2, 3, 3, 3, 1 ); # " " " # []INT list6 = ( 3, 3, 3, 1, 2, 4, 5, 1, 3 ); # additional test from the Raku/Wren sample # []INT list7 = ( 0, 3, 3, 3, 3, 7, 2, 2, 6 ); # additional test from the Raku/Wren sample # []INT list8 = ( 3, 3, 3, 3, 3, 4, 4, 4, 4 ); # additional test from the Raku/Wren sample # [][]INT lists = ( list1, list2, list3, list4, list5, list6, list7, list8 ); FOR l pos FROM LWB lists TO UPB lists DO []INT list = lists[ l pos ]; INT threes := 0; # number of threes in the list # INT three pos := 0; # position of the last three in the list # BOOL list ok := FALSE; FOR e pos FROM LWB list TO UPB list DO IF list[ e pos ] = 3 THEN threes +:= 1; three pos := e pos FI OD; IF threes = 3 THEN # exactly 3 threes - check they are adjacent # list ok := ( list[ three pos - 1 ] = 3 AND list[ three pos - 2 ] = 3 ) FI; # show the result # print( ( "[" ) ); FOR e pos FROM LWB list TO UPB list DO print( ( " ", whole( list[ e pos ], 0 ) ) ) OD; print( ( " ] -> ", IF list ok THEN "true" ELSE "false" FI, newline ) ) OD
END</lang>
- Output:
[ 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
AppleScript
<lang applescript>------- EXACTLY N INSTANCES OF N AND ALL CONTIGUOUS ------
-- nnPeers :: Int -> [Int] -> Bool on nnPeers(n)
script p on |λ|(x) n = x end |λ| end script script notP on |λ|(x) n ≠ x end |λ| end script script on |λ|(xs) set {contiguous, residue} to ¬ span(p, dropWhile(notP, xs)) n = length of contiguous and ¬ all(notP, residue) end |λ| end script
end nnPeers
TEST -------------------------
on run
set xs to [¬ [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]] set p to nnPeers(3) script test on |λ|(x) showList(x) & " -> " & p's |λ|(x) end |λ| end script unlines(map(test, xs))
end run
GENERIC ------------------------
-- all :: (a -> Bool) -> [a] -> Bool on all(p, xs)
-- True if p holds for every value in xs tell mReturn(p) set lng to length of xs repeat with i from 1 to lng if not |λ|(item i of xs, i, xs) then return false end repeat true end tell
end all
-- dropWhile :: (a -> Bool) -> [a] -> [a]
-- dropWhile :: (Char -> Bool) -> String -> String
on dropWhile(p, xs)
set lng to length of xs set i to 1 tell mReturn(p) repeat while i ≤ lng and |λ|(item i of xs) set i to i + 1 end repeat end tell items i thru -1 of xs
end dropWhile
-- intercalate :: String -> [String] -> String
on intercalate(delim, xs)
set {dlm, my text item delimiters} to ¬ {my text item delimiters, delim} set s to xs as text set my text item delimiters to dlm s
end intercalate
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f -- to each element of xs. tell mReturn(f) set lng to length of xs set lst to {} repeat with i from 1 to lng set end of lst to |λ|(item i of xs, i, xs) end repeat return lst end tell
end map
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper. if script is class of f then f else script property |λ| : f end script end if
end mReturn
-- showList :: [a] -> String
on showList(xs)
"[" & intercalate(", ", map(my str, xs)) & "]"
end showList
-- span :: (a -> Bool) -> [a] -> ([a], [a])
on span(p, xs)
-- The longest (possibly empty) prefix of xs -- that contains only elements satisfying p, -- tupled with the remainder of xs. -- span(p, xs) eq (takeWhile(p, xs), dropWhile(p, xs)) script go property mp : mReturn(p) on |λ|(vs) if {} ≠ vs then set x to item 1 of vs if |λ|(x) of mp then set {ys, zs} to |λ|(rest of vs) {{x} & ys, zs} else {{}, vs} end if else {{}, {}} end if end |λ| end script |λ|(xs) of go
end span
-- str :: a -> String
on str(x)
x as string
end str
-- unlines :: [String] -> String
on unlines(xs)
-- A single string formed by the intercalation -- of a list of strings with the newline character. set {dlm, my text item delimiters} to ¬ {my text item delimiters, linefeed} set s to xs as text set my text item delimiters to dlm s
end unlines</lang>
- Output:
[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
FreeBASIC
<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},_ {4,6,8,7,2,3,3,3,1}}
dim as boolean go, pass dim as integer i, j, c
for i = 1 to 5
go = false pass = true c = 0 for j = 1 to 9 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</lang>
- Output:
1 true 2 false 3 false 4 false 5 true
Haskell
<lang haskell>import Data.Bifunctor (bimap) import Data.List (span)
nnPeers :: Int -> [Int] -> Bool nnPeers n xs =
let p x = n == x in uncurry (&&) $ bimap (p . length) (not . any p) (span p $ dropWhile (not . p) xs)
TEST -------------------------
main :: IO () main =
putStrLn $ unlines $ fmap (\xs -> show xs <> " -> " <> show (nnPeers 3 xs)) [ [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] ]</lang>
- Output:
[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
JavaScript
<lang javascript>(() => {
"use strict";
// ------- N INSTANCES OF N AND ALL CONTIGUOUS -------
// nnPeers :: Int -> [Int] -> Bool const nnPeers = n => // True if xs contains exactly n instances of n // and the instances are all contiguous. xs => { const p = x => n === x, mbi = xs.findIndex(p);
return -1 !== mbi ? (() => { const rest = xs.slice(mbi), sample = rest.slice(0, n);
return n === sample.length && ( sample.every(p) && ( !rest.slice(n).some(p) ) ); })() : false; };
// ---------------------- TEST ----------------------- const main = () => [ [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] ] .map( xs => `${JSON.stringify(xs)} -> ${nnPeers(3)(xs)}` ) .join("\n");
return main();
})();</lang>
- Output:
[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
jq
Works with gojq, the Go implementation of jq
The test cases, and the output, are exactly as for entry at #Wren. Preliminaries <lang jq>def count(s): reduce s as $x (0; .+1);
- Output: a stream of arrays of length $w
def moving_window($w):
select(length >= $w) | recurse( .[1:]; length >= $w) | .[:$w];</lang>
The task <lang jq>def 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]
];
def threeConsecutiveThrees:
count(.[] == 3 // empty) == 3 and any( moving_window(3); all(.[]; . == 3));
"Exactly three adjacent 3's:", (lists[]
| "\(.) -> \(threeConsecutiveThrees)")
</lang>
- Output:
As for #Wren.
Julia
<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))
end
needle = [3, 3, 3] for haystack in [
[9,3,3,3,2,1,7,8,5], [5,2,9,3,3,7,8,4,1], [1,4,3,3,3,3,8,3,2], [1,2,3,4,5,6,7,8,9], [4,6,8,7,2,3,3,3,1]] println("$needle in $haystack: ", onlyconsecutivein(needle, haystack))
end
needle = [3, 2, 3] for haystack in [
[9,3,3,3,2,3,7,8,5], [5,6,9,1,3,2,3,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,2,3,1]] println("$needle in $haystack: ", onlyconsecutivein(needle, haystack))
end
</lang>
- Output:
[3, 3, 3] in [9, 3, 3, 3, 2, 1, 7, 8, 5]: true [3, 3, 3] in [5, 2, 9, 3, 3, 7, 8, 4, 1]: false [3, 3, 3] in [1, 4, 3, 3, 3, 3, 8, 3, 2]: false [3, 3, 3] in [1, 2, 3, 4, 5, 6, 7, 8, 9]: false [3, 3, 3] in [4, 6, 8, 7, 2, 3, 3, 3, 1]: true [3, 2, 3] in [9, 3, 3, 3, 2, 3, 7, 8, 5]: false [3, 2, 3] in [5, 6, 9, 1, 3, 2, 3, 4, 1]: true [3, 2, 3] in [1, 4, 3, 6, 7, 3, 8, 3, 2]: false [3, 2, 3] in [1, 2, 3, 4, 5, 6, 7, 8, 9]: false [3, 2, 3] in [4, 6, 8, 7, 2, 3, 2, 3, 1]: false
Perl
<lang perl>#!/usr/bin/perl
use strict; # https://rosettacode.org/wiki/Exactly_three_adjacent_3_in_lists use warnings;
my @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]);
my $i = 3; # target value and repetition count
for my $ref ( @lists )
{ my @n = grep $ref->[$_] == $i, 0 .. $#$ref; print "@$ref => ", (@n == $i && (grep { $n[$_-1]+1 == $n[$_] } 1..$i-1) ? 'true' : 'false'), "\n"; }</lang>
- Output:
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
Phix
with javascript_semantics procedure test(integer n, sequence s) sequence f = find_all(n,s) printf(1,"%V: %t\n",{s,length(f)=n and f[$]-f[1]=n-1}) end procedure --for n=1 to 4 do for n=3 to 3 do printf(1,"\nExactly %d adjacent %d's:\n",n) papply(true,test,{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}}}) -- {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}}}) end for
- Output:
(Agrees with Raku and Wren if the commented-out lines are used)
Exactly 3 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
Python
<lang python>N instances of N and all contiguous
from itertools import dropwhile, takewhile
- nnPeers :: Int -> [Int] -> Bool
def nnPeers(n):
True if xs contains exactly n instances of n and all instances are contiguous. def p(x): return n == x
def go(xs): fromFirstMatch = list(dropwhile( lambda v: not p(v), xs )) ns = list(takewhile(p, fromFirstMatch)) rest = fromFirstMatch[len(ns):]
return p(len(ns)) and ( not any(p(x) for x in rest) )
return go
- ------------------------- TEST -------------------------
- main :: IO ()
def main():
Tests for N=3 print( '\n'.join([ f'{xs} -> {nnPeers(3)(xs)}' for xs in [ [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] ] ]) )
- MAIN ---
if __name__ == '__main__':
main()</lang>
- Output:
[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
Raku
Generalized <lang perl6>for 1 .. 4 -> $n {
say "\nExactly $n {$n}s, and they are consecutive:";
say .gist, ' ', lc (.Bag{$n} == $n) && ( so .rotor($n=>-($n - 1)).grep: *.all == $n ) for [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]
}</lang>
- Output:
Exactly 1 1s, and they are consecutive: [9 3 3 3 2 1 7 8 5] true [5 2 9 3 3 7 8 4 1] true [1 4 3 6 7 3 8 3 2] true [1 2 3 4 5 6 7 8 9] true [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 Exactly 2 2s, and they are consecutive: [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] false [4 6 8 7 2 3 3 3 1] false [3 3 3 1 2 4 5 1 3] false [0 3 3 3 3 7 2 2 6] true [3 3 3 3 3 4 4 4 4] false Exactly 3 3s, and they are consecutive: [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 Exactly 4 4s, and they are consecutive: [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] false [4 6 8 7 2 3 3 3 1] false [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] true
Ring
<lang ring> see "working..." + nl
list = List(5) list[1] = [9,3,3,3,2,1,7,8,5] list[2] = [5,2,9,3,3,7,8,4,1] list[3] = [1,4,3,6,7,3,8,3,2] list[4] = [1,2,3,4,5,6,7,8,9] list[5] = [4,6,8,7,2,3,3,3,1]
for n = 1 to 5
good = 0 cnt = 0 len = len(list[n]) for p = 1 to len if list[n][p] = 3 good++ ok next if good = 3 for m = 1 to len-2 if list[n][m] = 3 and list[n][m+1] = 3 and list[n][m+2] = 3 cnt++ ok next ok showarray(list[n]) if cnt = 1 see " > " + "true" + nl else see " > " + "false" + nl ok
next
see "done..." + nl
func showArray(array)
txt = "" see "[" for n = 1 to len(array) txt = txt + array[n] + "," next txt = left(txt,len(txt)-1) txt = txt + "]" see txt
</lang>
- Output:
working... [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 done...
Wren
<lang ecmascript>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], [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]
] System.print("Exactly three adjacent 3's:") for (list in lists) {
var threesCount = list.count { |n| n == 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>
- Output:
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
Or, more generally, replacing the above 'for' statement with this one: <lang ecmascript>for (d in 1..4) {
System.print("Exactly %(d) adjacent %(d)'s:") for (list in lists) { var dCount = list.count { |n| n == 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>
- Output:
Exactly 1 adjacent 1's: [9, 3, 3, 3, 2, 1, 7, 8, 5] -> true [5, 2, 9, 3, 3, 7, 8, 4, 1] -> true [1, 4, 3, 6, 7, 3, 8, 3, 2] -> true [1, 2, 3, 4, 5, 6, 7, 8, 9] -> true [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 Exactly 2 adjacent 2's: [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] -> false [4, 6, 8, 7, 2, 3, 3, 3, 1] -> false [3, 3, 3, 1, 2, 4, 5, 1, 3] -> false [0, 3, 3, 3, 3, 7, 2, 2, 6] -> true [3, 3, 3, 3, 3, 4, 4, 4, 4] -> false Exactly 3 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 Exactly 4 adjacent 4's: [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] -> false [4, 6, 8, 7, 2, 3, 3, 3, 1] -> false [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] -> true
XPL0
<lang 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 [C:= 0; for I:= 0 to Size-1 do
if L(I) = 3 then [C:= C+1; J:= I];
if C # 3 then return false; \must have exactly three 3's return L(J-1)=3 & L(J-2)=3; \the 3's must be adjacent ];
int List(5+1), I; [List(1):= [9,3,3,3,2,1,7,8,5];
List(2):= [5,2,9,3,3,7,8,4,1]; List(3):= [1,4,3,6,7,3,8,3,2]; List(4):= [1,2,3,4,5,6,7,8,9]; List(5):= [4,6,8,7,2,3,3,3,1]; for I:= 1 to 5 do [IntOut(0, I); Text(0, if Check(List(I)) then " true" else " false"); CrLf(0); ];
]</lang>
- Output:
1 true 2 false 3 false 4 false 5 true