[ create a new paste ] login | about

Link: http://codepad.org/udoDscmx    [ raw code | output | fork ]

mohit_at_codepad - Haskell, pasted on Mar 9:
-- Author Mohit Jain

-- Problem statement
-- Input: A string of characters. All characters are in range [a-z]
-- Output: All characters separated by commas except
--         Rule 1: If same character repeats n times, print character * n
--         Rule 2: If consecutive characters are subsequent, print from_char-to_char
--         Rule 3: If a character fall in both rule 1 and 2, apply rule 1 for it.

module Main where

-- http://www.haskell.org/onlinereport/list.html
-- import list to use intersperse and group
import List
-- import char to use ord and char
import Char

-- printMyWay takes a decoded String and encodes it
-- Here for testing I used many strings as list
-- And hence the map and unlines are added
main = putStrLn $ unlines $ map printMyWay ["a", "hoge", "aa", "aabc", "aaaabcdepqrrraa", "abcdez"]
-- main = do
--  args <- getArgs
--  putStrLn $ unlines $ map printMyWay args

printMyWay ::[Char]->String
-- This function encodes(converts) string that can be print readily
-- Scans input string and groups all characters that are same and write them in star format
-- Scans input string and groups all continguous characters into chain and write in dash format
printMyWay x =  finalString where                              -- Example: x = "aaaabcdepqrrraa"
       groupedChars = group x                                  -- Example: groupedChars  = ["aaaa", "b", "c", "d", "e", "p", "q", "rrr", "aa"]
       seggregatedList = map groupedStringToStar groupedChars  -- Example: seggregatedList = ["a*4", "b", "c", "d", "e", "p", "q", "r*3", "a*2"]
       dashedList = locateContinuousChars seggregatedList      -- Example: dashedList = ["a*4", "b-e", "p-q", "r*3", "a*2"]
       finalString = insertComma dashedList                    -- Example: finalString = "a*4, b-e, p-q, r*3, a*2"

insertComma ::[String]->String
-- This functions reads a String array and inserts
-- a ", " string between each element
-- And then concatenates each string and return
-- Test Input ["Mohit", "Jain"]
-- Output "Mohit, Jain"
insertComma x = concat $ intersperse ", " x

groupedStringToStar ::[Char]->String
-- This function converts grouped strings to *n pattter
-- [Rule 1]:: If input is 1 character, return unchanged
-- [Rule 2]:: If input is n character, return char*n
-- Test Input "aaaa"
-- Output "a*4"
groupedStringToStar all@(x:[]) =  all
groupedStringToStar all@(x:xs) =  [x] ++ "*" ++ (show $ length all)

locateContinuousChars ::[String]->[String]
-- This function finds 1 letter strings in array
-- And if strings are 1 letter long sub-sequent strings
-- It will add them all into 1 3 letter string in the form from-to
-- Test Input "abcdef"
-- Output "a-f"
locateContinuousChars [] = []
locateContinuousChars (x:xs) = insertDash x x (nextString x) xs

insertDash ::String->String->String->[String]->[String]
-- This is helper function for locateContiniousChars
-- Description of fromStr, toStr, expectedStr contains word letter, but they are actually 1 letter strings
-- fromStr: First letter in chain
-- toStr: Last letter in chain created so far
-- expectedStr: Next letter
-- remList: Remaining list
insertDash fromStr toStr _ [] = [dashedString fromStr toStr]
insertDash fromStr toStr expectedStr remList@(x:xs) = if expectedStr == x
                                                      then insertDash fromStr x (nextString x) xs
                                                      else dashedString fromStr toStr : locateContinuousChars remList

dashedString ::String->String->String
-- If chain contains 1 element, output is without dash
-- If chain contains more than 1 elements, output is of the form from-to
-- x: first element in char
-- y: last element in char
dashedString x y = if x == y then x else x ++ "-" ++ y

nextString ::String->String
-- Utility to find next character of string
-- If input string contains more than 1 characters, output is same
-- If input is 1 character long, output is a string containing next character
nextString (x:[]) = [chr $ 1 + ord x]
nextString all@(x:xs) = all


Output:
1
2
3
4
5
6
7
a
h, o, g, e
a*2
a*2, b-c
a*4, b-e, p-q, r*3, a*2
a-e, z



Create a new paste based on this one


Comments: