Advertisement
karlicoss

the second one ;)

Jul 5th, 2011
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Control.Monad
  2. import Data.Char
  3. import Data.List
  4. import Data.Maybe
  5.  
  6. getLines :: Int -> IO [String]
  7. getLines n = replicateM n getLine
  8.  
  9.  
  10. mapX :: a -> [a -> b] -> [b] {- Maps a list of functions to one value -}
  11. mapX _ [] = []
  12. mapX x (f:fs) = [f x] ++ (mapX x fs)
  13.  
  14. map2 :: (a -> b -> c) -> [a] -> [b] -> [c] {- Maps a binary function to two lists -}
  15. map2 _ [] [] = []
  16. map2 f (ax:axs) (bx:bxs) = [f ax bx] ++ map2 f axs bxs
  17.  
  18. all' :: (a -> Bool) -> [a] -> Bool {- As to me,it's kinda wierd that default all behave as all [] == True :( -}
  19. all' _ [] = False
  20. all' f l  = all f l
  21.  
  22. fastPow :: Int -> Int -> Int {- No default fast multiplication in Haskell? -}
  23. fastPow x 0 = 1
  24. fastPow x a
  25.     | even a     = exp2 * exp2
  26.     | odd a      = exp2 * exp2 * x
  27.     where
  28.         exp2 = fastPow x (div a 2)
  29.  
  30.  
  31. splitByPredicate :: (a -> Bool) -> [a] -> [[a]]
  32. splitByPredicate _ [] = []
  33. splitByPredicate p l
  34.     | p (head l) == True = [takeWhile p l] ++ (splitByPredicate p (dropWhile p l))
  35.     | otherwise          = [takeWhile (not . p) l] ++ (splitByPredicate p (dropWhile (not . p) l))
  36.  
  37.  
  38. type CellType = Int
  39.  
  40. checkCellType :: String -> CellType
  41. checkCellType s
  42.     | length (splitByPredicate isDigit s) == 4 = 1
  43.     | length (splitByPredicate isDigit s) == 2 = 2
  44.     | otherwise                                = error "wrong string!"
  45.  
  46.  
  47. colStr2Inth :: String -> Int
  48. colStr2Inth [] = 0
  49. colStr2Inth s  = (colStr2Inth $ init s) * 26 + (ord (last s) - ord 'A')
  50.  
  51. colStr2Int :: String -> Int
  52. colStr2Int s = sum (map (fastPow 26) [0 .. (length s - 1)]) + colStr2Inth s
  53.  
  54. convertCellType2To1 :: String -> String
  55. convertCellType2To1 s =
  56.     let sp = splitByPredicate isDigit s
  57.     in "R" ++ (sp !! 1) ++ "C" ++ (show $ colStr2Int (sp !! 0))
  58.  
  59.  
  60. colInt2Strhh :: Int -> String
  61. colInt2Strhh n
  62.     | n < 26    = [chr (ord 'A' + n)]
  63.     | otherwise = (colInt2Strhh (div n 26)) ++ (colInt2Strhh (mod n 26))
  64.  
  65. colInt2Strh :: Int -> Int -> String
  66. colInt2Strh n l =
  67.     let hresult = colInt2Strhh n
  68.     in (replicate (l - length hresult)) 'A' ++ hresult
  69.  
  70. colInt2Str :: Int -> String
  71. colInt2Str n =
  72.     let powList = map (fastPow 26) [0 .. 100]
  73.         sumPowList = scanl1 (+) powList
  74.         fi = fromJust (findIndex (<0) (map2 (-) (replicate 100 n) sumPowList))
  75.     in colInt2Strh (n - (sumPowList !! (fi - 1))) (fi)
  76.  
  77. convertCellType1To2 :: String -> String
  78. convertCellType1To2 s =
  79.     let sp = splitByPredicate isDigit s
  80.     in colInt2Str (read (sp !! 3) :: Int) ++ (sp !! 1)
  81.  
  82.  
  83. convertCellType :: String -> String
  84. convertCellType s
  85.     | checkCellType s == 1 = convertCellType1To2 s
  86.     | checkCellType s == 2 = convertCellType2To1 s
  87.  
  88.  
  89. main = do
  90.     sn <- getLine
  91.     let n = read sn :: Int
  92.     sl <- getLines n
  93.     putStrLn $ unlines (map convertCellType sl)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement