Exactly three adjacent 3 in lists: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Haskell}}: Settled for the simpler variant)
Line 45: Line 45:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.List (group)
<lang haskell>import Data.Bifunctor (bimap)
import Data.List (elemIndex)


nnPeers :: Int -> [Int] -> Bool
nnPeers :: Int -> [Int] -> Bool
nnPeers n xs =
nnPeers n xs = maybe False go (elemIndex n xs)
where
let p = (n ==)
in p (length (filter p xs))
p = (n ==)
&& any
go i =
((&&) . p . length <*> any p)
uncurry (&&) $
(group xs)
bimap
(all p)
(not . any p)
(splitAt n (drop i xs))


--------------------------- TEST -------------------------
--------------------------- TEST -------------------------
Line 73: Line 77:
[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>

Or (same test and results):

<lang haskell>import Data.Bifunctor (bimap)
import Data.List (elemIndex)

nnPeers :: Int -> [Int] -> Bool
nnPeers n xs = maybe False go (elemIndex n xs)
where
p = (n ==)
go i =
uncurry (&&) $
bimap
(all p)
(not . any p)
(splitAt n (drop i xs))</lang>


=={{header|Julia}}==
=={{header|Julia}}==

Revision as of 16:34, 7 December 2021

Exactly three adjacent 3 in lists is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
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]

Print 'true' if the value '3' appears in the list exactly 3 times and they are adjacent ones otherwise print 'false'.

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 (elemIndex)

nnPeers :: Int -> [Int] -> Bool nnPeers n xs = maybe False go (elemIndex n xs)

 where
   p = (n ==)
   go i =
     uncurry (&&) $
       bimap
         (all p)
         (not . any p)
         (splitAt n (drop i xs))

TEST -------------------------

main :: IO () main =

 putStrLn $
   unlines $
     (\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

Julia

This example is incorrect. Please fix the code and remove this message.

Details: Will incorrectly pass for a list with more than 3 consecutive '3's

<lang julia>function consecutivein(a::Vector{T}, lis::Vector{T}) where T

   return any(i -> a == lis[i:i+length(a)-1], 1:length(lis)-length(a)+1)

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,6,7,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: ", consecutivein(needle, haystack))

end </lang>

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
   if cnt = 1
      see "" + n + " true" + nl
   else
      see "" + n + " false" + nl
   ok

next

see "done..." + nl </lang>

Output:
working...
1 true
2 false
3 false
4 false
5 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