web developer & system programmer

coder . cl

ramblings and thoughts on programming...


google devquiz in haskell

published: 21-10-2010 / updated: 21-10-2010
posted in: development, haskell, programming, tips
by Daniel Molina Wegener

Recently Google has announced an Android event, oriented to Developers. They have made a small quiz with few questions, and I’ve solved that Quiz in Haskell just for fun. Probably I can do it in Python, but this time I prefer to solve it in Haskell. Let’s see some questions:


question a

What does the following program (given in pseudo-code) print?

The pseudo-code for this question was as follows:

x = 6
y = 2
    if y > 1, then
        y = y * 2
    else
        x = x * 2
print (x + y)

And the solution in Haskell:

module Main where

simple :: Int -> Int -> Int
simple x y = if x > 1
             then y * 2 + x
             else x * 2 + y

main :: IO ()
main = do let x :: Int
              y :: Int
              z :: Int
              x = 6
              y = 2
              z = simple x y
              in putStrLn (show z)


question b

How many times does the following program print ‘hello’? (note: the for loop in our pseudo-language includes the endpoints, so, for example, 1 to 4 means 1, 2, 3, 4.)

The pseudo-code for this question is as follows:

for i = 1 to 7
    if i != 7, then
        for j = 1 to 8
            print 'hello'
module Main where

main :: IO ()
main = do let r :: String
              i :: Int
              s :: String
              h :: String
              h = "hellon"
              i = ( (last (filter (x -> x /= 7) [1..7])) * 8 )
              r = ( show i )
              s = concat (take i (repeat h))
              in putStrLn (s ++ "n" ++ r)


question c

Between 1945 and 5403 (inclusive), how many numbers are even and also divisible by 7? (Hint: you can write a program to help you calculate)

module Main where

main :: IO ()
main = do let r :: String
              i :: Int
              i = length ( filter
                           ( x -> x `mod` 2 == 0
                                   && x `mod` 7 == 0 )
                           [1945..5403] )
              r = (show i)
              in putStrLn r


question d

In the little-known nation of Undeclaredvariablestan, all phone numbers have 6 digits. The phone company imposes these rules on numbers: There cannot be two consecutive identical digits, because that’s lame; The sum of the digits must be even, because that’s cool; The last digit can’t be the same as the first, because that’s unlucky. So, given these very reasonable, mature and well-designed rules, how many phone numbers in the list below are valid?

module Main where

import Data.Char

numbers :: [String]
numbers = ["215887", -- a long list of numbers
           "694419"]

chk_sum :: String -> Bool
chk_sum s = (((sum (map digitToInt s)) `mod` 2) == 0)

chk_fl :: String -> Bool
chk_fl s = ((head s) /= (last s))

chk_seq :: String -> Bool
chk_seq s = do let m :: [Int]
                   m = (map digitToInt s)
                   in ((length
                        (filter
                         (x -> x)
                         (zipWith (==) m (tail m)))) == 0)

chk_all :: String -> Bool
chk_all s = ((chk_sum s) && (chk_fl s) && (chk_seq s))

main :: IO ()
main = do let b1 :: Int
              b1 = length (filter (x -> (chk_all x)) numbers)
              in putStrLn (show b1)

No coments yet.

post a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>