On Jul 2, 2009, at 3:21 PM, Mark Volkmann wrote:
Now it is using a vector instead of a list. Too bad there is no push function in core. I'm using conj.
conj is the correct thing to use for the push operation.Here's my take on it based on yours. This bails early as soon as there is a mismatch or an illegal character:
(ns challenge (:use clojure.test)) (def lefts #{\( \[}) (def matches {\( \) \[ \]}) (defn balanced? [s] (loop [s s stack ()] (if (seq s) (let [[c & s] s] (if (lefts c) (recur s (conj stack c)) (if (= (matches (peek stack)) c) (recur s (pop stack)) false))) (empty? stack)))) (deftest balanced-test (are [text result] (= (balanced? text) result) "()" true "[]" true "([])" true "[()]" true "[]()" true "[][[([])]]" true "(" false ")" false "[" false "]" false "][" false ")(" false "( )" false "([)" false "[)]" false "([)]" false "({})" false "[())]") false) (defn main [] (run-tests 'challenge)) --Steve
smime.p7s
Description: S/MIME cryptographic signature