Eertree: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: added syntax colouring, made p2js compatible)
m (syntax highlighting fixup automation)
Line 22: Line 22:
{{trans|D}}
{{trans|D}}


<lang 11l>T Node
<syntaxhighlight lang="11l">T Node
Int length
Int length
Int suffix
Int suffix
Line 86: Line 86:


V tree = eertree(‘eertree’)
V tree = eertree(‘eertree’)
print(subPalindromes(tree))</lang>
print(subPalindromes(tree))</syntaxhighlight>


{{out}}
{{out}}
Line 95: Line 95:
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{trans|Java}}
{{trans|Java}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;


Line 190: Line 190:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>[ee, e, r, t, rtr, ertre, eertree]</pre>
<pre>[ee, e, r, t, rtr, ertre, eertree]</pre>
Line 196: Line 196:
=={{header|C++}}==
=={{header|C++}}==
{{trans|D}}
{{trans|D}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <functional>
#include <functional>
#include <map>
#include <map>
Line 316: Line 316:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>[ee, e, r, t, rtr, ertre, eertree]</pre>
<pre>[ee, e, r, t, rtr, ertre, eertree]</pre>
Line 322: Line 322:
=={{header|D}}==
=={{header|D}}==
{{trans|Go}}
{{trans|Go}}
<lang D>import std.array;
<syntaxhighlight lang="d">import std.array;
import std.stdio;
import std.stdio;


Line 393: Line 393:
}
}
return s.data;
return s.data;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>["ee", "e", "r", "t", "rtr", "ertre", "eertree"]</pre>
<pre>["ee", "e", "r", "t", "rtr", "ertre", "eertree"]</pre>


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 471: Line 471:
}
}
return
return
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 479: Line 479:
=={{header|Java}}==
=={{header|Java}}==
{{trans|D}}
{{trans|D}}
<lang Java>import java.util.ArrayList;
<syntaxhighlight lang="java">import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashMap;
import java.util.List;
import java.util.List;
Line 569: Line 569:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>[ee, r, t, rtr, ertre, eertree, e]</pre>
<pre>[ee, r, t, rtr, ertre, eertree, e]</pre>
Line 575: Line 575:
=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Python}}
{{trans|Python}}
<lang julia>mutable struct Node
<syntaxhighlight lang="julia">mutable struct Node
edges::Dict{Char, Node}
edges::Dict{Char, Node}
link::Union{Node, Missing}
link::Union{Node, Missing}
Line 653: Line 653:


eertree("eertree")
eertree("eertree")
</lang> {{output}} <pre>
</syntaxhighlight> {{output}} <pre>
Results of processing string "eertree":
Results of processing string "eertree":
Number of sub-palindromes: 7
Number of sub-palindromes: 7
Line 661: Line 661:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Python}}
{{trans|Python}}
<lang scala>// version 1.1.4
<syntaxhighlight lang="scala">// version 1.1.4


class Node {
class Node {
Line 779: Line 779:
val result = eertree.getSubPalindromes()
val result = eertree.getSubPalindromes()
println("Sub-palindromes: $result")
println("Sub-palindromes: $result")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 789: Line 789:


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
If Version<9.5 Then exit
If Version<9.5 Then exit
If Version=9.5 And Revision<2 Then Exit
If Version=9.5 And Revision<2 Then Exit
Line 866: Line 866:


Print Palindromes$(eertree("eertree"))
Print Palindromes$(eertree("eertree"))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 874: Line 874:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang Nim>import algorithm, strformat, strutils, tables
<syntaxhighlight lang="nim">import algorithm, strformat, strutils, tables


type
type
Line 987: Line 987:
echo fmt"Number of sub-palindromes: {eertree.nodes.len}"
echo fmt"Number of sub-palindromes: {eertree.nodes.len}"
let result = eertree.getSubPalindromes()
let result = eertree.getSubPalindromes()
echo fmt"Sub-palindromes: {result.join("", "")}"</lang>
echo fmt"Sub-palindromes: {result.join("", "")}"</syntaxhighlight>


{{out}}
{{out}}
Line 996: Line 996:
=={{header|Objeck}}==
=={{header|Objeck}}==
{{trans|Java}}
{{trans|Java}}
<lang objeck>use Collection.Generic;
<syntaxhighlight lang="objeck">use Collection.Generic;


class Eertree {
class Eertree {
Line 1,127: Line 1,127:
return @edges;
return @edges;
}
}
}</lang>
}</syntaxhighlight>


