0 votes

I am trying to get better with recursion, and one excercise I came upon was to revert a string. At first I was trying to solve this by any means possible, and my first code had ~50 lines. Step by step, improving it , and reading all the Google, I ended up with this:

func three(input):
if input == "":
    return input
    return input[-1] + three(input.resize(len(input) - 1))

The only problem is - there's no resize function with strings. Alternatively, I've seen solutions doing "return everything but the last symbol".
Here's the best explanation: https://stackoverflow.com/questions/5532902/python-reversing-a-string-using-recursion

This is guy's code:

def backward(text):
if text == "":
    return text
    return text[-1] + backward(text[:-1])

(Resembles something, huh?))
But. There's no "call everything but the last symbol" function/syntaxis in GDSript.

Or is there? Please, help.

asked 5 days ago in Projects by Dmitriy Romanov (15 points)

1 Answer

+1 vote
Best answer

I don't see why you need to resize the string. It's going to end up the same size isn't it?

Anyway, you want to usesubstr ( int from, int len ) and use 0 as from, and input len-1 as len, instead of input.resize(len(input) - 1)

answered 5 days ago by duke_meister (685 points)
selected 5 days ago by Dmitriy Romanov

Yeah, I'm blind. Was reading documentation top to bottom and bottom to top, but did not see that function. Thanks.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.