Ruby Quiz, Haskell Solution: Maximum Sub-Array

Posted: August 30th, 2009 | Author: Michel Rijnders | Filed under: Haskell, Ruby Quiz | No Comments »

The Quiz

Given an array of integers, find the sub-array with maximum sum. (The complete, original quiz is here.)

A Haskell Solution

module Main where

import Data.List (inits, maximumBy, tails)
import System (getArgs)

maxSubArray :: [Int] -> [Int]
maxSubArray =
  maximumBy (\ x y -> compare (sum x) (sum y)) . concatMap inits . tails

main :: IO ()
main = do
  args <- getArgs
  print (maxSubArray (read (head args) :: [Int]))

5 Must-have iPhone libraries

Posted: August 28th, 2009 | Author: Ward Bekker | Filed under: iPhone development | 1 Comment »

A list of  5 must-have iPhone libraries that we use at tty.nl for our iPhone dev needs:

#1 ASIHTTPRequest

The network API of Apple’s Cocoa Touch framework is not a pretty sight. It’s verbose and hard to put into use. The ASIHTTPRequest library by All-Seeing Interactive is high quality wrapper around the CFNetwork API that makes http communication trivial to implement. Love it!

#2 Json-framework

REST & JSON are here to stay. (Bye bye SOAP, won’t miss you!). The json-framework library enables your killer iPhone app to parse & generate strict JSON.

#3 Touchcode

Cocoa’s NSXML API is much too complex for the 95% of the use cases. For that 95% you should just use touchcode. It’s based on the Open Source libxml2 and supports most XPATH queries.

#4 Pinch analytics

Pinch analytics is best described as Google Analytics for your iPhone app. It allows you to track the amount of unique users, the time spent per user, device types and much more. And just like Google’s package it’s totally free.

#5 RegexKitLite

I was shocked to discover the Cocoa Touch framework doesn’t support Regular Expressions out of the box. No worries though: RegexKitLite allows you to continue to use your carefully crafted regular expressions.


Ruby Quiz, Haskell Solution: Happy Numbers

Posted: August 24th, 2009 | Author: Michel Rijnders | Filed under: Haskell, Ruby Quiz | No Comments »

The Quiz

Write a program that tells whether a given integer is happy. A happy number is found using the following process: Take the sum of the squares of its digits, and continue iterating this process until it yields 1, or produces an infinite loop. (The complete, original quiz is here.)

A Haskell Solution

module Main where

import System (getArgs)

digits :: Int -> [Int]
digits = map (\c -> read [c] :: Int) . show

happy :: Int -> Bool
happy n = happy' n []
  where
    s = sum . map (\x -> x * x) . digits
    happy' n ns
      | s n == 1      = True
      | s n `elem` ns = False
      | otherwise     = happy' (s n) (n : ns)

main :: IO ()
main = do
  args <- getArgs
  if happy (read (head args) :: Int)
    then putStrLn ":-)"
    else putStrLn ":-("
  return ()

Cake’s Progress

Posted: August 24th, 2009 | Author: Michel Rijnders | Filed under: Cake, Open Source Projects | No Comments »

Cake is progressing rather slowly at the moment. Its next release (0.2) is about receiving requests, and thus will involve lots of networking code. Since I know very little about network programming I am first working my way through "UNIX Network Programming", Volume 1.

In the meantime, to still do some Haskell coding, I’m trying to solve some Perl and Ruby quizzes in Haskell. I’ll post my solutions plus explanations here.


Perl Quiz, Haskell Solution: Plusified Equations

Posted: August 19th, 2009 | Author: Michel Rijnders | Filed under: Haskell, Perl Quiz of the Week | 1 Comment »

The Quiz

We are given two positive integers $L and $R we need to find Plusified
expressions of both for which Eval($E_L) == Eval($E_R). So what is a plusified
expression? It is an expression where we can choose whether to add a single
"+" between any consecutive digit. So for example the number 123 has the
following plusified expression:

  • 123
  • 12+3
  • 1+23
  • 1+2+3

So if we are given 123 and 96 we can form the following plusified equation:

  • 12+3 == 9+6

Your mission is to write a Perl program (or an equivalent program in any
programming language) that will find all solutions to the plusified equation
of two numbers given as input. To normalise the output we’ll rule that:

  1. The equations should be given one at each line.
  2. They will be sorted so consecutive digits will take precedence over "+"'s.
  3. A "+" has no surrounding spaces.
  4. The = sign does have a preceding and following space.

A Haskell Solution

Listing of the complete solution:

module Main where

import Data.List (iterate)
import System (getArgs)

split :: Char -> String -> [String]
split delim s =
  let (s', s'') = break (== delim) s
  in s' : case s'' of
        delim : _ -> split delim (tail s'')
            otherwise -> []

plusify :: String -> [String]
plusify ""       = [""]
plusify (c : "") = [c : ""]
plusify (c : cs) = concatMap (\cs' -> [c : cs', c : '+' : cs']) (plusify cs)

combis :: String -> String -> [(String, String)]
combis x y =
  [(l, r) | l <- plusify x, r <- plusify y, sum' l == sum' r]
    where sum' = sum . map (\s -> read s :: Int) . split '+'

main :: IO ()
main = do
  args <- getArgs
  mapM_ (putStrLn . \(l, r) -> l ++ " = " ++ r) (combis (args!!0) (args!!1))
  return ()