MCPcopy Create free account
hub / github.com/Dobiasd/FunctionalPlus / problem_13

Function problem_13

examples/99_problems.cpp:111–128  ·  view source on GitHub ↗

P13 (**) Run-length encoding of a list (direct solution).

Source from the content-addressed store, hash-verified

109
110// P13 (**) Run-length encoding of a list (direct solution).
111void problem_13()
112{
113 const auto f = [](const Intss& acc, int x) -> Intss {
114 if (is_empty(acc)) {
115 return singleton_seq(singleton_seq(x));
116 } else if (size_of_cont(acc.back()) == 1 && acc.back().back() == x) {
117 return replace_elem_at_idx(size_of_cont(acc) - 1, { 2, x }, acc);
118 } else {
119 if (acc.back().back() == x) {
120 return replace_elem_at_idx(
121 size_of_cont(acc) - 1, { acc.back().front() + 1, x }, acc);
122 } else {
123 return append_elem(singleton_seq(x), acc);
124 }
125 }
126 };
127 print_result(fold_left(f, Intss(), xs));
128}
129
130// P14 (*) Duplicate the elements of a list.
131void problem_14()

Callers 1

mainFunction · 0.85

Calls 7

print_resultFunction · 0.85
is_emptyFunction · 0.50
singleton_seqFunction · 0.50
size_of_contFunction · 0.50
replace_elem_at_idxFunction · 0.50
append_elemFunction · 0.50
fold_leftFunction · 0.50

Tested by

no test coverage detected