Jump to content

Longest palindromic substrings: Difference between revisions

→‎{{header|Raku}}: Show longest 5 substring groups. Minor edits
(→‎{{header|Raku}}: Show longest 5 substring groups. Minor edits)
Line 445:
=={{header|Raku}}==
{{works with|Rakudo|2020.09}}
This version regularizes (ignores) case and ignores non alphabeticalphanumeric characters. It is only concerned with finding the ''longest'' palindromic substrings so does not exhaustively find ''all possible'' palindromes. If a palindromic substring is found to be part of a longer palindrome, it is not captured separately. Showing the longest 5 palindromic substring groups.
 
<lang perl6>my @chars = q:to/END/ .lc.comb: /\w/;
Line 508:
}
 
.unique.sort.put for @cpfoa.grep( *.so ).tail(5).reverse;</lang>
{{out}}
<pre>doninemeninterpretninemeninod godarednuggetafateggunderadog</pre>
gohangasalamiimalasagnahog
arwontloversrevoltnowra
imanamregalagermanami mayamoodybabydoomayam ootnolemonsnomelontoo oozyratinasanitaryzoo
ratsliveonnoevilstar</pre>
 
This isn't particularly optimised but isn't too shabby either. When run against the first million digits of pi: [http://www.eveandersson.com/pi/digits/1000000 http://www.eveandersson.com/pi/digits/1000000], and modifying the last line to return the top five longest palindrome groups <pre>.unique.sort.put for @cpfoa.grep( *.so ).tail(5).reverse;</pre> Wewe get:
 
<pre>9475082805749
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.