Distinct palindromes within decimal numbers: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (Thundergnat moved page Distinct Palindromes Within Decimal Numbers to Distinct Palindromes within decimal numbers: Standardize capitalization) |
|||
Line 212: | Line 212: | ||
83071934127905179083 true |
83071934127905179083 true |
||
1320267947849490361205695 false |
1320267947849490361205695 false |
||
</pre> |
|||
=={{header|jq}}== |
|||
'''Works with [[Jq|jq]]''' (but the second task requires a version with support for "big integer" literals) |
|||
<br> |
|||
'''Works with gojq, the Go implementation of jq''' (provided `keys_unsorted` is replaced by `keys`) |
|||
In this entry, except for 0 itself, palindromes involving leading 0s are ignored. |
|||
One feature of the implementation given here is that uniqueness is achieved without |
|||
any sorting; however, if gojq is used, then `keys` would have to be used, thus entailing a sort after distinctness has been achieved. |
|||
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. |
|||
<lang jq> |
|||
# input: an integer |
|||
# output a stream of distinct integers, each representing an admissible palindrome |
|||
def palindromes: |
|||
# input: an array |
|||
def is_palindrome: . == reverse; |
|||
def all_palindromes: |
|||
(tostring|explode) |
|||
| range(0; length) as $i |
|||
| range($i+1; length+1) as $j |
|||
| .[$i:$j] # candidate |
|||
| select(is_palindrome) |
|||
| implode |
|||
# trim leading 0s: |
|||
| if length > 1 then sub("^00*"; "") else . end |
|||
| select(length>0) ; |
|||
INDEX(all_palindromes; .) | keys_unsorted[] | tonumber; |
|||
def task1: |
|||
" i palindromes", |
|||
(range(100; 126) |
|||
| "\(.) \([palindromes]|join(" "))" ); |
|||
def task2: |
|||
(9, 169, 12769, 1238769, 123498769, 12346098769, 1234572098769, |
|||
123456832098769, 12345679432098769, 1234567905432098769, 123456790165432098769, |
|||
83071934127905179083, 1320267947849490361205695) |
|||
| select( any(palindromes; . > 99) ); |
|||
task1, |
|||
"\nThe integers amongst those in the problem statement that have 2 or more digits:", |
|||
task2</lang> |
|||
{{out}} |
|||
<pre> |
|||
i palindromes |
|||
100 1 0 |
|||
101 1 101 0 |
|||
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 11 0 |
|||
111 1 11 111 |
|||
112 1 11 2 |
|||
113 1 11 3 |
|||
114 1 11 4 |
|||
115 1 11 5 |
|||
116 1 11 6 |
|||
117 1 11 7 |
|||
118 1 11 8 |
|||
119 1 11 9 |
|||
120 1 2 0 |
|||
121 1 121 2 |
|||
122 1 2 22 |
|||
123 1 2 3 |
|||
124 1 2 4 |
|||
125 1 2 5 |
|||
The integers amongst those in the problem statement that have 2 or more digits: |
|||
1320267947849490361205695 |
|||
</pre> |
</pre> |
||