Data update

This commit is contained in:
Ingy döt Net 2025-08-11 18:05:26 -07:00
parent 4d5544505c
commit 4924dd0264
3073 changed files with 55820 additions and 4408 deletions

View file

@ -0,0 +1,24 @@
(import std.List)
(let gen_string (fun (length) {
(mut output "")
(while (< (len output) length) {
(set output (+ output (@ "[]" (random 0 1)))) })
output }))
(let balanced? (fun (str) {
(mut i 0)
(=
(len str)
(len
(list:takeWhile str (fun (char) {
(if (= "[" char)
(set i (+ i 1))
(set i (- i 1)))
(>= i 0) }))))}))
(list:forEach
(list:iota 0 5)
(fun (_) {
(let str (gen_string (random 2 8)))
(print (string:format "{:<8}: {:^6}" str (if (balanced? str) "OK" "NOT OK"))) }))

View file

@ -0,0 +1,12 @@
(defun BalancedBrackets (str / cntOpen curr)
(setq cntOpen 0)
(setq str (vl-string->list str))
(while (and (>= cntOpen 0) str)
(setq curr (car str) str (cdr str))
(cond
((= 91 curr) (setq cntOpen (1+ cntOpen)))
((= 93 curr) (setq cntOpen (1- cntOpen)))
)
)
(and (null str) (zerop cntOpen))
)

View file

@ -1,7 +1,3 @@
// Generate a string with N opening brackets ("[") and N closing brackets ("]"), in some arbitrary order.
// Determine whether the generated string is balanced; that is, whether it consists entirely of pairs of opening/closing brackets (in that order),
// none of which mis-nest.
import system'routines;
import extensions;
import extensions'text;
@ -10,7 +6,7 @@ randomBrackets(len)
{
if (0 == len)
{
^emptyString
^ EmptyString
}
else
{
@ -43,8 +39,8 @@ public program()
{
var str := randomBrackets(len);
console.printLine("""",str,"""",str.isBalanced ? " is balanced" : " is not balanced")
Console.printLine("""",str,"""",str.isBalanced ? " is balanced" : " is not balanced")
};
console.readChar()
Console.readChar()
}

View file

@ -0,0 +1,19 @@
read;
"[","]" { add "*"; push; }
[:space:] { clear; }
!"" {
clear; add "Sorry, only brackets [] allowed in this!\n";
print; quit;
}
parse>
pop; pop;
"[*]*","ob*ob*" { clear; add "ob*"; push; .reparse }
pop;
"[*ob*]*" { clear; add "ob*"; push; .reparse }
(eof) {
"ob*" { clear; add "brackets look balanced!\n"; print; quit; }
clear; add "brackets not balanced.\n"; print; quit;
}
push; push; push;

View file

@ -0,0 +1,36 @@
local function is_balanced(s)
if s == "" then
return true
end
local counter_left = 0 -- number of left brackets so far unmatched
for i = 1, #s do
local c = s[i]
if c == "[" then
counter_left += 1
elseif counter_left > 0 do
counter_left -= 1
else
return false
end
end
return counter_left == 0
end
print("Checking examples in task description:")
local brackets = {"", "[]", "][", "[][]", "][][", "[[][]]", "[]][[]"}
for brackets as b do
local res = is_balanced(b)
local bb = (b != "") ? b : "(empty)"
print(string.format("%-8s %s", bb, res ? "OK" : "NOT OK"))
end
print("\nChecking 7 random strings of brackets of length 8:")
math.randomseed(os.time())
for i = 1, 7 do
local b = ""
for j = 1, 8 do
b ..= (math.random(0, 1) == 0) ? "[" : "]"
end
local res = is_balanced(b)
print(string.format("%s %s", b, res ? "OK" : "NOT OK"))
end