Distinct palindromes within decimal numbers: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: duplicate </lang>)
m (syntax highlighting fixup automation)
Line 24:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: formatting io kernel math math.ranges present prettyprint
sequences sequences.extras sets ;
 
Line 41:
1320267947849490361205695 }
[ dup dpal [ length 2 < ] reject empty? "%-25d %u\n" printf ]
each</langsyntaxhighlight>
{{out}}
<pre>
Line 90:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 166:
fmt.Printf("%-25s %t\n", num, none)
}
}</langsyntaxhighlight>
 
{{out}}
Line 226:
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.
<syntaxhighlight lang="jq">
<lang jq>
# input: an integer
# output a stream of distinct integers, each representing an admissible palindrome
Line 259:
task1,
"\nThe integers amongst those in the problem statement that have 2 or more digits:",
task2</langsyntaxhighlight>
{{out}}
<pre>
Line 295:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function allpalindromes(a::Vector{T}) where T
pals = Vector{Vector{T}}([[x] for x in a])
len = length(a)
Line 320:
println(rpad(n, 26), palindrome2plusfree(n))
end
</langsyntaxhighlight>{{out}}
<pre>
Number Palindromes
Line 367:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[ContainsPalindromeQ]
ContainsPalindromeQ[n_Integer, minlength_ : 1, b_ : 10] := Select[DeleteDuplicates@Subsequences[IntegerDigits[n, b], {minlength, \[Infinity]}], PalindromeQ]
ContainsPalindromeQ /@ Range[100, 125] // Column
Line 373:
123498769, 12346098769, 1234572098769, 123456832098769,
12345679432098769, 1234567905432098769, 123456790165432098769,
83071934127905179083, 1320267947849490361205695}</langsyntaxhighlight>
{{out}}
<pre>{{1},{0},{0,0}}
Line 405:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils, sets, strutils
 
iterator substrings(s: string): string =
Line 446:
let verb = if pals2.len == 0: " doesn't contain palindromes "
else: " contains at least one palindrome "
echo s, verb, "of two digits or more"</langsyntaxhighlight>
 
{{out}}
Line 491:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
# https://rosettacode.org/wiki/Distinct_Palindromes_Within_Decimal_Numbers
Line 509:
123456832098769, 12345679432098769, 1234567905432098769,
123456790165432098769, 83071934127905179083, 1320267947849490361205695'
=~ /\d+/g;</langsyntaxhighlight>
{{out}}
<pre>
Line 542:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
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;">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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 628:
=={{header|Raku}}==
A minor modification of the [[Longest_palindromic_substrings#Raku|Longest palindromic substrings]] task. As such, works for any string, not just integers.
<syntaxhighlight lang="raku" perl6line>use Sort::Naturally;
 
sub getpal ($str) {
Line 658:
123456832098769, 12345679432098769, 1234567905432098769,
123456790165432098769, 83071934127905179083, 1320267947849490361205695,
<Do these strings contain a minimum two character palindrome?></langsyntaxhighlight>
{{out}}
<pre>All palindromic substrings including (bizarrely enough) single characters:
Line 714:
=={{header|REXX}}==
This REXX version can handle strings or numbers.
<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*/
if LO='' | LO="," then LO= 100 /*Not specified? Then use the default.*/
Line 752:
end /*k*/
end /*j*/
return #</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 807:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func palindromes(arr) {
gather {
for a in (0..arr.end), b in (a .. arr.end) {
Line 826:
say ("#{'%25s' % n} has #{'%2d' % p.len} palindromes of length 2 or more: ",
p.sort.map{.join}.sort_by{.len}.join(' '))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 875:
{{libheader|Wren-fmt}}
{{libheader|Wren-sort}}
<langsyntaxhighlight lang="ecmascript">import "/seq" for Lst
import "/fmt" for Fmt
import "/sort" for Sort
Line 911:
var none = !ss.any { |s| s == s[-1..0] }
Fmt.print("$-25s $s", num, none)
}</langsyntaxhighlight>
 
{{out}}
10,327

edits