Semordnilap: Difference between revisions

1,460 bytes added ,  23 days ago
SuperCollider solution to Rosetta Code TASK: Semordnilap
(SuperCollider solution to Rosetta Code TASK: Semordnilap)
 
Line 3,335:
+-------------+</syntaxhighlight>
 
=={{header|SuperCollider}}==
{{incorrect|SuperCollider|The number of pairs should be 158.}}
<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 };
semordnilap = sect(words, sdrow); // converted to symbols so intersection is possible
semordnilap = semordnilap.collect { |each| each.asString };
"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 }}==
Answers:
{{works with|SuperCollider|3.13.0}}
<syntaxhighlight lang="supercollider">
Submitted to Rosetta Code 2024-06-07 by: MusicCoder.
There are 405 in unixdict.txt
<syntaxhighlight lang="supercolliderSuperCollider">(
For example those, with more than 3 characters:
// ==========================================================================
live evil
// START-SuperCollider solution to Rosetta Code TASK: Semordnilap ## BY: MusicCoder : 2024-06-07 ##
tram mart
// ==========================================================================
drib bird
(
eros sore
/*
</syntaxhighlight>
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.
This seems wrong, but perhaps the test file has changed?
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***.
*/
 
var text, words, sdrowrevs, semordnilapcandidates, pairs, selection;
// open file and read all contents into a single string variable called text
File.use("~/rosetta/data/unixdict.txt".resolveRelativestandardizePath, "r", { |f| x = text = f.readAllString });
// to get words - split on new-line, to get distinct convert to SET
words = text.split(Char.nl).collect { |each| each.asSymbol }as(Set);
// reverse order of all characters then split & convert as above
sdrowrevs = text.reverse.split(Char.nl).collect { |each| each.asSymbol }as(Set);
// 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 };
 
"There are % semordnilas in unixdict.txt\n".postf(semordnilappairs.size);
"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
selection = semordnilappairs.select { |each| each[0].size >= 4 }.scramble.keep(45);
// PRINT each example on a new line
selection.do { |each| "% %\n".postf(each, each.reverse)postln; };"\n";
)
// ==========================================================================
// **END-SuperCollider solution to Rosetta Code TASK: Semordnilap ## BY: MusicCoder : 2024-06-07 ##
// ==========================================================================
)</syntaxhighlight>
{{out}}
<pre>
There are 405158 semordnilas in unixdict.txt
For example those, with more than 3 characters:
[ gnaw, wang ]
[ part, trap ]
[ hoop, pooh ]
[ leper, repel ]
[ aryl, lyra ]
</pre>
 
=={{header|Swift}}==
10

edits