View this PageEdit this Page (locked)Attachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Schedule: 7 October 2005

Disscussion: 4 Answer a Midterm Review Question

Summer 2002

Palindromes

Write a method findPalindrome: that takes as input a collection of strings, possible containing spaces, (e.g. #(‘2002’ ‘a man a plan a canal panama’ ‘george of the jungle’)) and returns just those strings that are palindromes, i.e., without spaces, the letters are the same forward and backward (for the example, #(‘2002’ ‘a man a plan a canal panama’) would be the correct return value). Some useful tips:

Strings understand reverse
string reject: [:ch | ch = Character space] will return string with spaces removed.


findPalindromes: Strings
"finds the palindromes with in an array or strings"

| originals forwardString ReversedString | 

originals := OrderedCollection new.

Strings do: [:originalString |
forwardString:=originalString reject: [:ch | ch = Character space].
ReversedString:=forwardString reverse.
(ReversedString compare: forwardString) ifTrue:[originals add: originalString]].

((originals size)=0)
ifTrue:[^nil.]
ifFalse:[^originals].

Back

Links to this Page