Skip to main content

Identifying a Palindrome with Linux and Python Unraveled


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







Comments

Popular posts from this blog

Calculator Techniques for the Casio FX-991ES and FX-991EX Unraveled

In solving engineering problems, one may not have the luxury of time. Most situations demand immediate results. The price of falling behind schedule is costly and demeaning to one's reputation. Therefore, every bit of precaution must be taken to expedite calculations. The following introduces methods to tackle these problems speedily using a Casio calculator FX-991ES and FX-991EX.


►For algebraic problems where you need to find the exact value of a dependent or independent variable, just use the CALC or [ES] Mode 5 functions or [EX] MENU A functions.


►For definite differentiation and integration problems, simply use the d/dx and integral operators in the COMP mode.


►For models that follow the differential equation: dP/dx=kt and models that follow a geometric function(i.e. A*B^x).

[ES]
-Simply go to Mode 3 (STAT) (5)      e^x
-For geometric functions Mode 3 (STAT) 6 A*B^x
-(Why? Because the solution to the D.E. dP/dx=kt is an exponential function e^x.
When we know the boundary con…

Common Difficulties and Mishaps in 6.004 Computation Structures (by MITx)

Updated: 
May 6, 2018
VLSI Project: The Beta Layout [help needed]Current Tasks: ►Complete 32-bit ALU layout [unpipelined] in a 3-metal-layer C5 process. ►Extend Excel VBA macro to generate code for sequential instructions (machine language to actual electrical signals).
Current Obstacles/Unresolved Decisions:
►Use of complementary CMOS or pass transistor logic (do both? time expensive, will depend on sched.
►Adder selection: Brent-Kung; Kogge Stone; Ladner Fischer (brent takes up most space but seems to be fastest, consider fan-out) [do all? time expensive, will depend on sched.)
►layout requirements and DRC errors

Please leave a comment on the post below for advise. Any help is highly appreciated.




Yay or Nay? A Closer Look at AnDapt’s PMIC On-Demand Technology

Innovations on making product features customizable are recently gaining popularity. Take Andapt for example, a fabless start-up that unveiled its Multi-Rail Power Platform technology for On-Demand PMIC applications a few months back. (read all about it here: Will PMIC On-Demand Replace Catalog Power Devices?) Their online platform, WebAmp, enables the consumer to configure the PMIC based on desired specifications. Fortunately, I got a hands-on experience during the trial period (without the physical board (AmP8DB1) or adaptor (AmpLink)). In my opinion, their GUI is friendly but it lacks a verification method for tuning (i.e. the entered combination of specs). How would we know if it will perform as expected or if there are contradicting indications that yield queer behavior? Also, there is not just one IP available, but many that cater to a differing number of channels and voltage requirements (each with their own price tag).
Every new emerging technology has the potential to oversh…