Sort 35 Discussions, By:
Please Login in order to post a comment
main = do interact (concat . (\[p,q]->zipWith (\x y-> [x,y]) p q) . lines)
Using list comprehension in Haskell
mingle :: String -> String -> String
mingle a b = concat [[x, y] | (x, y) <- zip a b]
Thinking along the same lines here with Scala:
val List(p, q) = stdin.getLines.take(2).toList
(p, q).zipped flatMap (List(_, _))
But I still like the Haskell solution better. Comprehensions are much more intiuitive in Haskell. Comprehensions in Scala are type-homogenous, Scala 3 will hopefully take care of this. This is a topic for somewhere else though.
My Racket solution:
(define (mingle p q)
(let iter ([accu empty] [p (string->list p)] [q (string->list q)])
(if (empty? p)
(list->string (reverse accu))
(iter (cons (first q) (cons (first p) accu)) (rest p) (rest q)))))
(define p (read-line))
(define q (read-line))
(displayln (mingle p q))
Suggestions for improvement welcome.
Has anyone, using clojure, used a high order function to solve this? I was getting timeouts until I stopped using them. I'm curious to know if I'm just using the high order functions inefficiently. If you have such a solution please PM me.
Hi @fraburnham, btw you can access clojure submission in the contest here.
Same. At first, I tried reduce and map along with str but had the last test failed. The next thing I did was change from map/reduce to simple loop and recur, and everything worked fine. Still not quite certain why the latter bears lower complexity, though.
this worked for me
(fn [acc [a b]]
(conj acc a b))
(map list (read-line) (read-line)))))
This works too:
(apply str (mapcat list (read-line) (read-line)))
wanted to show my Clojure solution
[a b] (->> (mapcat list (seq a) (seq b))
(println (string-mingling (read-line) (read-line)))
mingle "" "" = ""
mingle p q = (head p) : (head q) : mingle (tail p) (tail q)
main = do
p <- getLine
q <- getLine
putStrLn $ mingle p q
did this pass? Without tail recursion, common-lisp fails at 9 and 10 (heap exhausted). And even with tail recursion, it fails at 10 because of the time limit, so I had to use a map, and no recursion, even though this is in the "recursion" section!!
Haskell solution did pass all the test cases. I don't know much about tail recursion to comment on that.