Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Exercise: Palindromes and Isograms
Index -> General Programming
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: 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
Sponsor
sponsor
Saad




PostPosted: 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
Haskell:
import Char

separateWords :: String -> [String]
separateWords line@(x:xs) | x == '\"' = separateWords_ "" [] xs True
                          | otherwise = separateWords_ "" [] xs False

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 = filter isAlpha string

isIsograph :: String -> Bool
isIsograph string = not $ any (\letters -> length letters /= 1) $ group $ sort string
                   
main = getLine >>= \input -> print $ filter (\word -> isPalindrome word || isIsograph word) $ separateWords $ map (toLower) input
md




PostPosted: 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




PostPosted: 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




PostPosted: 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 Razz
Saad




PostPosted: 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.
code:
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
code:
>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


rizzix @ Fri May 09, 2008 8:32 pm wrote:
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 Razz


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




PostPosted: 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




PostPosted: 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 Razz
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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 Razz
btiffin




PostPosted: 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




PostPosted: 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
         
          esc_single ('\\':'\'':xs) rs = esc_single xs ('\'':rs)
          esc_single [] rs             = (reverse rs, [])
          esc_single ('\'':xs) rs      = (reverse rs, xs)
          esc_single (x:xs) rs         = esc_single xs (x:rs)
         
          esc_double ('\\':'"':xs) rs  = esc_double xs ('"':rs)
          esc_double [] rs             = (reverse rs, [])
          esc_double ('"':xs) rs       = (reverse rs, xs)
          esc_double (x:xs) rs         = esc_double xs (x:rs)
         
          word (' ':xs) rs = (reverse rs, xs)
          word (x:xs)   rs = word xs (x:rs)
          word []       rs = (reverse rs, [])
         
          isIsoOrPalin xs = let rs = filter ((/=) ' ') (map toLower xs)
                            in xs == reverse xs || nub rs == rs


btiffin: is parse a builtin function?
btiffin




PostPosted: 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. Smile

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. Sad REBOL uses caret for escaping.

Cheers
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 20 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: