Exercise: Identifying Palindromes

Palindromes are strings that read the same forward and backward, such as the words "radar", "madam", "eye", "kayak", and "testset".

There are several ways to evaluate whether or not a given string is a palindrome. Here is one very simple solution to identifying palindromes. This solution uses the built-in reverse method:

def isPalindromeWithBuiltIn(s: String): Boolean =
  s == s.reverse

Your Task

Your task is to implement method def isPalindrome(s: String): Boolean WITHOUT using the built-in reverse method, but to solve this task with basic list methods.

Pull the latest version from Git. For this exercise we have prepared:

  • Palindrome.scala: contains method stub isPalindrome which needs to be implemented
  • PalindromeTest.scala: contains unit tests. Before using these unit tests, you have to activate them by replacing all occurrences of ignore with in.

 

This task can be solved iteratively or recursively; both variants are fine, but you have to pick one. Here are hints for a recursive algorithm:
  • Comparison: check whether the first and last characters (by using methods head and last) are equal
  • Recursively repeat this comparison on a reduced string: remove the first and last characters and, hence, compare the second character with the second last character, etc.