Semordnilap: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
(Updated to work with Nim 1.4. Replaced "sort" with the easier "sortByIt".) |
||
Line 2,214: | Line 2,214: | ||
<lang nim>import strutils, sequtils, sets, algorithm |
<lang nim>import strutils, sequtils, sets, algorithm |
||
proc |
proc reversed(s: string): string = |
||
result = newString(s.len) |
result = newString(s.len) |
||
for i,c in s: |
for i, c in s: |
||
result[s.high - i] = c |
result[s.high - i] = c |
||
let |
let |
||
words = readFile("unixdict.txt").strip.splitLines |
words = readFile("unixdict.txt").strip().splitLines() |
||
wordset = words. |
wordset = words.toHashSet |
||
revs = words.map( |
revs = words.map(reversed) |
||
var pairs = zip(words, revs).filterIt(it[0] < it[1] and it[1] in wordset) |
var pairs = zip(words, revs).filterIt(it[0] < it[1] and it[1] in wordset) |
||
echo "Total number of semordnilaps: ", pairs.len |
echo "Total number of semordnilaps: ", pairs.len |
||
pairs |
pairs = pairs.sortedByIt(it[0].len) |
||
echo pairs[ |
echo pairs[^5..^1]</lang> |
||
{{out}} |
{{out}} |
||
<pre>Total number of semordnilaps: 158 |
<pre>Total number of semordnilaps: 158 |