interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / patience-sort / oracle.py

23 lines · 633 bytes Raw Blame History
1# Independent reference for "Sorting algorithms/Patience sort" (Python 3, standard library only).
2# expected.txt is this program's stdout; omc never produced it.
3import heapq
4a = [4, 65, 2, -31, 0, 99, 83, 782, 1, 4, -31, 18]
5print(*a)
6piles = []
7for v in a:
8 for p in piles:
9 if p[-1] >= v:
10 p.append(v)
11 break
12 else:
13 piles.append([v])
14heap = [(p[-1], i) for i, p in enumerate(piles)]
15heapq.heapify(heap)
16out = []
17while heap:
18 v, i = heapq.heappop(heap)
19 out.append(piles[i].pop())
20 if piles[i]:
21 heapq.heappush(heap, (piles[i][-1], i))
22assert out == sorted(a)
23print(*out)