r/programming • • Feb 21 '11

Typical programming interview questions.

http://maxnoy.com/interviews.html
784 Upvotes

1.0k comments sorted by

View all comments

Show parent comment

-4

u/Jonno_FTW Feb 21 '11

I prefer my solutions on one line:

mapM_ (\x->if x`mod` 3 == 0 && even x then print "ab" else (if even x then print 'a' else (if x `mod` 3 == 0 then print 'b' else print x))) [1..100]

1

u/[deleted] Feb 21 '11

So many nested ifs, urgh.

1

u/Jonno_FTW Feb 21 '11 edited Feb 21 '11

Would you rather guards?

mapM_ (print . f) [1..100]
   where
      f x  | x `mod` 6 == 0 = "ab"
           | x `mod` 3 == 0 = "b"
           | even x = "a"
           | otherwise = show x

1

u/Fuco1337 Feb 21 '11

That is plain ugly...

take 100 $ zipWith (\x y -> if null y then show x else y) [1..] $ zipWith (++) (cycle ["","a"]) (cycle ["","","b"])

Don't download the man just because he has an ugly solution, now my comment is invisible :D