
-----------------------------------
wtd
Fri May 09, 2008 1:31 am

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.

-----------------------------------
Saad
Fri May 09, 2008 4:50 pm

Re: Exercise: Palindromes and Isograms
-----------------------------------
Haven't coded in haskell for a while but I decided to try it anyway
import Char

separateWords :: String -> 

-----------------------------------
md
Fri May 09, 2008 5:40 pm

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
Fri May 09, 2008 8:26 pm

RE:Exercise: Palindromes and Isograms
-----------------------------------
Here's mine: < see code below >

Edit: syntax tags are getting to be really annoying

-----------------------------------
rizzix
Fri May 09, 2008 8:32 pm

Re: 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?

An isogram is-a palindrome, so just check for palindromes :P

-----------------------------------
Saad
Fri May 09, 2008 8:51 pm

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.
import Char
import List

separateWords :: String -> [String]
separateWords line@(x:xs) | x == '"' = separateWords_ "" [] xs True
                          | otherwise = separateWords_ [x] [] xs False
    where
        separateWords_ :: String -> [String] -> String -> Bool -> [String]
        separateWords_ currentWord allWords (x:xs) True | x == '"' = separateWords_ "" (allWords ++ [currentWord]) xs False
                                                        | otherwise = separateWords_ (currentWord ++ [x]) allWords xs True
                                                        
        separateWords_ "" allWords [] _ = allWords
        separateWords_ "" allWords (x:xs) False | x == '"' = separateWords_ "" (allWords) xs True
                                                | x == ' ' = separateWords_ "" (allWords) xs False
                                               
        separateWords_ currentWord allWords (x:xs) False| x == '"' = separateWords_ "" (allWords ++ [currentWord]) xs True
                                                        | x == ' ' = separateWords_ "" (allWords ++ [currentWord])  xs False
                                                        | otherwise = separateWords_ (currentWord ++ [x]) allWords xs False
        separateWords_ currentWord allWords [] _ = allWords ++ [currentWord]

isPalindrome :: String -> Bool
isPalindrome string = word == reverse word
                where
                    word = map toLower $ filter isAlpha string

isIsograph :: String -> Bool
isIsograph string = all (\letters -> length letters == 1) $ group $ sort string
                   
main = getLine >>= \input -> mapM_ (print) $ filter (\word -> isPalindrome word || isIsograph word) $ separateWords input

And a sample output 
>ghc a.hs
>Exit code: 0
>main
Race qux foo foof Saad sad "race car" "cars"
"Race"
"qux"
"foof"
"sad"
"race car"
"cars"
>Exit code: 0


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 :P

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 [url=http://en.wikipedia.org/wiki/Isogram]here

-----------------------------------
richcash
Fri May 09, 2008 8:56 pm

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
Fri May 09, 2008 9:08 pm

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 :P

-----------------------------------
wtd
Fri May 09, 2008 10:06 pm

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
Fri May 09, 2008 10:31 pm

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
Fri May 09, 2008 10:38 pm

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
Fri May 09, 2008 10:45 pm

Re: RE:Exercise: Palindromes and Isograms
-----------------------------------
Isograms for the purposes of this exercise are case-insensitive.

You know that only makes it longer, not shorter :P

-----------------------------------
btiffin
Fri May 09, 2008 10:47 pm

Re: Exercise: Palindromes and Isograms
-----------------------------------
Here's one in REBOL.  It does not handle escaped quotes with backslash, but caret instead.
#!rebol -c
rebol 
$ ./palins.r
? "Naomi, sex at noon taxes, I moan" aba "was saw" shockingly


-----------------------------------
rizzix
Fri May 09, 2008 11:10 pm

RE:Exercise: Palindromes and Isograms
-----------------------------------
Here everything inclusive of the case-check: import List (nub)
import Char (toLower)

main = getLine >>= (mapM_ putStrLn) . (filter isIsoOrPalin) . strings 

btiffin: is parse a builtin function?

-----------------------------------
btiffin
Sat May 10, 2008 12:11 am

Re: Exercise: Palindromes and Isograms
-----------------------------------
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. 

Cheers

-----------------------------------
btiffin
Sat May 10, 2008 1:48 am

Re: Exercise: Palindromes and Isograms
-----------------------------------
Update to the REBOL version.  I was so excited about getting a chance to use  alter that I skipped the obvious, and I added tabs to the delimters this time.  alter is short for alternate, it scans a series! and either inserts or removes the value.  Its use is normally limited to handling flags, so I thought this would be another use, but unique is more appropriate.
#!rebol -c
rebol 
wtd;  Your exercises are cutting into my breaks!  :)

-----------------------------------
Saad
Sat May 10, 2008 11:50 am

Re: Exercise: Palindromes and Isograms
-----------------------------------
Saad Shouldn't you account for single-quoted strings?


Correct again. However I realised a better way of doing it with help from regular expressions

Was done with GHC version 6.8.2

Compiled via ghc  -package regex-compat-0.71.0.1
import List (sort, group)
import Char (toLower, isAlpha)
import Text.Regex

main = getLine >>= \input -> mapM_ putStrLn $ filter (\word -> isPalindrome word || isIsograph word) $ separateWords 

Basically I replace all the quotation types with a ~ and then its simple string manipulation

-----------------------------------
rizzix
Sat May 10, 2008 2:10 pm

RE:Exercise: Palindromes and Isograms
-----------------------------------
what if the word was 'abc ~ def'

-----------------------------------
Saad
Sat May 10, 2008 2:22 pm

Re: RE:Exercise: Palindromes and Isograms
-----------------------------------
what if the word was 'abc ~ def'

It was done assuming that the words entered would be valid words.

However another version based on the same idea but with support of characters like ~ (although wtd could make what he wants more clear if he wants that kind of support or not)
import List (sort, group)
import Char (toLower, isAlpha)

main = getLine >>= \input -> mapM_ putStrLn $ filter (\word -> isPalindrome word || isIsograph word) $ separate input
    where
        separate :: String -> 

-----------------------------------
rizzix
Sat May 10, 2008 2:39 pm

Re: Exercise: Palindromes and Isograms
-----------------------------------
Here's another approach to this, using Parser Combinators.

import scala.util.parsing.combinator.lexical._;
import scala.util.parsing.input.CharArrayReader.EofCh

object Test extends Application {
    case class Word(str : String)
    
    def isIsoPalin(s : List

Edit: fixed some bugs, cleaned up code.
