The following CAPPLE Haskell solution yields an NZEC error when run:
import System.IO
import Control.Monad
data BinaryTree a = EmptyNode | Node a (BinaryTree a) (BinaryTree a) deriving (Show)
singleton :: a -> BinaryTree a
singleton a = Node a EmptyNode EmptyNode
insertNode :: (Ord a) => BinaryTree a -> a -> (BinaryTree a, Bool)
insertNode EmptyNode c = (singleton c, True)
insertNode (Node a tree1 tree2) c
| c < a = let newTree = insertNode tree1 c in (Node a (fst newTree) tree2, snd newTree)
| c == a = (Node c tree1 tree2, False)
| c > a = let newTree = insertNode tree2 c in (Node a tree1 (fst newTree), snd newTree)
main = do
store1 <- getLine
let numTestCases = read store1 :: Int
forM [1..numTestCases] runTest
return 0
runTest :: Int -> IO Int
runTest a = do
store2 <- getLine
let numTrees = read store2 :: Int
store3 <- getLine
let parsed = parseBinaryTree store3 (EmptyNode, 0) :: (BinaryTree Int, Int)
print $ snd parsed
return 0
parseBinaryTree :: (Read a, Ord a) => String -> (BinaryTree a, Int) -> (BinaryTree a, Int)
parseBinaryTree [] final = final
parseBinaryTree a (tree, count) =
let nativeParse = (reads a) !! 0
newTree = insertNode tree (fst nativeParse)
in parseBinaryTree (drop 1 a) (fst newTree, if snd newTree then count+1 else count)
It works on my Windows computer and I tested it on a Linuz environment at TutorailsPoint and both times, it worked with no error. Why does CodeChef return NXEC error for this solution? Help will be very appreciated!