Skip to content

Commit 23505e8

Browse files
authored
Add Palindromic Number in Tcl (#5073)
1 parent 889399d commit 23505e8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package require Tcl 8.6
2+
3+
proc usage {} {
4+
puts stderr "Usage: please input a non-negative integer"
5+
exit 1
6+
}
7+
8+
proc isNonNegativeInteger {s} { return [regexp {^[0-9]+$} $s] }
9+
10+
11+
proc isPalindrome {s} {
12+
set n [string length $s]
13+
set i 0
14+
set j [expr {$n - 1}]
15+
while {$i < $j} {
16+
if {[string range $s $i $i] ne [string range $s $j $j]} {
17+
return 0
18+
}
19+
incr i
20+
incr j -1
21+
}
22+
return 1
23+
}
24+
25+
if {$argc != 1} { usage }
26+
27+
set input [string trim [lindex $argv 0]]
28+
if {![isNonNegativeInteger $input]} { usage }
29+
30+
puts [expr {[isPalindrome $input] ? "true" : "false"}]

0 commit comments

Comments
 (0)