in Books, Doing Books, How to Solve it by Computer

How to Solve it by Computer #11

First we start with a starting sequence. Which evenly distributes the values:

Then we need the reminding values which can be calculated like this:

Afterwards we just generate a new sequence with X=1 one and Y=n-X zeros:

Now we just need to map this and got our desired output:

That’s quite unreadable, therefore I’ll write a short function:

And the function in action:

Works fine. Ok, the next step is to insert these additional white spaces. That is, we go into a list, take the first whitespace, insert additional one and go the next one.

The idea is pretty basic. We iterate through the sentence, if we see a whitespace, we add our additional white spaces, else we just add the current character.

Let’s see if it works:

Looks good, now let’s generalize the function:

It just applies all the functions on it. And here’s the function in action:

Works. It have the feeling that I made it harder than it’s actually is :D but it was a good learning experience.

Write a Comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.