Identifying Palindromes
March 31, 2015
Today’s task sounds simple but leads to a little bit of complexity.
Given an array, determine if it is a palindrome. Given a linked list, determine if it is a palindrome. Make both tests as efficient, in both time and space, as possible.
Your task is to write two programs to identify palindromes. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Note that integer division is used, as the middle element of an array of uneven length always satisfies the palindrome condition.
A mindless transcription of the model hare and tortoise to Python:
Sorry about the mis-indentations. Something in the copy-and-paste.
Here’s a list reversing solution, with the list reverse done in place and the correct order restored afterwards:
Palindrome Check for Arrays:
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class ListPalidrome {
private static List myList;
public static void main(String[] args) {
myList = new LinkedList();
System.out.println(“enter the elements in list, enter e to stop”);
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
myList.add(sc.nextInt());
}
sc.close();
System.out.println(isPalidrome(myList));
}
private static boolean isPalidrome(List myList) {
// input is empty or single element, return true
if (myList.isEmpty() || myList.size() == 1) {
return true;
}
// use an aux list for storing first half of the list
List auxList = new LinkedList();
int len = myList.size();
int t = 0;// tortoise
int h = 0;// hare
while (h < len – 1) {
// add tortoise to front of auxList
auxList.add(0, myList.get(t));
t++;
// double increment hare
h += 2;
}
//if length is odd, skip the middle element from comparison
if(len % 2 != 0) t++;
//compare tortoise journey to auxList
int k = 0;
while (t < len) {
if (myList.get(t++) != auxList.get(k++))
return false;
}
return true;
}
}
Code Formatted by ToGoTutor
[…] of my favorite sites Programming Praxis recently had an entry on detecting palindromes. Even if you’re not interested in palindromes, […]
import sys
def check_palindrom(input_list):
if len(input_list) == 1:
return “YES, A Palindrom”
else:
mid = len(input_list)/2
print “MID=”, mid
for i in range(mid):
if (input_list[i] != input_list[len(input_list)-1-i]):
return “No, Not a Palindrom”
else:
continue
return “YES, A Palindrom”
print “Enter the array element with space seperated. Press Enter when done”
input_array = raw_input()
input_list = input_array.split()
print len(input_list)
input_list = [sys.exit(1) if len(input_list)< 1 else (j) for j in input_list]
print input_list
print check_palindrom(input_list
A C++ version that works with std::vector, std::list or any class that supports begin, end, rbegin, and rend. (Since std::list is doubly-linked, it makes things easier.)
This version uses std::mismatch. In essence this is similar to the previous version I posted, except we have to precalculate the end condition using c.size and std::advance. Not a big deal for std::vector, but not good for std::list. Some implementations of std::list maintain a size member variable making c.size O(1) instead of O(n), so the impact varies.