Semordnilap: Difference between revisions
SuperCollider solution to Rosetta Code TASK: Semordnilap
Music Coder (talk | contribs) (SuperCollider solution to Rosetta Code TASK: Semordnilap) |
|||
Line 3,335:
+-------------+</syntaxhighlight>
=={{header|SuperCollider}}==▼
<syntaxhighlight lang="supercollider">(▼
var text, words, sdrow, semordnilap, selection;▼
File.use("unixdict.txt".resolveRelative, "r", { |f| x = text = f.readAllString });▼
words = text.split(Char.nl).collect { |each| each.asSymbol };▼
sdrow = text.reverse.split(Char.nl).collect { |each| each.asSymbol };▼
"There are % in unixdict.txt\n".postf(semordnilap.size);▼
"For example those, with more than 3 characters:".postln;▼
selection = semordnilap.select { |each| each.size >= 4 }.scramble.keep(4);▼
selection.do { |each| "% %\n".postf(each, each.reverse); };▼
)</syntaxhighlight>▼
▲=={{header|SuperCollider }}==
{{works with|SuperCollider|3.13.0}}
Submitted to Rosetta Code 2024-06-07 by: MusicCoder.
There are 405 in unixdict.txt▼
For example those, with more than 3 characters:▼
// ==========================================================================
// START-SuperCollider solution to Rosetta Code TASK: Semordnilap ## BY: MusicCoder : 2024-06-07 ##
// ==========================================================================
(
/*
https://rosettacode.org/wiki/Semordnilap
A semordnilap is a word (or phrase) that spells a **different word** (or phrase) backward.
"Semordnilap" is a word that itself is a semordnilap, i.e. palindromes. Example: lager and regal
This task does not consider semordnilap phrases, only single words.
Using only words from this list: unixdict.txt,
report the total number of unique semordnilap pairs, and print 5 examples.
Two matching semordnilaps, such as lager and regal, should be counted as ***one unique pair***.
*/
// open file and read all contents into a single string variable called text
▲File.use("~/rosetta/data/unixdict.txt".
// to get words - split on new-line, to get distinct convert to SET
// reverse order of all characters then split & convert as above
// get the intersection of the two sets: forward and reversed words
candidates = (words & revs).as(Array);
// use a list comprehension to build the pairs AND to drop any palindromes & revs
pairs = all {: [word, rev], word <- candidates, var rev = word.reverse, word < rev };
▲"For example those, with more than 3 characters:".postln;
// SELECT only those pairs where the word length is >= 4
// SCRAMBLE the resulting array and KEEP only 5 pairs
// PRINT each example on a new line
)
// ==========================================================================
// **END-SuperCollider solution to Rosetta Code TASK: Semordnilap ## BY: MusicCoder : 2024-06-07 ##
// ==========================================================================
{{out}}
<pre>
▲For example those, with more than 3 characters:
[ gnaw, wang ]
[ part, trap ]
[ hoop, pooh ]
[ leper, repel ]
[ aryl, lyra ]
</pre>
=={{header|Swift}}==
|