I want to understand why code 1 is throwing the below error"AttributeError: 'NoneType' object has no attribute 'append'"
def reverse(s): if len(s) == 1: return list(s) else: return reverse(s[1:]).append(s)
the same logic works if "s" is a string
s="hello"def reverse(s): if len(s) == 1: return s else: return reverse(s[1:]) + s
Can someone help me understand this?
Say you have this string:
And you want to reverse it so that it becomes:
What would be the most efficient / pythonic solution? I've tried a few different things but they all look horrible...
Thanks in advance!
In case anyone's interested, this wasn't for homework. I had a script that was processing data from a network capture and returning it as a string of hex bytes. The problem was the data was still in network order. Due to the way the app was written, I didn't want to go back through and try to use say socket.htons, I just wanted to reverse the string.
Unfortunately my attempts seemed so hideous, I knew there must be a better way (a more pythonic solution) - hence my question here.
How do you reverse a string in C or C++ without requiring a separate buffer to hold the reversed string?
I'm trying to implement my own algorithm to reverse a string using a list, such as
import restring = "alice"string = re.findall('.',string)length = len(string)for x in range(1,int(length/2)+1): first = string[x-1] last = string[length-x] string[x-1]=last string[length-x]=firststring = "".join(string)print(string)
What might be a more efficient way to reverse a string in terms of time complexity, space complexity or both (using a list or without using a list would be both OK)?