Published on

EPI 5.8 - Spreadsheet column encoding

Problem

Write a function that converts excel column IDs to corresponding integers with “A” corresponding to 1 and so on.

Solution

We will modify the solution in EPI 5.6 slightly so that we have:

indexOf :: Eq a => a -> [a] -> Int
indexOf item xs
    | length firstMatched == 0 = -1
    | otherwise = fst (head firstMatched)
    where
        matchesItem x = snd x /= item
        firstMatched = dropWhile matchesItem
                        (zip [0 .. (length xs) - 1] xs)


digitIndices :: [Char] -> [Int]
digitIndices digits = [ indexOf (chr i) digits | i <- [0 .. 255]]