Semordnilap: Difference between revisions

Updated to work with Nim 1.4. Replaced "sort" with the easier "sortByIt".
(Added 11l)
(Updated to work with Nim 1.4. Replaced "sort" with the easier "sortByIt".)
Line 2,214:
<lang nim>import strutils, sequtils, sets, algorithm
 
proc reversereversed(s: string): string =
result = newString(s.len)
for i, c in s:
result[s.high - i] = c
 
let
words = readFile("unixdict.txt").strip().splitLines()
wordset = words.toSettoHashSet
revs = words.map(reversereversed)
var pairs = zip(words, revs).filterIt(it[0] < it[1] and it[1] in wordset)
 
echo "Total number of semordnilaps: ", pairs.len
pairs.sort(proc (x,y): auto = cmppairs.sortedByIt(x[0].len,yit[0].len))
echo pairs[pairs^5.high-4..pairs.high^1]</lang>
 
{{out}}
<pre>Total number of semordnilaps: 158
Anonymous user