Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
22
Task/Balanced-brackets/F-Sharp/balanced-brackets.fs
Normal file
22
Task/Balanced-brackets/F-Sharp/balanced-brackets.fs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
let isBalanced str =
|
||||
let rec loop count = function
|
||||
| ']'::_ when count = 0 -> false
|
||||
| '['::xs -> loop (count+1) xs
|
||||
| ']'::xs -> loop (count-1) xs
|
||||
| [] -> count = 0
|
||||
| _::_ -> false
|
||||
|
||||
str |> Seq.toList |> loop 0
|
||||
|
||||
|
||||
let shuffle arr =
|
||||
let rnd = new System.Random()
|
||||
Array.sortBy (fun _ -> rnd.Next()) arr
|
||||
|
||||
let generate n =
|
||||
new string( String.replicate n "[]" |> Array.ofSeq |> shuffle )
|
||||
|
||||
|
||||
for n in 1..10 do
|
||||
let s = generate n
|
||||
printfn "\"%s\" is balanced: %b" s (isBalanced s)
|
||||
Loading…
Add table
Add a link
Reference in a new issue