Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
13
Task/Balanced-brackets/Mathematica/balanced-brackets.math
Normal file
13
Task/Balanced-brackets/Mathematica/balanced-brackets.math
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
(* Generate open/close events. *)
|
||||
gen[n_] := RandomSample[Table[{1, -1}, {n}] // Flatten]
|
||||
|
||||
(* Check balance. *)
|
||||
check[lst_] := And @@ (# >= 0 & /@ Accumulate[lst])
|
||||
|
||||
(* Do task for string with n opening and n closing brackets. *)
|
||||
doString[n_] := (
|
||||
lst = gen[n];
|
||||
str = StringJoin[lst /. {1 -> "[", -1 -> "]"}];
|
||||
Print[str <> If[match[lst, 0],
|
||||
" is balanced.",
|
||||
" is not balanced."]])
|
||||
Loading…
Add table
Add a link
Reference in a new issue