June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,12 +1,14 @@
function balanced(str)
i = 0
for c in str
if c == '[' i +=1 elseif c == ']' i -=1 end
if i < 0 return false end
end
i == 0 ? true : false
function balancedbrackets(str::AbstractString)
i = 0
for c in str
if c == '[' i += 1 elseif c == ']' i -=1 end
if i < 0 return false end
end
return i == 0
end
brackets(n) = join(shuffle([("[]"^n)...]))
brackets(n::Integer) = join(shuffle(collect(Char, "[]" ^ n)))
map(x -> (x, balanced(x)), [brackets(i) for i = 0:8])
for (test, pass) in map(x -> (x, balancedbrackets(x)), collect(brackets(i) for i = 0:8))
@printf("%22s%10s\n", test, pass ? "pass" : "fail")
end

View file

@ -1 +1 @@
balanced(str) = foldl((x,y)->x<0? -1: x+y, 0, [(x=='[')-(x==']') for x=str])==0
balancedbrackets(str::AbstractString) = foldl((x, y) -> x < 0 ? -1 : x + y, 0, collect((x == '[') - (x == ']') for x in str)) == 0