{{output}}
{{output}}
Line 1,136: Line 1,136:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>$str = "eertree";
<syntaxhighlight lang="perl">$str = "eertree";


for $n (1 .. length($str)) {
for $n (1 .. length($str)) {
Line 1,151: Line 1,151:
}
}


print join ' ', grep {not $seen{$_}++} @pal, "\n";</lang>
print join ' ', grep {not $seen{$_}++} @pal, "\n";</syntaxhighlight>
{{out}}
{{out}}
<pre>e ee eertree ertre r rtr t</pre>
<pre>e ee eertree ertre r rtr t</pre>
Line 1,157: Line 1,157:
=={{header|Phix}}==
=={{header|Phix}}==
If you use this in anger it may be wise to replace {string chars, sequence next} with a dictionary, which can obviously be either a new dictionary for each node, or perhaps better a single/per tree dictionary keyed on {n,ch}.
If you use this in anger it may be wise to replace {string chars, sequence next} with a dictionary, which can obviously be either a new dictionary for each node, or perhaps better a single/per tree dictionary keyed on {n,ch}.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">LEN</span><span style="color: #0000FF;">,</span><span style="color: #000000;">SUFF</span><span style="color: #0000FF;">,</span><span style="color: #000000;">CHARS</span><span style="color: #0000FF;">,</span><span style="color: #000000;">NEXT</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">LEN</span><span style="color: #0000FF;">,</span><span style="color: #000000;">SUFF</span><span style="color: #0000FF;">,</span><span style="color: #000000;">CHARS</span><span style="color: #0000FF;">,</span><span style="color: #000000;">NEXT</span>
Line 1,237: Line 1,237:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
The tree matches Fig 1 in the pdf linked above.
The tree matches Fig 1 in the pdf linked above.
Line 1,257: Line 1,257:
=={{header|Python}}==
=={{header|Python}}==


<lang python>#!/bin/python
<syntaxhighlight lang="python">#!/bin/python
from __future__ import print_function
from __future__ import print_function


Line 1,361: Line 1,361:
eertree.get_sub_palindromes(eertree.rto, [eertree.rto], [], result) #Odd length words
eertree.get_sub_palindromes(eertree.rto, [eertree.rto], [], result) #Odd length words
eertree.get_sub_palindromes(eertree.rte, [eertree.rte], [], result) #Even length words
eertree.get_sub_palindromes(eertree.rte, [eertree.rte], [], result) #Even length words
print ("Sub-palindromes:", result)</lang>
print ("Sub-palindromes:", result)</syntaxhighlight>


{{out}}
{{out}}
Line 1,371: Line 1,371:
{{trans|Python}}
{{trans|Python}}


<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(struct node (edges ; edges (or forward links)
(struct node (edges ; edges (or forward links)
link ; suffix link (backward links)
link ; suffix link (backward links)
Line 1,456: Line 1,456:
(for ((c "eertree")) (eertree-add! et (char->integer c)))
(for ((c "eertree")) (eertree-add! et (char->integer c)))
(map (compose list->string (curry map integer->char)) (eertree-get-palindromes et)))
(map (compose list->string (curry map integer->char)) (eertree-get-palindromes et)))
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,464: Line 1,464:
(formerly Perl 6)
(formerly Perl 6)
{{trans|Ring}}
{{trans|Ring}}
<lang perl6>my $str = "eertree";
<syntaxhighlight lang="raku" line>my $str = "eertree";
my @pal = ();
my @pal = ();
my ($strrev,$strpal);
my ($strrev,$strpal);
Line 1,482: Line 1,482:


say @pal.unique;
say @pal.unique;
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,490: Line 1,490:
=={{header|REXX}}==
=={{header|REXX}}==
This REXX program is modeled after the &nbsp; '''Ring''' &nbsp; example.
This REXX program is modeled after the &nbsp; '''Ring''' &nbsp; example.
<lang rexx>/*REXX program creates a list of (unique) sub─palindromes that exist in an input string.*/
<syntaxhighlight lang="rexx">/*REXX program creates a list of (unique) sub─palindromes that exist in an input string.*/
parse arg x . /*obtain optional input string from CL.*/
parse arg x . /*obtain optional input string from CL.*/
if x=='' | x=="," then x= 'eertree' /*Not specified? Then use the default.*/
if x=='' | x=="," then x= 'eertree' /*Not specified? Then use the default.*/
Line 1,508: Line 1,508:
say '──────── The number of sub─palindromes found: ' words($)
say '──────── The number of sub─palindromes found: ' words($)
say '──────── The list of sub─palindromes found: ' strip($)
say '──────── The list of sub─palindromes found: ' strip($)
/*stick a fork in it, we're all done. */</lang>
/*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 1,517: Line 1,517:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Eertree
# Project : Eertree


Line 1,543: Line 1,543:
next
next
see sortpal + nl
see sortpal + nl
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 1,557: Line 1,557:
=={{header|Ruby}}==
=={{header|Ruby}}==
{{trans|D}}
{{trans|D}}
<lang ruby>class Node
<syntaxhighlight lang="ruby">class Node
def initialize(length, edges = {}, suffix = 0)
def initialize(length, edges = {}, suffix = 0)
@length = length
@length = length
Line 1,635: Line 1,635:


tree = eertree("eertree")
tree = eertree("eertree")
print subPalindromes(tree), "\n"</lang>
print subPalindromes(tree), "\n"</syntaxhighlight>
{{out}}
{{out}}
<pre>["ee", "e", "r", "t", "rtr", "ertre", "eertree"]</pre>
<pre>["ee", "e", "r", "t", "rtr", "ertre", "eertree"]</pre>
Line 1,641: Line 1,641:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<lang vbnet>Module Module1
<syntaxhighlight lang="vbnet">Module Module1


Class Node
Class Node
Line 1,737: Line 1,737:
End Sub
End Sub


End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre>[ee, e, r, t, rtr, ertre, eertree]</pre>
<pre>[ee, e, r, t, rtr, ertre, eertree]</pre>
Line 1,743: Line 1,743:
=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang ecmascript>class Node {
<syntaxhighlight lang="ecmascript">class Node {
construct new() {
construct new() {
_edges = {} // edges (or forward links)
_edges = {} // edges (or forward links)
Line 1,861: Line 1,861:
System.print("Number of sub-palindromes: %(eertree.nodes.count)")
System.print("Number of sub-palindromes: %(eertree.nodes.count)")
var result = eertree.getSubPalindromes()
var result = eertree.getSubPalindromes()
System.print("Sub-palindromes: %(result)")</lang>
System.print("Sub-palindromes: %(result)")</syntaxhighlight>


{{out}}
{{out}}
Line 1,872: Line 1,872:
=={{header|zkl}}==
=={{header|zkl}}==
{{trans|Python}}
{{trans|Python}}
<lang zkl>class Node{
<syntaxhighlight lang="zkl">class Node{
fcn init(length){
fcn init(length){
var edges=Dictionary(), # edges (or forward links). (char:Node)
var edges=Dictionary(), # edges (or forward links). (char:Node)
Line 1,947: Line 1,947:
}
}
}
}
}</lang>
}</syntaxhighlight>
<lang zkl>st:="eertree";
<syntaxhighlight lang="zkl">st:="eertree";
println("Processing string \"", st,"\"");
println("Processing string \"", st,"\"");
eertree:=Eertree(st);
eertree:=Eertree(st);
println("Number of sub-palindromes: ", eertree.nodes.len());
println("Number of sub-palindromes: ", eertree.nodes.len());
println("Sub-palindromes: ", eertree.get_sub_palindromes());</lang>
println("Sub-palindromes: ", eertree.get_sub_palindromes());</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>