RosettaCodeData/Task/Run-length-encoding/Mathematica/run-length-encoding-4.math

9 lines
255 B
Text
Raw Permalink Normal View History

2013-04-10 23:57:08 -07:00
RunLengthEncode[s_String] := StringJoin[
{ToString[Length[#]] <> First[#]} & /@ Split[StringSplit[s, ""]]
]
RunLengthDecode[s_String] := StringJoin[
Table[#[[2]], {ToExpression[#[[1]]]}] & /@
Partition[StringSplit[s, x : _?LetterQ :> x], 2]
]