August 23, 2024

P11 - Modified run-length encoding.

Modify the result of problem P10in such a way that if an element has no duplicates it is simply copied into the result list. Only elements with duplicates are transferred as(N, E)terms.

Example:

> p11:encodeModified(['a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e']).
    [{4,a},b,{2,c},{2,a},d,{4,e}]

erlang

%Modified run-length encoding.

-module(p11).
-export([encodeModified/1]).

encodeModified(Ls) ->
    A = p10:encode(Ls),    
    lists:map(fun({Value, Elem}) -> 
            if 
                Value == 1 ->
                    Elem;
                Value > 1 ->
                    {Value, Elem}
            end
        end, A).   
Be first to comment
Leave a reply