We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

# Sequence full of colors

# Sequence full of colors

+ 0 comments f# solution

open System let rec check r g y b = function | [] -> r = g && y = b | _ when (abs (r - g) > 1) -> false | _ when (abs (y - b) > 1) -> false | 'R' :: xs -> check (r + 1) g y b xs | 'G' :: xs -> check r (g + 1) y b xs | 'Y' :: xs -> check r g (y + 1) b xs | 'B' :: xs -> check r g y (b + 1) xs [<EntryPoint>] let main _ = let n = Console.ReadLine() |> int [ 1..n ] |> List.iter (fun _ -> Console.ReadLine() |> Seq.map char |> List.ofSeq |> check 0 0 0 0 |> Console.WriteLine) 0

+ 0 comments haskell

import Control.Monad (forM_) full r g b y "" = r == g && y == b full r g b y ('R':cs) = r <= g && full (r+1) g b y cs full r g b y ('G':cs) = r >= g && full r (g+1) b y cs full r g b y ('B':cs) = y >= b && full r g (b+1) y cs full r g b y ('Y':cs) = y <= b && full r g b (y+1) cs main = do ns <- getLine forM_ [1..(read ns)] $ \ _ -> do seq <- getLine print $ full 0 0 0 0 seq

+ 0 comments Elixir solution:

defmodule Solution do def colorize(input) do cp = String.codepoints(input) Enum.reduce(cp, {0,0,0,0}, fn x, {r,g,b,y} -> case x do "R" when abs(r+1-g) < 2 -> {r+1,g,b,y} "G" when abs(g+1-r) < 2 -> {r,g+1,b,y} "B" when abs(b+1-y) < 2 -> {r,g,b+1,y} "Y" when abs(y+1-b) < 2 -> {r,g,b,y+1} _ -> {False, False, False, False} end end) |> fn {r,g,b,y} -> case {r,g,b,y} do {False, False, False, False} -> "False" _ when r-g != 0 -> "False" _ when y-b != 0 -> "False" _ -> "True" end end.() end end input = IO.read(:stdio, :all) |> String.split("\n") [_ | elements] = input Enum.map(elements, fn x -> Solution.colorize(x) end) |> Enum.each(fn x -> IO.puts(x) end)

+ 1 comment Why RYBG case test is true this does not meet.

Difference between the number of red balls and green balls in every prefix of the sequence is at most 1.

Load more conversations

Sort 35 Discussions, By:

Please Login in order to post a comment