Semordnilap: Difference between revisions

Content added Content deleted
m (Rust - reformatted with rustfmt)
No edit summary
Line 720: Line 720:
<pre>158
<pre>158
[Tuple!(string, char[])("bag", "gab"), Tuple!(string, char[])("pat", "tap"), Tuple!(string, char[])("avis", "siva"), Tuple!(string, char[])("haw", "wah"), Tuple!(string, char[])("rot", "tor")]</pre>
[Tuple!(string, char[])("bag", "gab"), Tuple!(string, char[])("pat", "tap"), Tuple!(string, char[])("avis", "siva"), Tuple!(string, char[])("haw", "wah"), Tuple!(string, char[])("rot", "tor")]</pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| System.Classes}}
{{libheader| System.StrUtils}}
{{libheader| System.Diagnostics}}
<lang Delphi>
program Semordnilap;

{$APPTYPE CONSOLE}
{$R *.res}

uses
System.SysUtils,
System.Classes,
System.StrUtils,
System.Diagnostics;

function Sort(s: string): string;
var
c: Char;
i, j, aLength: Integer;
begin
aLength := s.Length;

if aLength = 0 then
exit('');

Result := s;

for i := 1 to aLength - 1 do
for j := i + 1 to aLength do
if result[i] > result[j] then
begin
c := result[i];
result[i] := result[j];
result[j] := c;
end;
end;

function IsAnagram(s1, s2: string): Boolean;
begin
if s1.Length <> s2.Length then
exit(False);

Result := Sort(s1) = Sort(s2);

end;

function CompareLength(List: TStringList; Index1, Index2: Integer): Integer;
begin
result := List[Index1].Length - List[Index2].Length;
if Result = 0 then
Result := CompareText(Sort(List[Index2]), Sort(List[Index1]));
end;

function IsSemordnilap(word1, word2: string): Boolean;
begin
Result := SameText(word1, ReverseString(word2));
end;

var
SemordnilapDict, Dict: TStringList;
Count, Index, i, j: Integer;
words: string;
StopWatch: TStopwatch;

begin
Randomize;
StopWatch := TStopwatch.Create;
StopWatch.Start;

Dict := TStringList.Create();
Dict.LoadFromFile('unixdict.txt');

SemordnilapDict := TStringList.Create;

Dict.CustomSort(CompareLength);

Index := Dict.Count - 1;
words := '';
Count := 1;

while Index - Count >= 0 do
begin
if IsAnagram(Dict[Index], Dict[Index - Count]) then
begin
if IsSemordnilap(Dict[Index], Dict[Index - Count]) then
begin
words := Dict[Index] + ' - ' + Dict[Index - Count];
SemordnilapDict.Add(words);
end;
Inc(Count);
end
else
begin
if Count > 2 then
for i := 1 to Count - 2 do
for j := i + 1 to Count - 1 do
begin
if IsSemordnilap(Dict[Index - i], Dict[Index - j]) then
begin
words := Dict[Index - i] + ' - ' + Dict[Index - j];
SemordnilapDict.Add(words);
end;
end;

Dec(Index, Count);
Count := 1;
end;
end;

StopWatch.Stop;

Writeln(Format('Time pass: %d ms [i7-4500U Windows 7]', [StopWatch.ElapsedMilliseconds]));

writeln(#10'Semordnilap found: ', SemordnilapDict.Count);
writeln(#10'Five random samples:'#10);

for Index := 0 to 4 do
writeln(' ', SemordnilapDict[Random(SemordnilapDict.Count)]);

SemordnilapDict.SaveToFile('Semordnilap.txt');
SemordnilapDict.Free;
Dict.Free;
Readln;
end.

</lang>

{{out}}
<pre>
Time pass: 558 ms [i7-4500U Windows 7]

Semordnilap found: 158

Five random samples:

on - no
me - em
peek - keep
ton - not
viva - aviv
</pre>


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==