interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / anagrams / anagrams.om

121 lines · 2595 bytes Raw Blame History
1note Rosetta Code task "Anagrams": https://rosettacode.org/wiki/Anagrams
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6note the word list (Rosetta Code reads unixdict.txt; this is a fixed sample of it), newline-terminated
7text dict "abel\nable\nbale\nbela\nelba\nalger\nglare\nlager\nlarge\nregal\nangel\nangle\ngalen\nglean\nlange\ncaret\ncarte\ncater\ncrate\ntrace\nevil\nlevi\nlive\nveil\nvile\nelan\nlane\nlean\nlena\nneal\n"
8note emit writes every byte of a to stdout; a short write is retried, an error exits 1
9fn emit a view uchar * to void
10o = ulong 0
11while lt o len a bound 4096
12k = write 1 a o sub len a o
13if le k 0
14exit 1
15end
16o = add o ulong k
17end
18end
19note key writes the letters of a sorted (insertion sort) into k; words are at most 16 letters
20fn key a view uchar * k edit uchar 16 to void
21for i ulong 16
22put k i 0
23end
24for i ulong len a
25c = get a i
26j = i
27while gt j 0 bound 16
28if le get k sub j 1 c
29break
30end
31put k j get k sub j 1
32j = sub j 1
33end
34put k j c
35end
36end
37fn same x view uchar 16 y view uchar 16 to bool
38for i ulong 16
39if ne get x i get y i
40ret false
41end
42end
43ret true
44end
45note index records each word's (start, length) and returns how many words there are
46fn index st edit ulong 64 ln edit ulong 64 to ulong
47k = ulong 0
48s = ulong 0
49for i ulong len dict
50if eq get dict i 10
51put st k s
52put ln k sub i s
53k = add k 1
54s = add i 1
55end
56end
57ret k
58end
59note count: how many words share word w's key
60fn count st view ulong 64 ln view ulong 64 n ulong w ulong to ulong
61a = new uchar 16 0
62b = new uchar 16 0
63key part dict get st w get ln w a
64c = ulong 0
65for v ulong n
66key part dict get st v get ln v b
67if same a b
68c = add c 1
69end
70end
71ret c
72end
73note first: no earlier word has word w's key
74fn first st view ulong 64 ln view ulong 64 w ulong to bool
75a = new uchar 16 0
76b = new uchar 16 0
77key part dict get st w get ln w a
78for v ulong w
79key part dict get st v get ln v b
80if same a b
81ret false
82end
83end
84ret true
85end
86note print every group of the largest size, groups by first appearance, words in list order
87fn show st view ulong 64 ln view ulong 64 n ulong best ulong to void
88a = new uchar 16 0
89b = new uchar 16 0
90for w ulong n
91if and first st ln w eq count st ln n w best
92key part dict get st w get ln w a
93f = true
94for v ulong n
95key part dict get st v get ln v b
96if same a b
97if not f
98emit sp
99end
100emit part dict get st v get ln v
101f = false
102end
103end
104emit nl
105end
106end
107end
108fn main to int
109st = new ulong 64 0
110ln = new ulong 64 0
111n = index st ln
112best = ulong 0
113for w ulong n
114c = count st ln n w
115if gt c best
116best = c
117end
118end
119show st ln n best
120ret 0
121end