Piles of Boxes

Several days ago, I’ve joined a Hackathon that sponsored by Facebook.

As qualifying people to this event, they hand out an easy algorithm question. Shortly, you need to calculate how less time to move a bunch of boxes in same heigh.

Here is a solution for reference:


import collections

def box(boxesInPiles):
    sortBoxes = collections.OrderedDict(sorted(collections.Counter(boxesInPiles).items()))
    count = 0
    for index, key in enumerate(sortBoxes):
        count += index * sortBoxes[key]
    return count

print(box([4, 5, 5, 2, 4]))

Haskell moment

Here are some Haskell code chunks, which both are simple recursion algorithms.

Quicksort is a sort of poster child for Haskell because everyone does it to showcase how elegant Haskell is.

quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
let smallerSorted = quicksort [a | a <- xs, a <= x]
    biggerSoted = quicksort [a | a <- xs, a > x]
in smallerSorted ++ [x] ++ biggerSoted
maximum' :: (Ord a) => [a] -> a
maximum' [] = error "List is empty!"
maximum' [x] = x
maximum' (x:xs) = (max x (maximum' xs))

replicate' :: Int  -> b -> [b]
replicate' n x
    | n <= 0 = []
    | otherwise = (x:(replicate' (n-1) x))

take' :: Int -> [a] -> [a]
take' n (x:xs)
    | n <= 0 = []
    | otherwise = x:(take' (n-1) xs)

reserve' :: [a] -> [a]
reserve' [] = []
reserve' (x:xs) = (reserve' xs) ++ [x]

Be a survivor of a disaster

这两天全靠红牛和咖啡续命了。

11月1号考完了这学期的第一门Final,在皇家展览馆考的。看了一下考场的座次表,三千人一起考试真的是美滋滋。考场的“服务人员”态度也特别好,看我手画Burndown Chart,就贴心的给我递过来了一把尺子 (可能是看我手画的太惨不忍睹了)。

因为这个暑假有四个月的时间,待在家里的话虽然可以轻松许多,但是还是觉得趁还可以实习,应该多锻炼一下自己。本来以为离放假还有一段时间,打算等考完试再去找实习,但是前两天算了一下日期,我发现再不找实习估计就来不及了,于是赶紧把简历投了起来。运气还不错的是,有好几个大佬都给了机会让我试一试。考完试的第二天,也就是11月2号,我约了微软的面试。正好打算考完试休息放松一天,于是2号就被我完全腾空用来面试了。

Continue reading