View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Case Final Project Summer 2007

Sum2002 Midterm Review: Palindromes

Questions/answers/comments? Midterm Review - Summer 2002


findPalindrome: stringList
"traverses the stringList and returns an ordered collection
of those string that are palindromes."

| palindromeList noSpacesString reversedString isPalindrome |

stringList collect: [ :string |
noSpacesString _ string reject: [ :ch | ch = Character space . ] .
reversedString _ noSpacesString reverse .
isPalindrome _ true .
(1 to: noSpacesString size) do: [ :letter |
((noSpacesString at: letter) = (reversedString at: letter))
ifFalse: [ isPalindrome _ false ] .
] .
(isPalindrome)
ifTrue: [ palindromeList add: string . ] .

] .

^ palindromeList .



The first part is okay where you collect the results of removing the spaces into a new collection, but the second part would be much shorter if you use the test of equal values (=) on the no space string and reversed no space string within a select. Barbara Ericson

yeah, in retrospect, that was a very c-style answer. alan fay

Link to this Page