0

I am new to python and I want to make the first character from the first string, followed by the last character from the second string like this

    >>>String("aceg", "kjihfdb")
     abcdefghijk
Robert
  • 25
  • 2
  • It seems like you'd just want to reverse the second string, then interleave the two. That's two distinct operations, and searching should result yield results for both. – Carcigenicate Feb 07 '20 at 01:26
  • 1
    @Carcigenicate, I voted to close for... needing more detail, maybe? Anyway, I didn't vote for that duplicate. I'm not crazy about how SO shows that now as it often misrepresents close votes. – ChrisGPT was on strike Feb 07 '20 at 01:31
  • 2
    @Carcigenicate: I voted to reopen. – l'L'l Feb 07 '20 at 01:33
  • @Chris Oh, that's weird. I thought you hammered it (although now that I'm checking, I see that it doesn't show a gold next to your name. Sorry). – Carcigenicate Feb 07 '20 at 01:37
  • This question is not a duplicate of what's linked. This doesn't border on simple string concatenation. Needs to be **unclosed**. – mfonism Feb 07 '20 at 01:38
  • @Carcigenicate, there appear to be _two_ Chris users who voted to close. I don't have a gold [tag:python] badge. The other Chris does. – ChrisGPT was on strike Feb 07 '20 at 01:41
  • 2
    I feel that this should be closed for other reasons: it needs a clear problem statement, rather than "I don't know how to do this." – Prune Feb 07 '20 at 01:42
  • 1
    ... and sure enough, my "reopen" vote failed because others beat me to it. Now ... – Prune Feb 07 '20 at 01:43
  • 1
    This is a beginner we're chasing away. Just keep that in mind. – mfonism Feb 07 '20 at 01:43
  • 3
    Welcome to StackOverflow. [On topic](https://stackoverflow.com/help/on-topic), [how to ask](https://stackoverflow.com/help/how-to-ask), and ... [the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) apply here. StackOverflow is a knowledge base for *specific* programming problems -- not a coding or tutorial resource. This exercise is to make you learn some string manipulation. You've already identified that you need to reverse the second string and interleave with the first. Show us your attempt to code this. – Prune Feb 07 '20 at 01:44
  • @Robert, please have a look at this gist I created just for you https://gist.github.com/mfonism/f450f0ca192aac8e4811a202b1690d43 — where I explain the logic you need to create you code. – mfonism Feb 07 '20 at 01:45
  • If you get stuck, we'll help the specific needs, but walking you through the logic to a solution is about three steps outside the Stack Overflow charter. Meet us part-way, and we should be able to get you through the rest. – Prune Feb 07 '20 at 01:46
  • 1
    @Prune, the OP hasn't identified that they need to reverse the second string and interleave with the first. Someone else did that. In my experience, most people who ask questions like this have no clue how to start. But a gentle nudge in the right direction will get them firing on all cylinders. We shouldn't be too ready to close n00b1e questions. So, we don't want to spoon-feed them with the code — that's okay... but let's hold their hands and teach them how to think! – mfonism Feb 07 '20 at 01:48
  • All your answer is in [Python tutorial: 3.1.2 Strings](https://docs.python.org/3/tutorial/introduction.html#strings), please read it, then try some code. `s[0]` gets you the 0'th character from `s`, for example. – smci Apr 07 '20 at 22:16

4 Answers4

4

First, let me make it clear that it's always best practice to provide a rundown of what attempts you have made to tackle the problem. This way, it makes it easier for other people to point out where you might have gone wrong and lead you to the right direction.

This point notwithstanding, here's one way to approach the problem using basic recursion.

def String(str1, str2):
    if len(str1) == 0:
        return str2[::-1]
    elif len(str2) == 0:
        return str1
    else:
        return str1[0] + str2[-1] + String(str1[1:], str2[:-1])

As with any recursion problem, we establish a base case. In the context of this question, the base case would be if any of the arguments of the String function are empty. If the first argument is empty, we have to reverse the second string; conversely, if the second string is of length 0, we simply return the first string as it is. If neither of them are empty, we invoke a recursive call on the function, as shown in the last else statement. Specifically, we concatenate the first character of the first string and the last character of the last string with the returned result of the recursive call.

If anything is unclear, I'd be happy to answer any additional questions you might have.

Jake Tae
  • 1,681
  • 1
  • 8
  • 11
0
def weave_words(first, second):
    ...

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.

mfonism
  • 535
  • 6
  • 15
0

Funny oneline solution :

def String(a, b):
    return ''.join(''.join((a[index], b[-index - 1])) for index in range(min(len(a), len(b)))) + (b[-len(a) - 1::-1] if len(a) < len(b) else a[len(b) or 1 - 1:])


print(String(a, b))
ErnestBidouille
  • 1,071
  • 4
  • 16
0

With some itertools...

>>> ''.join(chain(*zip_longest(s, reversed(t), fillvalue='')))
'abcdefghijk'
Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65