Semordnilap: Difference between revisions

Content deleted Content added
m Add note that the program downloads unixdict.txt
Midaz (talk | contribs)
→‎{{header|Uiua}}: Improved algorithm
 
(4 intermediate revisions by 3 users not shown)
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="SuperCollider">
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, revs, candidates, pairs, selection;
// open file and read all contents into a single string variable called text
File.use("~/rosetta/data/unixdict.txt".standardizePath, "r", { |f| text = f.readAllString });
// to get words - split on new-line, to get distinct convert to SET
words = text.split(Char.nl).as(Set);
// reverse order of all characters then split & convert as above
revs = text.reverse.split(Char.nl).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(pairs.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 = pairs.select { |each| each[0].size >= 4 }.scramble.keep(5);
// PRINT each example on a new line
selection.do { |each| each.postln; };"\n";
)
// ==========================================================================
// **END-SuperCollider solution to Rosetta Code TASK: Semordnilap ## BY: MusicCoder : 2024-06-07 ##
// ==========================================================================
</syntaxhighlight>
{{out}}
<pre>
There are 158 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}}==
Line 3,482 ⟶ 3,510:
4. ah'ha
5. al'la
</pre>
 
=={{header|Uiua}}==
For each word check if its reverse is known, and that it's greater. This removes palindromes and also keeps only first of each pair. Uses a map as a poor man's set for better performance.
 
<syntaxhighlight lang="Uiua">
A ← map:⊚⧻.⊜□≠@\n.&fras"unixdict.txt"
▽:⟜≡(↧⊃(<⇌.|has⇌))⊙◌°map⟜¤A
&p$"Found _ semordnilaps:"⊸⧻
</syntaxhighlight>
{{out}}
<pre>
Found 158 semordnilaps:
{"able" "abut" "ac" "ah" "al" "am" "amos" "and" "ape" "aps" "are" "ares" "aryl" "as" "at" "ate" "ave" "avid" "avis" "aviv" "avon" "bad" "bag" "ban" "bard" "bat" "bin" "bird" "bog" "bon" "brag" "bud" "burg" "bus" "but" "cal" "cam" "caw" "cit" "cos" "cram" "cup" "dam" "damon" "de" "deep" "deer" "del" "den" "dew" "dial" "dim" "dine" "dog" "don" "doom" "door" "dr" "draw" "dual" "ear" "edit" "eel" "eh" "eire" "em" "emil" "emit" "en" "enol" "eros" "even" "evil" "flog" "flow" "gal" "gar" "gas" "gel" "gem" "gnat" "gnaw" "got" "gulp" "gum" "gut" "haw" "hay" "ho" "hoop" "ir" "irs" "it" "iv" "ix" "jar" "kay" "keel" "keep" "knot" "kramer" "ku" "lager" "lap" "leer" "leon" "leper" "let" "lever" "liar" "lien" "lin" "lit" "loop" "loot" "lop" "los" "map" "mar" "mart" "mat" "may" "meet" "mit" "mn" "moor" "mot" "nap" "nat" "net" "nip" "nit" "no" "nor" "not" "nov" "now" "nu" "nut" "oat" "par" "part" "pat" "pay" "paz" "per" "pit" "pot" "pow" "pus" "rat" "raw" "rot" "saw" "suez" "tort" "tv" "way"}
</pre>
 
Line 3,647 ⟶ 3,689:
158
</pre>
 
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">dim norm$(27000), result$(270, 2)
 
print "Start reading unixdict.txt"
open "i:\unixdict.txt" for reading as #1
 
while not(eof(#1)) // read to end of file
line input #1 in_str$ // get line = word
in_str$ = trim$(in_str$) // we don//t want spaces
if len(in_str$) > 1 then // if length > 1 then reverse$
rev$ = reverse$(in_str$)
if in_str$ <> rev$ then // if in_str is not a palingdrome
count = count + 1 // increase counter
norm$(count) = in_str$ // store in the array
big$ = big$ + rev$ + " " // create big string with reverse$d words
fi
fi
wend
 
close #1
print " ... Done"
print
print "Start looking for semordnilap"
 
for i = 1 to count
for j = 1 to amount // check to avoid the double
if result$(j, 2) = norm$(i) continue
next j
j = instr(big$, " " + norm$(i) + " ")
if j <> 0 then // found one
amount = amount + 1 // increase counter
result$(amount, 1) = norm$(i) // store normal word
result$(amount, 2) = reverse$(norm$(i)) // store reverse$ word
fi
next i
 
print
print "Found", amount, " unique semordnilap pairs"
print
print "Display 5 semordnilap pairs"
print
 
count = 0
for i = 1 to amount
if len(result$(i, 1)) >= 5 then
count = count + 1
print result$(i, 1), chr$(9), result$(i, 2)
if count >= 5 break
fi
next i
end
 
sub reverse$(norm$)
local rev$, i, l
l = len(norm$) - 1
rev$ = norm$
for i = 0 to l
mid$(rev$, l - i, 1) = mid$(norm$, i, 1)
next i
return rev$
end sub</syntaxhighlight>
{{out}}
<pre>
Start reading unixdict.txt
... Done
 
Start looking for semordnilap
 
Found70 unique semordnilap pairs
 
Display 5 semordnilap pairs
 
diesel seidel
dirge ridge
gamma magma
groan organ
latus talus
 
---Program done, press RETURN---</pre>
 
=={{header|zkl}}==