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,37 @@
require "io2"
local fmt = require "fmt"
-- Inserts item x in list a, and keeps it sorted assuming a is already sorted.
-- If x is already in a, inserts it to the right of the rightmost x.
local function insort_right(a, x, q)
local lo = 1
local hi = #a + 1
local yn = "ynYN":split("")
while lo < hi do
local mid = (lo + hi) // 2
++q
local prompt = string.format("%2d: Is %6s less than %6s ? y/n: ", q, x, a[mid])
local less = string.lower(io.readOpt(prompt, yn)) == "y"
if less then
hi = mid
else
lo = mid + 1
end
end
a:insert(lo, x)
return q
end
local function order(items)
local ordered = {}
local q = 0
for items as item do
q = insort_right(ordered, item, q)
end
return ordered
end
local items = "violet red green indigo blue yellow orange":split(" ")
local ordered = order(items)
print("\nThe colors of the rainbow, in sorted order, are:")
fmt.lprint(ordered)

View file

@ -0,0 +1,12 @@
ask_sort(List, Sorted) :- predsort(ask, List, Sorted).
ask(Cmp, Left, Right) :-
format("Comparing ~w with ~w: ", [Left, Right]),
get_single_char(Cmp0),
char_code(Cmp1, Cmp0),
( memberchk(Cmp1, [<, =, >])
-> Cmp = Cmp1,
writeln(Cmp1)
; format("Please answer <, = or >."),
ask(Cmp, Left, Right)
).

View file

@ -0,0 +1,15 @@
?- ask_sort(["violet", "red", "green", "indigo", "blue", "yellow", "orange"], Sorted).
Comparing red with green: <
Comparing violet with red: >
Comparing violet with green: >
Comparing indigo with blue: >
Comparing yellow with orange: >
Comparing blue with orange: >
Comparing blue with yellow: >
Comparing red with orange: <
Comparing green with orange: >
Comparing green with yellow: >
Comparing green with blue: <
Comparing violet with blue: >
Comparing violet with indigo: >
Sorted = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"].