Distinct palindromes within decimal numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: duplicate </lang>)
m (syntax highlighting fixup automation)
Line 24: Line 24:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
{{works with|Factor|0.99 2021-02-05}}
<lang factor>USING: formatting io kernel math math.ranges present prettyprint
<syntaxhighlight lang="factor">USING: formatting io kernel math math.ranges present prettyprint
sequences sequences.extras sets ;
sequences sequences.extras sets ;


Line 41: Line 41:
1320267947849490361205695 }
1320267947849490361205695 }
[ dup dpal [ length 2 < ] reject empty? "%-25d %u\n" printf ]
[ dup dpal [ length 2 < ] reject empty? "%-25d %u\n" printf ]
each</lang>
each</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 90: Line 90:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Wren}}
{{trans|Wren}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 166: Line 166:
fmt.Printf("%-25s %t\n", num, none)
fmt.Printf("%-25s %t\n", num, none)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 226: Line 226:
Note that for the second task, and for very large integers (greater than 2^53) in general, accurate results require
Note that for the second task, and for very large integers (greater than 2^53) in general, accurate results require
gojq, or a version of jq supporting "big integer" literals.
gojq, or a version of jq supporting "big integer" literals.
<syntaxhighlight lang="jq">
<lang jq>
# input: an integer
# input: an integer
# output a stream of distinct integers, each representing an admissible palindrome
# output a stream of distinct integers, each representing an admissible palindrome
Line 259: Line 259:
task1,
task1,
"\nThe integers amongst those in the problem statement that have 2 or more digits:",
"\nThe integers amongst those in the problem statement that have 2 or more digits:",
task2</lang>
task2</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 295: Line 295:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>function allpalindromes(a::Vector{T}) where T
<syntaxhighlight lang="julia">function allpalindromes(a::Vector{T}) where T
pals = Vector{Vector{T}}([[x] for x in a])
pals = Vector{Vector{T}}([[x] for x in a])
len = length(a)
len = length(a)
Line 320: Line 320:
println(rpad(n, 26), palindrome2plusfree(n))
println(rpad(n, 26), palindrome2plusfree(n))
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Number Palindromes
Number Palindromes
Line 367: Line 367:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[ContainsPalindromeQ]
<syntaxhighlight lang="mathematica">ClearAll[ContainsPalindromeQ]
ContainsPalindromeQ[n_Integer, minlength_ : 1, b_ : 10] := Select[DeleteDuplicates@Subsequences[IntegerDigits[n, b], {minlength, \[Infinity]}], PalindromeQ]
ContainsPalindromeQ[n_Integer, minlength_ : 1, b_ : 10] := Select[DeleteDuplicates@Subsequences[IntegerDigits[n, b], {minlength, \[Infinity]}], PalindromeQ]
ContainsPalindromeQ /@ Range[100, 125] // Column
ContainsPalindromeQ /@ Range[100, 125] // Column
Line 373: Line 373:
123498769, 12346098769, 1234572098769, 123456832098769,
123498769, 12346098769, 1234572098769, 123456832098769,
12345679432098769, 1234567905432098769, 123456790165432098769,
12345679432098769, 1234567905432098769, 123456790165432098769,
83071934127905179083, 1320267947849490361205695}</lang>
83071934127905179083, 1320267947849490361205695}</syntaxhighlight>
{{out}}
{{out}}
<pre>{{1},{0},{0,0}}
<pre>{{1},{0},{0,0}}
Line 405: Line 405:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import algorithm, sequtils, sets, strutils
<syntaxhighlight lang="nim">import algorithm, sequtils, sets, strutils


iterator substrings(s: string): string =
iterator substrings(s: string): string =
Line 446: Line 446:
let verb = if pals2.len == 0: " doesn't contain palindromes "
let verb = if pals2.len == 0: " doesn't contain palindromes "
else: " contains at least one palindrome "
else: " contains at least one palindrome "
echo s, verb, "of two digits or more"</lang>
echo s, verb, "of two digits or more"</syntaxhighlight>


{{out}}
{{out}}
Line 491: Line 491:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


# https://rosettacode.org/wiki/Distinct_Palindromes_Within_Decimal_Numbers
# https://rosettacode.org/wiki/Distinct_Palindromes_Within_Decimal_Numbers
Line 509: Line 509:
123456832098769, 12345679432098769, 1234567905432098769,
123456832098769, 12345679432098769, 1234567905432098769,
123456790165432098769, 83071934127905179083, 1320267947849490361205695'
123456790165432098769, 83071934127905179083, 1320267947849490361205695'
=~ /\d+/g;</lang>
=~ /\d+/g;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 542: Line 542:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show_all_palindromes</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">longest</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show_all_palindromes</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">longest</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
Line 579: Line 579:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nNumber Has no &gt;2 digit palindromes\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nNumber Has no &gt;2 digit palindromes\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">show_all_palindromes</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">show_all_palindromes</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 628: Line 628:
=={{header|Raku}}==
=={{header|Raku}}==
A minor modification of the [[Longest_palindromic_substrings#Raku|Longest palindromic substrings]] task. As such, works for any string, not just integers.
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;
<syntaxhighlight lang="raku" line>use Sort::Naturally;


sub getpal ($str) {
sub getpal ($str) {
Line 658: Line 658:
123456832098769, 12345679432098769, 1234567905432098769,
123456832098769, 12345679432098769, 1234567905432098769,
123456790165432098769, 83071934127905179083, 1320267947849490361205695,
123456790165432098769, 83071934127905179083, 1320267947849490361205695,
<Do these strings contain a minimum two character palindrome?></lang>
<Do these strings contain a minimum two character palindrome?></syntaxhighlight>
{{out}}
{{out}}
<pre>All palindromic substrings including (bizarrely enough) single characters:
<pre>All palindromic substrings including (bizarrely enough) single characters:
Line 714: Line 714:
=={{header|REXX}}==
=={{header|REXX}}==
This REXX version can handle strings or numbers.
This REXX version can handle strings or numbers.
<lang>/*REXX pgm finds distinct palindromes contained in substrings (decimal #s or strings). */
<syntaxhighlight lang="text">/*REXX pgm finds distinct palindromes contained in substrings (decimal #s or strings). */
parse arg LO HI mL $$ /*obtain optional arguments from the CL*/
parse arg LO HI mL $$ /*obtain optional arguments from the CL*/
if LO='' | LO="," then LO= 100 /*Not specified? Then use the default.*/
if LO='' | LO="," then LO= 100 /*Not specified? Then use the default.*/
Line 752: Line 752:
end /*k*/
end /*k*/
end /*j*/
end /*j*/
return #</lang>
return #</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 807: Line 807:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func palindromes(arr) {
<syntaxhighlight lang="ruby">func palindromes(arr) {
gather {
gather {
for a in (0..arr.end), b in (a .. arr.end) {
for a in (0..arr.end), b in (a .. arr.end) {
Line 826: Line 826:
say ("#{'%25s' % n} has #{'%2d' % p.len} palindromes of length 2 or more: ",
say ("#{'%25s' % n} has #{'%2d' % p.len} palindromes of length 2 or more: ",
p.sort.map{.join}.sort_by{.len}.join(' '))
p.sort.map{.join}.sort_by{.len}.join(' '))
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 875: Line 875:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
{{libheader|Wren-sort}}
{{libheader|Wren-sort}}
<lang ecmascript>import "/seq" for Lst
<syntaxhighlight lang="ecmascript">import "/seq" for Lst
import "/fmt" for Fmt
import "/fmt" for Fmt
import "/sort" for Sort
import "/sort" for Sort
Line 911: Line 911:
var none = !ss.any { |s| s == s[-1..0] }
var none = !ss.any { |s| s == s[-1..0] }
Fmt.print("$-25s $s", num, none)
Fmt.print("$-25s $s", num, none)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}