// IsPalindrome.groovy, ©2017 by Josh Moyer // All rights reserved. def Palindrome(theString) { def strLen = theString.length() def i def oppIndex def currentChar if (strLen == 0) return Result.Null for (i = 0 ; i < strLen / 2 ; i++) { currentChar = theString.charAt(i) oppIndex = strLen - i if (currentChar == theString.charAt(oppIndex -1)) continue else return Result.Not } return Result.Is } def enum Result { Is, Not, Null } class TestCase { def TestInput def TestResult def TestCase(Input, Result) { TestInput = Input TestResult = Result } } def CurrentResult TestCase[] TestCases = [ new TestCase('', Result.Null), new TestCase(' ', Result.Is), new TestCase(' ', Result.Is), new TestCase(' ', Result.Is), new TestCase('A', Result.Is), new TestCase('AA', Result.Is), new TestCase('AB', Result.Not), new TestCase('AAA', Result.Is), new TestCase('AAB', Result.Not), new TestCase('ABA', Result.Is), new TestCase('radar', Result.Is), new TestCase('Radar', Result.Not), new TestCase('Rädar', Result.Not), new TestCase('1', Result.Is), new TestCase('12', Result.Not), new TestCase('123', Result.Not), new TestCase('121', Result.Is) ] for (CurrentCase in TestCases) { CurrentResult = Palindrome(CurrentCase.TestInput) if (CurrentResult == CurrentCase.TestResult) continue else println CurrentCase.TestInput + ' failed test.' }