# 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]]
```
```spreadsheet_column_encoding :: [Char] -> [Int]
sum [ digit_to_value digit pos | (pos, digit) <- dig_positions xs]
where
reverse_indexes xs = [(length xs) - 1, (length xs) - 2 .. 0]
dig_positions xs = zip  (reverse_indexes xs) xs
symbols = ['A' .. 'Z']
ourIndexes = digitIndices symbols
digit_to_value digit pos = (1 + (ourIndexes !! (ord digit))) *
floor (26 ** fromIntegral pos)

```

## One thought on “EPI 5.8 – Spreadsheet Column Encoding”

1. […] EPI 5.8 – Spreadsheet Column Encoding […]