Balanced brackets: Difference between revisions

Content added Content deleted
m (Reduced some redundant expressions in the section of Common Lisp)
Line 1,722: Line 1,722:
<lang lisp>
<lang lisp>
(defun string-of-brackets (n)
(defun string-of-brackets (n)
(let ((result (make-string (* 2 n)))
(let* ((len (* 2 n))
(opening n)
(res (make-string len))
(closing n))
(opening (/ len 2))
(dotimes (i (* 2 n) result)
(closing (/ len 2)))
(setf (aref result i)
(dotimes (i len res)
(cond
(setf (aref res i)
((zerop opening) #\])
(cond ((zerop opening) #\])
((zerop closing) #\[)
((zerop closing) #\[)
(t (if (= (random 2) 0)
(t (if (= (random 2) 0)
(progn (decf opening) #\[)
(progn (decf opening) #\[)
(progn (decf closing) #\]))))))))
(progn (decf closing) #\]))))))))


(defun balancedp (string)
(defun balancedp (string)