Distinct palindromes within decimal numbers: Difference between revisions

Added Go
(Added Sidef)
(Added Go)
Line 86:
83071934127905179083 t
1320267947849490361205695 f
</pre>
 
=={{header|Go}}==
{{trans|Wren}}
<lang go>package main
 
import (
"fmt"
"sort"
"strings"
)
 
func substrings(s string) []string {
var ss []string
n := len(s)
for i := 0; i < n; i++ {
for j := 1; j <= n-i; j++ {
ss = append(ss, s[i:i+j])
}
}
return ss
}
 
func reversed(s string) string {
var sb strings.Builder
for i := len(s) - 1; i >= 0; i-- {
sb.WriteByte(s[i])
}
return sb.String()
}
 
func main() {
fmt.Println("Number Palindromes")
for i := 100; i <= 125; i++ {
var pals []string
ss := substrings(fmt.Sprintf("%d", i))
for _, s := range ss {
if s == reversed(s) {
pals = append(pals, s)
}
}
m := make(map[string]bool)
for _, pal := range pals {
m[pal] = true
}
pals = pals[:0]
for k := range m {
pals = append(pals, k)
}
sort.Slice(pals, func(i, j int) bool {
if len(pals[i]) == len(pals[j]) {
return pals[i] < pals[j]
}
return len(pals[i]) < len(pals[j])
})
fmt.Printf("%d %3s\n", i, pals)
}
nums := []string{
"9", "169", "12769", "1238769", "123498769", "12346098769", "1234572098769",
"123456832098769", "12345679432098769", "1234567905432098769", "123456790165432098769",
"83071934127905179083", "1320267947849490361205695",
}
fmt.Println("\nNumber Has no >= 2 digit palindromes")
for _, num := range nums {
tmp := substrings(num)
var ss []string
for _, t := range tmp {
if len(t) > 1 {
ss = append(ss, t)
}
}
none := true
for _, s := range ss {
if s == reversed(s) {
none = false
break
}
}
fmt.Printf("%-25s %t\n", num, none)
}
}</lang>
 
{{out}}
<pre>
Number Palindromes
100 [ 0 1 00]
101 [ 0 1 101]
102 [ 0 1 2]
103 [ 0 1 3]
104 [ 0 1 4]
105 [ 0 1 5]
106 [ 0 1 6]
107 [ 0 1 7]
108 [ 0 1 8]
109 [ 0 1 9]
110 [ 0 1 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 [ 0 1 2]
121 [ 1 2 121]
122 [ 1 2 22]
123 [ 1 2 3]
124 [ 1 2 4]
125 [ 1 2 5]
 
Number Has no >= 2 digit palindromes
9 true
169 true
12769 true
1238769 true
123498769 true
12346098769 true
1234572098769 true
123456832098769 true
12345679432098769 true
1234567905432098769 true
123456790165432098769 true
83071934127905179083 true
1320267947849490361205695 false
</pre>
 
9,476

edits