Posted: Fri May 09, 2008 1:31 am Post subject: Exercise: Palindromes and Isograms
Write a program which reads in a line of input, splits it up into words, then outputs (in order) words that are either palindromes or isograms.
Please note that words containing spaces may be input by surrounding them with quotes. Quotes may be input themselves by escaping them with a backslash.
Again, please identify the language you choose to use.
Sponsor Sponsor
Saad
Posted: Fri May 09, 2008 4:50 pm Post subject: Re: Exercise: Palindromes and Isograms
Haven't coded in haskell for a while but I decided to try it anyway
Posted: Fri May 09, 2008 5:40 pm Post subject: RE:Exercise: Palindromes and Isograms
For those of us who are lazy (me), what's an isogram. And does it have to be a palindrome of real words?
rizzix
Posted: Fri May 09, 2008 8:26 pm Post subject: RE:Exercise: Palindromes and Isograms
Here's mine: < see code below >
Edit: syntax tags are getting to be really annoying
rizzix
Posted: Fri May 09, 2008 8:32 pm Post subject: Re: RE:Exercise: Palindromes and Isograms
md @ Fri May 09, 2008 5:40 pm wrote:
For those of us who are lazy (me), what's an isogram. And does it have to be a palindrome of real words?
An isogram is-a palindrome, so just check for palindromes
Saad
Posted: Fri May 09, 2008 8:51 pm Post subject: Re: Exercise: Palindromes and Isograms
As rizzix pointed out before, I did have errors in my code. I make a couple of errors that I should have caught (I did test it just not thoroughly enough). Error is now fixed what I thought were the errors.
For those of us who are lazy (me), what's an isogram. And does it have to be a palindrome of real words?
An isogram is-a palindrome, so just check for palindromes
From the definition from wikipedia
An isogram (also known as a "nonpattern word") is a logological term for a word or phrase without a repeating letter. Link here
richcash
Posted: Fri May 09, 2008 8:56 pm Post subject: Re: Exercise: Palindromes and Isograms
Well, on wikipedia, one of the defintions for isograms says a word with each letter appearing exactly once. Only under the latter definition that isograms are words where each letter appears the same number of times would palindromes be a subset of isograms.
So there might be some ambiguity here unless one of those definitions is implied for CS problems.
Edit : ^Saad beat me to it.
rizzix
Posted: Fri May 09, 2008 9:08 pm Post subject: RE:Exercise: Palindromes and Isograms
Oh interesting... I thought it was `just` single letter words like aaa, bbb, etc
Saad: Shouldn't you account for single-quoted strings?
wtd: way to go, for making it so ambiguous
Sponsor Sponsor
wtd
Posted: Fri May 09, 2008 10:06 pm Post subject: RE:Exercise: Palindromes and Isograms
In this case, it's defined as a word with no repeating letters.
And no, you don't have to check to make sure the "words" are valid.
rizzix
Posted: Fri May 09, 2008 10:31 pm Post subject: RE:Exercise: Palindromes and Isograms
OK, so what about strings like: 'abc def ghi'
The space character appears twice. Would that be considered an isogram?
wtd
Posted: Fri May 09, 2008 10:38 pm Post subject: RE:Exercise: Palindromes and Isograms
No, it does not count. That would be an isogram. Isograms for the purposes of this exercise are case-insensitive.
rizzix
Posted: Fri May 09, 2008 10:45 pm Post subject: Re: RE:Exercise: Palindromes and Isograms
wtd @ Fri May 09, 2008 10:38 pm wrote:
Isograms for the purposes of this exercise are case-insensitive.
You know that only makes it longer, not shorter
btiffin
Posted: Fri May 09, 2008 10:47 pm Post subject: Re: Exercise: Palindromes and Isograms
Here's one in REBOL. It does not handle escaped quotes with backslash, but caret instead.
REBOL:
#!rebol -c
rebol []
line: ask "? "
words: parse/all line " "
probe words
foreach word words [
mod: uppercase trim/all copy word
if equal? mod reverse copy mod [print ["Palindrome:" word]]
iso: copy []
foreach ch mod [
alter iso ch
]
if equal? length? iso length? word [print ["Isogram:" word]]
]
Output:
$ ./palins.r
? "Naomi, sex at noon taxes, I moan" aba "was saw" shockingly
["Naomi, sex at noon taxes, I moan" "aba" "was saw" "shockingly"]
Palindrome: Naomi, sex at noon taxes, I moan
Palindrome: aba
Palindrome: was saw
Isogram: shockingly
rizzix
Posted: Fri May 09, 2008 11:10 pm Post subject: RE:Exercise: Palindromes and Isograms
Here everything inclusive of the case-check:
Haskell:
import List (nub)
import Char (toLower)
main = getLine >>= (mapM_ putStrLn) . (filter isIsoOrPalin) . strings []
where strings rs [] = reverse rs
strings rs ('\'':xs) = let (ys, ss) = esc_single xs [] in strings (ys:rs) ss
strings rs ('"':xs) = let (ys, ss) = esc_double xs [] in strings (ys:rs) ss
strings rs (' ':xs) = let (ys, ss) = word xs [] in strings (ys:rs) ss
strings rs xs = let (ys, ss) = word xs [] in strings (ys:rs) ss
Posted: Sat May 10, 2008 12:11 am Post subject: Re: Exercise: Palindromes and Isograms
Quote:
btiffin: is parse a builtin function?
Yep, and I'm just using the string splitting feature. In block mode it includes Icon like pattern matching with to, thru charset matching, datatypes and literals, bobloblaw, with expression evaluation on every match step.
In the case I used, the rule was simply " ", meaning split using space as a delimiter. I really should have included ^- for tabs as well. And my code would probably FAIL given that I didn't bother to properly parse for backslash escaped quotes. REBOL uses caret for escaping.