packagecurly// Validate will take a string of curly brackets, check it, and determine if it is valid (balanced and in proper order)// "{}" = true// "{{}" = false// "{{}}" = truefuncValidate(curliesstring)bool{ifcurlies==""{returntrue}balanced:=0for_,curly:=rangecurlies{switchcurly{case'{':balanced++case'}':balanced--}ifbalanced<0{returnfalse}}returnbalanced==0}
curly_test.go
packagecurlyimport"testing"vartestCases=[]struct{descriptionstringinputstringexpectedbool}{{"empty string","",true,},{"simple string","{}",true,},{"incorrect curlies","{{}",false,},{"out of order curlies","{}}{",false,},{"many curlies","{}{{{}{}}}",true,},}funcTestValidate(t*testing.T){for_,test:=rangetestCases{ifresult:=Validate(test.input);result!=test.expected{t.Fatalf("FAIL: %s, Validate(%s): %v - expected %v",test.description,test.input,result,test.expected)}t.Logf("PASS: %s",test.description)}}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here we Go!... I swear I'm funny sometimes
curly.go
curly_test.go