# String Compression

StephanGoergen + 1 comment Simple solution in haskell using the

**group function**:import Data.List formatGroup :: String -> String formatGroup cs | length cs == 1 = [head cs] | otherwise = head cs : (show . length) cs compress :: String -> String compress s = concatMap formatGroup $ group s main = do s <- getLine putStrLn (compress s)

hiljusti + 0 comments https://hackage.haskell.org/package/base-4.10.1.0/docs/Data-List.html#v:group

Does feel a

*little*like cheating though, since I think the challenge here is supposed to be implementing the grouping bit :)

- RH
russ_hyde + 0 comments *Functional*scala solution:import scala.annotation.tailrec object Solution { def compress(xs: String): List[(Char, Int)] = { @tailrec def go(acc: List[(Char, Int)], ys: String): List[(Char, Int)] = { if(ys.isEmpty){ acc } else { val y: Char = ys.head val spans: (String, String) = ys.span(_ == y) go((y, spans._1.length) :: acc, spans._2) } } go(Nil, xs).reverse } def format_count_tuple[T](tups: List[(T, Int)]): String = { tups.map(x => { if(x._2 == 1){ s"${x._1}" } else { s"${x._1}${x._2}" } }).mkString("") } def main(args: Array[String]) { val xs = readLine println(format_count_tuple(compress(xs))) } }

blubberdiblub + 0 comments There's

`itertools.groupby()`

in Python, which would solve this problem with ease.Unfortunately, the

`group-by`

in Racket has different semantics.However, there's an elegant solution possible involving regexes.

- AM
axel_marqz + 0 comments --- F# solution ---

let compress (msg: string) = let (_, _, res) = (msg + " ") |> Seq.fold (fun acum i -> let (pc, n, resmsg: StringBuilder) = acum match pc with | p when n = 0 -> (i, 1, resmsg) | p when p = i -> (p, n+1, resmsg) | _ -> (i, 1, resmsg.Append(pc).Append(match n with | 1 -> "" | _ -> n.ToString())) ) (' ', 0, StringBuilder()) res.ToString()

eliaszx + 0 comments Haskell

comp [] _ = [] comp [c] 1 = [c] comp [c] n = [c] ++ show n comp (x:xs) n | x == head xs = comp xs (n+1) | n == 1 = [x] ++ comp xs 1 | otherwise = [x] ++ show n ++ comp xs 1 main = getLine >>= putStrLn . (flip comp 1)

- C
cyzhaa + 0 comments Haskell

compression ( x : y : xs ) count | y == x = compression ( y : xs ) ( count + 1 ) | otherwise = ( if count == 1 then x : compression ( y : xs ) 1 else x : ( show count ) ++ compression ( y : xs ) 1 ) compression ( x : [] ) count | count > 1 = x : show count | otherwise = [ x ] _compression = flip compression 1 main = getLine >>= putStrLn . _compression

Ozbaysa + 0 comments -- Enter your code here. Read input from STDIN. Print output to STDOUT doCalculation :: Char -> [Char] -> Int -> [Char] doCalculation y [] _ = [y] doCalculation y (x:xs) n = if y == x then [y] ++ doCalculationHelper x (xs) (n+1) else [y] ++ doCalculation x (xs) (0) where doCalculationHelper :: Char -> [Char] -> Int -> [Char] doCalculationHelper _ [] n' = show (n' + 1) doCalculationHelper y' (x':xs') n' = if y' == x' then doCalculationHelper x' (xs') (n'+1) else show (n' + 1) ++ doCalculation x' xs' 0 main :: IO() main = do line <- getLine let h = doCalculation (head line) (tail line) 0 putStr h

Here you go, guys. Solution without using the "import" statement. If you guys have any further question about my code or any further improvements, do not hesitate to write.

jbtule + 0 comments F# if you have trouble with things timing out. Trying to using System.Console.In rather than System.Console.ReadLine and strings.

meriororen + 0 comments Racket. It's a little bit unfriendly for racket to process strings I think.

#lang racket (define (num-to-list-of-char x) (string->list (number->string x))) (define (compress l [c 1]) (if (empty? (cdr l)) (cons (car l) (if (= 1 c) (list) (num-to-list-of-char c))) (if (char=? (car l) (cadr l)) (compress (cdr l) (+ c 1)) (if (> c 1) (cons (car l) (append (num-to-list-of-char c) (compress (cdr l)))) (cons (car l) (compress (cdr l))))))) (let ([q (read-line)]) (display (list->string (compress (string->list q)))))

Sort 16 Discussions, By:

Please Login in order to post a comment