We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 889399d commit 23505e8Copy full SHA for 23505e8
archive/t/tcl/palindromic-number.tcl
@@ -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