This project implements a Palindrome Checker in Go, providing two different implementations to check whether a given string is a palindrome. It also includes comprehensive unit tests, test-driven development (TDD) practices, and benchmark tests to compare the performance of the two implementations.
- Go (Golang): The primary programming language used for implementation.
- Testing: Go's built-in testing framework for unit testing.
- Benchmarking: Go's benchmarking feature to measure the performance of functions.
A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward. Examples of palindromes include "radar," "level," "12321," and "A man, a plan, a canal, Panama!".
The isPalindrome
function checks if a given string is a palindrome by reversing the string and comparing it with the original. If the reversed string matches the original, the function returns true
; otherwise, it returns false
. This implementation uses the invertText
helper function to reverse the string.
The isPalindrome2
function directly compares characters from the start and end of the string, moving towards the center. It iterates through the string, comparing characters at corresponding positions. If any characters do not match, the function returns false
; otherwise, it returns true
.
Both isPalindrome
and isPalindrome2
functions were developed using a test-driven development (TDD) approach. Test cases were written to define the expected behavior of the functions before implementation. This ensured that the functions were thoroughly tested and behaved correctly under various scenarios.
Benchmark tests were conducted to measure the performance of the isPalindrome
and isPalindrome2
functions. These benchmarks were used to compare the execution time of the two implementations and determine which one performs better in terms of speed.
Benchmark tests revealed that the isPalindrome2
function, which directly compares characters, performs faster than the isPalindrome
function, which involves string reversal. This is because the isPalindrome2
function avoids the overhead of creating a reversed string and comparing it with the original. The direct character comparison approach of isPalindrome2
results in better performance, especially for large strings.
The Palindrome Checker program provides an interactive menu for users to perform actions such as checking if a word is a palindrome or learning about palindromes. Users can choose from the following options:
- What is a palindrome?: Provides a simple explanation of what constitutes a palindrome.
- Check if a word is a palindrome: Allows users to enter a word and determine if it's a palindrome.
- Exit: Allows users to exit the program.
Users can navigate the menu by entering the corresponding option number.
In conclusion, the Palindrome Checker project demonstrates the implementation of two different approaches to check for palindromes in a given string. Through the use of test-driven development (TDD) practices and benchmarking, the performance differences between the two implementations were analyzed, highlighting the importance of choosing efficient algorithms for common programming problems like palindrome detection.