Distinct palindromes within decimal numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: sort palindromes by length)
(→‎{{header|Raku}}: Add a Raku example)
Line 245: Line 245:
1320267947849490361205695 false
1320267947849490361205695 false
</pre>
</pre>

=={{header|Raku}}==
A minor modification of the [[Longest_palindromic_substrings#Raku|Longest palindromic substrings]] task. As such, works for any string, not just integers.
<lang perl6>use Sort::Naturally;

sub getpal ($str) {
my @chars = $str.comb;
my @pal = flat @chars,
(1 ..^ @chars).map: -> \idx {
my @s;
for 1, 2 {
my int ($rev, $fwd) = $_, 1;
loop {
quietly last if ($rev > idx) || (@chars[idx - $rev] ne @chars[idx + $fwd]);
$rev = $rev + 1;
$fwd = $fwd + 1;
}
@s.push: @chars[idx - $rev ^..^ idx + $fwd].join if $rev + $fwd > 2;
last if @chars[idx - 1] ne @chars[idx];
}
next unless +@s;
@s
}
@pal.unique.sort({.chars, &naturally});
}

say 'All palindromic substrings including (bizarrely enough) single characters:';
put "$_ => ", getpal $_ for 100..125;
put "\nDo these strings contain a minimum two character palindrome?";
printf "%25s => %s\n", $_, getpal($_).tail.chars > 1 for flat
9, 169, 12769, 1238769, 123498769, 12346098769, 1234572098769,
123456832098769, 12345679432098769, 1234567905432098769,
123456790165432098769, 83071934127905179083, 1320267947849490361205695,
<Do these strings contain a minimum two character palindrome?></lang>
{{out}}
<pre>All palindromic substrings including (bizarrely enough) single characters:
100 => 1 0 00
101 => 1 0 101
102 => 1 0 2
103 => 1 0 3
104 => 1 0 4
105 => 1 0 5
106 => 1 0 6
107 => 1 0 7
108 => 1 0 8
109 => 1 0 9
110 => 1 0 11
111 => 1 11 111
112 => 1 2 11
113 => 1 3 11
114 => 1 4 11
115 => 1 5 11
116 => 1 6 11
117 => 1 7 11
118 => 1 8 11
119 => 1 9 11
120 => 1 2 0
121 => 1 2 121
122 => 1 2 22
123 => 1 2 3
124 => 1 2 4
125 => 1 2 5

Do these strings contain a minimum two character palindrome?
9 => False
169 => False
12769 => False
1238769 => False
123498769 => False
12346098769 => False
1234572098769 => False
123456832098769 => False
12345679432098769 => False
1234567905432098769 => False
123456790165432098769 => False
83071934127905179083 => False
1320267947849490361205695 => True
Do => False
these => True
strings => False
contain => False
a => False
minimum => True
two => False
character => True
palindrome? => False</pre>


=={{header|Wren}}==
=={{header|Wren}}==