Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Control.Monad
- import Data.Char
- import Data.List
- import Data.Maybe
- getLines :: Int -> IO [String]
- getLines n = replicateM n getLine
- mapX :: a -> [a -> b] -> [b] {- Maps a list of functions to one value -}
- mapX _ [] = []
- mapX x (f:fs) = [f x] ++ (mapX x fs)
- map2 :: (a -> b -> c) -> [a] -> [b] -> [c] {- Maps a binary function to two lists -}
- map2 _ [] [] = []
- map2 f (ax:axs) (bx:bxs) = [f ax bx] ++ map2 f axs bxs
- all' :: (a -> Bool) -> [a] -> Bool {- As to me,it's kinda wierd that default all behave as all [] == True :( -}
- all' _ [] = False
- all' f l = all f l
- fastPow :: Int -> Int -> Int {- No default fast multiplication in Haskell? -}
- fastPow x 0 = 1
- fastPow x a
- | even a = exp2 * exp2
- | odd a = exp2 * exp2 * x
- where
- exp2 = fastPow x (div a 2)
- splitByPredicate :: (a -> Bool) -> [a] -> [[a]]
- splitByPredicate _ [] = []
- splitByPredicate p l
- | p (head l) == True = [takeWhile p l] ++ (splitByPredicate p (dropWhile p l))
- | otherwise = [takeWhile (not . p) l] ++ (splitByPredicate p (dropWhile (not . p) l))
- type CellType = Int
- checkCellType :: String -> CellType
- checkCellType s
- | length (splitByPredicate isDigit s) == 4 = 1
- | length (splitByPredicate isDigit s) == 2 = 2
- | otherwise = error "wrong string!"
- colStr2Inth :: String -> Int
- colStr2Inth [] = 0
- colStr2Inth s = (colStr2Inth $ init s) * 26 + (ord (last s) - ord 'A')
- colStr2Int :: String -> Int
- colStr2Int s = sum (map (fastPow 26) [0 .. (length s - 1)]) + colStr2Inth s
- convertCellType2To1 :: String -> String
- convertCellType2To1 s =
- let sp = splitByPredicate isDigit s
- in "R" ++ (sp !! 1) ++ "C" ++ (show $ colStr2Int (sp !! 0))
- colInt2Strhh :: Int -> String
- colInt2Strhh n
- | n < 26 = [chr (ord 'A' + n)]
- | otherwise = (colInt2Strhh (div n 26)) ++ (colInt2Strhh (mod n 26))
- colInt2Strh :: Int -> Int -> String
- colInt2Strh n l =
- let hresult = colInt2Strhh n
- in (replicate (l - length hresult)) 'A' ++ hresult
- colInt2Str :: Int -> String
- colInt2Str n =
- let powList = map (fastPow 26) [0 .. 100]
- sumPowList = scanl1 (+) powList
- fi = fromJust (findIndex (<0) (map2 (-) (replicate 100 n) sumPowList))
- in colInt2Strh (n - (sumPowList !! (fi - 1))) (fi)
- convertCellType1To2 :: String -> String
- convertCellType1To2 s =
- let sp = splitByPredicate isDigit s
- in colInt2Str (read (sp !! 3) :: Int) ++ (sp !! 1)
- convertCellType :: String -> String
- convertCellType s
- | checkCellType s == 1 = convertCellType1To2 s
- | checkCellType s == 2 = convertCellType2To1 s
- main = do
- sn <- getLine
- let n = read sn :: Int
- sl <- getLines n
- putStrLn $ unlines (map convertCellType sl)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement