Page 210 - Learn To Program With Scratch
P. 210

To check whether the number is a palindrome, we need to compare
                         the first and eighth digits, the second and seventh digits, the third and
                         sixth digits, and so on. If any comparison produces a false result (meaning
                         that the two digits are not equal), then the number is not a palindrome.
                         A program that implements this palindrome test algorithm is shown in
                         Figure 8-4.







                                                     Set pos1 to access first digit.

                                                     Set pos2 to access last digit.

                                                     We need to perform (length / 2) comparisons.



                                                         If the digits at pos1 and pos2 are
                                                         not equal, then the input number
                                                         is not a palindrome.

                                                     Move pos1 forward.
                                                     Move pos2 backward.

                                                     All digits compared are the same.

                                                     .
                                                     The input number is a palindrome.
                         Figure 8-4: This program tests whether an integer input by the user is a palindrome .
                             The script accesses the digits to be compared with two variables (pos1
                         and pos2 in Figure 8-3) that move in opposite directions. The first variable
                         (pos1) starts at the first digit and moves forward, while the second variable
                         (pos2) starts at the last digit and moves backward. The number of required
                         comparisons is at most one-half the digits in the input number. With an
                         input of 12344321, we need at most four comparisons because the input
                         number has eight digits. (The same logic applies if the input integer has
                         an odd number of digits, since the digit in the middle of the number need
                         not be compared.) Once the program determines whether or not the user’s
                         number is a palindrome, it displays a message with the result.



                                                   try it out 8-1
           Palindrome .sb2  Load Palindrome.sb2 and run it to understand how it works . Because of the way
                            Scratch handles decimal repeat counts, if the input number has an odd number of
                            digits, the script performs one extra comparison of the two digits that surround the
                            middle digit . Try to fix the program to perform the correct number of repeats when
                            the input number has an odd number of digits .





           188   Chapter 8
                                           www.it-ebooks.info
   205   206   207   208   209   210   211   212   213   214   215