P13 (**) Run-length encoding of a list (direct solution).
| 109 | |
| 110 | // P13 (**) Run-length encoding of a list (direct solution). |
| 111 | void 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. |
| 131 | void problem_14() |
no test coverage detected