How to resolve the algorithm Balanced brackets step by step in the EchoLisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Balanced brackets step by step in the EchoLisp programming language

Table of Contents

Problem Statement

Task:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Balanced brackets step by step in the EchoLisp programming language

Source code in the echolisp programming language

(define (balance str)
	(for/fold (closed 0) ((par str))
	 #:break (< closed 0 ) => closed
	(+ closed
	(cond 
		((string=? par "[")  1)
		((string=? par "]") -1)
		(else 0)))))
		
(define (task N)
(define str (list->string (append (make-list N "[") (make-list N "]"))))
	(for ((i 10))
	(set! str (list->string (shuffle (string->list str))))
	(writeln  (if (zero? (balance str)) '👍  '❌ ) str)))

(task 4)

❌     "[]]][[]["    
❌     "]][][[[]"    
❌     "][[[]]]["    
👍     "[][[[]]]"    
❌     "]][[][]["    
❌     "][][[[]]"    
👍     "[][][[]]"    
❌     "]][[][[]"    
❌     "[[]]][[]"    
❌     "[[][]]]["


  

You may also check:How to resolve the algorithm Binary strings step by step in the Elixir programming language
You may also check:How to resolve the algorithm Numeric error propagation step by step in the Racket programming language
You may also check:How to resolve the algorithm Digital root/Multiplicative digital root step by step in the J programming language
You may also check:How to resolve the algorithm Runge-Kutta method step by step in the jq programming language
You may also check:How to resolve the algorithm Attractive numbers step by step in the V (Vlang) programming language