Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,26 @@
def _insort_right(a, x, q):
"""
Insert item x in list a, and keep it sorted assuming a is sorted.
If x is already in a, insert it to the right of the rightmost x.
"""
lo, hi = 0, len(a)
while lo < hi:
mid = (lo+hi)//2
q += 1
less = input(f"{q:2}: IS {x:>6} LESS-THAN {a[mid]:>6} ? y/n: ").strip().lower() == 'y'
if less: hi = mid
else: lo = mid+1
a.insert(lo, x)
return q
def order(items):
ordered, q = [], 0
for item in items:
q = _insort_right(ordered, item, q)
return ordered, q
if __name__ == '__main__':
items = 'violet red green indigo blue yellow orange'.split()
ans, questions = order(items)
print('\n' + ' '.join(ans))

View file

@ -0,0 +1,9 @@
from functools import cmp_to_key
def user_cmp(a, b):
return int(input(f"IS {a:>6} <, ==, or > {b:>6} answer -1, 0 or 1:"))
if __name__ == '__main__':
items = 'violet red green indigo blue yellow orange'.split()
ans = sorted(items, key=cmp_to_key(user_cmp))
print('\n' + ' '.join(ans))