## Intro 🌐

Problem solving is an important skill, for your career and your life in general.

That's why I take interesting katas of all levels, customize them and explain how to solve them.

### Today's exercise

Today, another `7 kyu`

kata,

meaning we slightly increase the difficulty.

Source: Codewars

You and a group of friends are earning some extra money by re-painting the numbers on people's letterboxes for a small fee.

Since there are 10 of you in the group, each person just paints one digit, e.g. somebody will paint only the 1's, somebody else will paint only the 2's and so on...

At the end of the day you realise not everybody did the same amount of work.

To avoid any fights you need to distribute the money fairly. That's where this Kata comes in.

Write a function `paintLetterboxes`

, that accepts two parameter: `start`

and `end`

.

Given a `start`

number, e.g. `125`

,

and an `end`

number, e.g. `132`

,

return the frequency of all 10 digits painted,

e.g. `[1, 9, 6, 3, 0, 1, 1, 1, 1, 1]`

.

```
125: 1 x 1, 1 x 2, 1 x 5
126: 1 x 1, 1 x 2, 1 x 6
...
132: 1 x 1, 1 x 3, 1 x 2
```

Input: two numbers.

Output: an array of numbers.

## Thinking about the Solution 💭

First, we need to understand the exercise! If we don't understand it, we can't solve it!.

I think I understand the exercise (= what I put into the function and what I want to get out of it).

Now, I need the specific steps to get from input to output.

I try to do this in small baby steps:

- Create array with the number of every number between start and end
- Map each number into its split digits
- Reduce every number into one big array
- Create object with all numbers from 0 to 9
- Count every digit
- Return as array

Example:

- Input:
`125, 132`

- Create array with the number of every number between start and end:
`[125, 126, 127, 128, 129, 130, 131, 132]`

- Map each number into its split digits:
`[ [ '1', '2', '5' ], [ '1', '2', '6' ], ..., [ '1', '3', '2' ] ]`

- Reduce every number into one big array:
`[ '1', '2', '5', '1', '2', '6', ..., '1', '3', '2' ]`

- Create object with all numbers from 0 to 9:
`{ '0': 0, '1': 0, ... , '9': 0 }`

- Count every digit:
`{ '0': 1, '1': 9, ... , '9': 1 }`

- Return as array:
`[ 1, 9, 6, 3, 0, 1, 1, 1, 1, 1 ]`

- Output:
`[ 1, 9, 6, 3, 0, 1, 1, 1, 1, 1 ]`

✅

🥵

## Implementation (Explicit) ⛑

```
function paintLetterboxes(start, end) {
// create array with the number of every number between start and end
// const array = [...Array(end - start + 1).keys()].map(i => i + start);
const allNumbers = [...Array(end + 1).keys()].slice(start);
// map each number into its split digits
const splitIntoDigits = allNumbers.map((num) => String(num).split(""));
// reduce every number into one big array
// const allDigits = splitIntoDigits.reduce((acc, cur) => acc.concat(cur), []); // node < 11
const allDigits = splitIntoDigits.flat();
// create object with all numbers from 0 to 9
const startObject = [...Array(10).keys()].reduce(
(acc, cur) => ({ ...acc, [cur]: 0 }),
{}
);
// count every digit
const counted = allDigits.reduce(
(acc, cur) => ({ ...acc, [cur]: acc[cur] + 1 }),
startObject
);
// return as array
const result = Object.entries(counted).reduce(
(acc, cur) => [...acc, cur[1]],
[]
);
return result;
}
```

### Result

```
console.log(paintLetterboxes(125, 132));
// [ 1, 9, 6, 3, 0, 1, 1, 1, 1, 1 ] ✅
console.log(paintLetterboxes(2, 4));
// [ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 ] ✅
```

## Implementation (Implicit) ⛑

```
function paintLetterboxes(start, end) {
const startObject = [...Array(10).keys()].reduce(
(acc, cur) => ({ ...acc, [cur]: 0 }),
{}
);
const counted = [...Array(end + 1).keys()]
.slice(start)
.map((num) => String(num).split(""))
.flat()
.reduce((acc, cur) => ({ ...acc, [cur]: acc[cur] + 1 }), startObject);
return Object.entries(counted).reduce((acc, cur) => [...acc, cur[1]], []);
}
```

### Result

```
console.log(paintLetterboxes(125, 132));
// [ 1, 9, 6, 3, 0, 1, 1, 1, 1, 1 ] ✅
console.log(paintLetterboxes(2, 4));
// [ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 ] ✅
```

## Playground ⚽

You can play around with the code here

## Next Part ➡️

Great work!

Probably, this solution is way too complicated, but it was fun!

We learned how to use `...`

, `Array`

, `keys`

, `entries`

, `slice`

, `flat`

, `map`

, `reduce`

.

I hope you can use your new learnings to solve problems more easily!

Next time, we'll solve another interesting kata. Stay tuned!

If I should solve a specific kata, shoot me a message here.

If you want to read my latest stuff, get in touch with me!

## Further Reading 📖

## Questions ❔

- How often do you do katas?
- Which implementation do you like more? Why?
- Any alternative solution?

## Discussion (2)

My turned-out-to-be-similar solution:

First time I actually used

`flatMap`

:)-

Btw, IMHO, kata description in a code-box is not the best UX-wise because of additional scrolling...

or separators?

Hey Kostia,

yes, you are right,

will search for a better solution, thanks!