Skip to content

Instantly share code, notes, and snippets.

@mfonism
Last active February 7, 2020 01:54
Show Gist options
  • Save mfonism/f450f0ca192aac8e4811a202b1690d43 to your computer and use it in GitHub Desktop.
Save mfonism/f450f0ca192aac8e4811a202b1690d43 to your computer and use it in GitHub Desktop.
My answer to the question on https://stackoverflow.com/questions/60105887/in-python-how-to-mix-two-sentence which was sadly closed before it could be determined that the question was indeed valid.

The Problem

def weave_words(first, second):
    ...

The Logic Behind the Solution

Let's do this by hand.

first = "aceg"
second = "kjihfdb"

First, you want to reverse the second word (and keep the first word as is):

first = "aceg"
second = "bdfhijk"

Then, you want to weave them together, picking one element from each word in turn, until one of them (the shorter word) runs out of elements, then you just tack the remaining elements in the other (the longer word) onto the result.

You can start by trimming the longer one so that it is equal in length to the shorter one.

first  = "aceg"
second = "bdfh"
remainder = "ijk"

Then you weave the equal length arms like so:

weave = "abcdefgh"
remainder = "ijk"

Then tack the remainder onto it:

weave = "abcdefgh"

Try to work with this logic to get a solution for yourself. If you don't eventually reach a satisfactory solution, feel free to reach out in the comment section under this answer. I'll provide you with code and explanation.

But first, you'll have to show me your own attempt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment