Eertree: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added syntax colouring, made p2js compatible) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 22: | Line 22: | ||
{{trans|D}} |
{{trans|D}} |
||
< |
<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))</ |
print(subPalindromes(tree))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 95: | Line 95: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 190: | Line 190: | ||
} |
} |
||
} |
} |
||
}</ |
}</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}} |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <functional> |
#include <functional> |
||
#include <map> |
#include <map> |
||
Line 316: | Line 316: | ||
return 0; |
return 0; |
||
}</ |
}</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}} |
||
< |
<syntaxhighlight lang="d">import std.array; |
||
import std.stdio; |
import std.stdio; |
||
Line 393: | Line 393: | ||
} |
} |
||
return s.data; |
return s.data; |
||
}</ |
}</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}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 471: | Line 471: | ||
} |
} |
||
return |
return |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 479: | Line 479: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<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: | ||
} |
} |
||
} |
} |
||
}</ |
}</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}} |
||
< |
<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") |
||
</ |
</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}} |
||
< |
<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") |
||
}</ |
}</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}} |
||
< |
<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("", "")}"</ |
echo fmt"Sub-palindromes: {result.join("", "")}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 996: | Line 996: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="objeck">use Collection.Generic; |
||
class Eertree { |
class Eertree { |
||
Line 1,127: | Line 1,127: | ||
return @edges; |
return @edges; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 1,136: | Line 1,136: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<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";</ |
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}. |
||
<!--< |
<!--<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> |
||
<!--</ |
<!--</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}}== |
||
< |
<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)</ |
print ("Sub-palindromes:", result)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,371: | Line 1,371: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<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 |
<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 '''Ring''' example. |
This REXX program is modeled after the '''Ring''' example. |
||
< |
<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. */</ |
/*stick a fork in it, we're all done. */</syntaxhighlight> |
||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
<pre> |
<pre> |
||
Line 1,517: | Line 1,517: | ||
=={{header|Ring}}== |
=={{header|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}} |
||
< |
<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"</ |
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#}} |
||
< |
<syntaxhighlight lang="vbnet">Module Module1 |
||
Class Node |
Class Node |
||
Line 1,737: | Line 1,737: | ||
End Sub |
End Sub |
||
End Module</ |
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}} |
||
< |
<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)")</ |
System.print("Sub-palindromes: %(result)")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,872: | Line 1,872: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<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: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
< |
<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());</ |
println("Sub-palindromes: ", eertree.get_sub_palindromes());</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |