Semordnilap: Difference between revisions

4,442 bytes added ,  25 days ago
SuperCollider solution to Rosetta Code TASK: Semordnilap
m (syntax highlighting fixup automation)
(SuperCollider solution to Rosetta Code TASK: Semordnilap)
 
(8 intermediate revisions by 6 users not shown)
Line 925:
(writeln 'pairs '→ (length semordnilap))
(writeln 'longest '→ (take semordnilap 5)))
</syntaxhighlight>
 
{{out}}
<pre>
(task)
pairs → 345
longest → (rengager tresser strasse reveler retrace)
</pre>
 
</syntaxhighlight>
 
=={{header|Eiffel}}==
Line 2,432 ⟶ 2,433:
drab - bard
</pre>
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">include ..\Utilitys.pmt
 
( ) ( )
 
"unixdict.txt" "r" fopen var f
 
true while
f fgets
dup -1 == if
drop
f fclose
false
else
-1 del
0 put
true
endif
endwhile
 
len while
len 1 > if
pop swap reverse find dup
if
extract rot swap 0 put swap
else
drop
endif
true
else
drop false
endif
endwhile
 
( 50 54 ) for get dup reverse print " -> " print ? endfor nl
len print " pairs" ?</syntaxhighlight>
{{out}}
<pre>dew -> wed
dial -> laid
dim -> mid
dine -> enid
dog -> god
 
158 pairs
 
=== Press any key to exit ===</pre>
 
=={{header|PHP}}==
Line 3,154 ⟶ 3,202:
 
=={{header|Seed7}}==
Note that the Seed7 program downloads unixdict.txt from the net.
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "gethttp.s7i";
 
const func string: reverse (in string: word) is func
result
var string: drow is "";
local
var integer: index is 0;
begin
for index range length(word) downto 1 do
drow &:= word[index];
end for;
end func;
 
const proc: main is func
Line 3,176 ⟶ 3,214:
var integer: count is 0;
begin
wordList := split(lower(getHttp("wwwwiki.puzzlers.org/pub/wordlists/unixdict.txt")), "\n");
for word range wordList do
drow := reverse(word);
Line 3,297 ⟶ 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,444 ⟶ 3,510:
4. ah'ha
5. al'la
</pre>
 
=={{header|Uiua}}==
For each word check its reverse is in the array, and that it's greater. This removes palindromes and also keeps only first of each pair.
 
<syntaxhighlight lang="Uiua">
&p &pf "Count: " ⧻.▽≡(×>⇌.⟜∊⇌)⟜¤. ⊜□ ≠@\n. &fras "unixdict.txt"
</syntaxhighlight>
{{out}}
<pre>
Count: 158
{"able" "abut" "ac" "ah" "al" "am" "amos" "and" "ape" "aps" "are"
...
"pot" "pow" "pus" "rat" "raw" "rot" "saw" "suez" "tort" "tv" "way"}
</pre>
 
Line 3,497 ⟶ 3,577:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">import "io" for File
 
var dict = File.read("unixdict.txt").split("\n")
Line 3,609 ⟶ 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}}==
10

edits