It all began one pleasant afternoon some few weeks ago, when our very charismatic group leader and I were engaged in animated conversation over effective scripting techniques under the Linux platform [that runs with a certain proprietary "Verilog" language based on Lisp, of which I may not be allowed to disclose] to supplement extraction of a certain simulation result of a parameter on an IC. He challenged me to write a program that would prompt the user to enter a word, and the program should distinguish whether the word entered is a palindrome or not. [By the way, a palindrome is a word that, when reversed, will spell out the same thing. Examples are "ababa", "anna", "madam", "racecar", etc.] The word to be entered must be a whole word and anything between the letters weren't allowed. So with my enormous pride on the line, I jocundly accepted.

First of, my foundation on Linux programming/scripting was flimsy at that time [I'm more of a MATLAB and VBA/VBScript guy]. With a very faint idea of where to start, I had very little direction on tackling the problem. Then I asked myself, what is the first step everyone takes when posed with a problem? That's right! Google the problem. To my dismay, no one has posted anything on the matter yet [which is partly why I'm writing this article right now...] Aaand... the battle would be an arduous endeavor from that point on.

Anyhow, after a few weeks [yes, weeks because alotting time for such a trivial matter is extremely difficult in the semiconductor industry, not to mention the other things you have to or would want to self-study] of digging and reading and analyzing and coding and debugging, I finally arrived at the code below:



#!/bin/bash



read -p "Please enter word:" wordkopo



let "leng=${#wordkopo}"

let "x=0"

let "xr=leng-1"

let "mon=0"

until [ "$x" -eq $leng ]

do

if [ ${wordkopo:$x:1} = ${wordkopo:$xr:1} ]

then

let "mon=$mon+1"

fi

let "x=$x+1"

let "xr=$xr-1"

done

if [ $mon = $leng]

then

echo "The word you entered is a palindrome."

fi






It is a pretty short code with functions that I've stitched together from various sources. The task still was not that straightforward since I still had to think of the algorithm. Anyway, I'll do my best to explain how each line works.

 The she-bang [#!] followed by [bin/bash] runs the script using bash [as opposed to the simple csh which gives the user basic functionality]. The "read" command prompts the user to enter the word, which is stored in the variable "wordkopo". The "let" command is like the "set" command in DOS, a variable declaration command. I've assigned the "leng" variable to store the total length of the word, the "x" variable to store the index 0 and "xr" [x reverse] to store the index equal to the word length, and "mon" as a way of counting how many letters have matched. Obviously, if all letters matched, then variable "mon" should be equal to the word length. So I had to find a way to make the comparison "leng" number of times, which is by using a looping function [until]. Then I used the "if" statement to make the comparison and increment "mon" if the letters are equal. At the end of the loop, if all letters were equal, then "mon" would have incremented "leng" number of times. Thus, if the word is a palindrome, then "mon" would have to be equal to "leng" and all I needed to do was add an "if" statement to make the comparison between the "mon" and "leng" variables. The "echo" function [just like DOS] simple prints out the string.

What really stumped me during my digging was how to isolate the letters from the word. I searched and searched but could not find a function until... Eureka! It seems that using colons after the variable name isolates a letter in the word - ${<variablename>:<nth letter>:<numberoftailingletters>}. It is analogous to the head:tail function in listing. The syntax for the if statement is amusing too [reversing the "if" word to denote the end of the if statement, how cute].

This code does not have any practical use, but the functions and experience in analysis gained was worth the effort. Hopefully, I'd get to further my programming and automation skills and share my know-how [and more codes like this one] in the future.






[One year later, I add code that does the same thing through Python]


Python Implementation


I've recently taken a class in Python programming [to add to my technical arsenal, and because of its popularity - they say its pretty useful]. We were given this exercise on coding a script that identifies a word as a palindrome. I recalled the algorithm that I used when coding the same script in Linux. I present the code below.


def isPalindrome(aString):

    d=1

    c=0

    wlenj=len(aString)

    while c<wlenj-1:

       if aString[c] == aString[wlenj-c-1]: 

           d+=1

       c+=1

    decij=(d==wlenj)

    if wlenj == 0:

        return True

    else:

        return decij