Search results
May 31, 2009 · If you have an instructor, they probably want you to start with an empty string, and build up a new string from the old one. You can do this with pure syntax and literals using a while loop: def reverse_a_string_slowly(a_string): new_string = ''. index = len(a_string) while index: index -= 1 # index = index - 1.
2. It's using extended slicing - a string is a sequence in Python, and shares some methods with other sequences (namely lists and tuples). There are three parts to slicing - start, stop and step. All of them have default values - start defaults to 0, stop defaults to len (sequence), and step defaults to 1. By specifying [::-1] you're saying ...
Literally the first thing? You get the last character of the string, right? So the reverse of a string is the last character, followed by the reverse of everything but the last character, which is where the recursion comes in. The last character of a string can be written as x[-1] while everything but the last character is x[:-1].
Aug 9, 2015 · 3. Less code is usually faster in Python. Luckily, you don't have to guess: python -mtimeit -s"s='x'*100000" "for x in s[::-1]: pass". 100 loops, best of 3: 1.99 msec per loop. python -mtimeit -s"s='x'*100000" "for x in reversed(s): pass". 1000 loops, best of 3: 1.97 msec per loop. python -mtimeit -s"s='x'*100000" "for i in xrange(len(s)-1, 0-1 ...
Apr 30, 2016 · Common sense dictates that code with more lines should run slower. I reversed a the string in two ways. Implementation 1: "".join(reversed(map(lambda x:x,st))) Implementation 2: st[::-1] These have similar performance. Even for a 20000-character string, the difference is less than one millisecond. I think the first one should be a slower ...
Oct 15, 2010 · Method 1: Reverse in place with obj.reverse () If the goal is just to reverse the order of the items in an existing list, without looping over them or getting a copy to work with, use the <list>.reverse() function. Run this directly on a list object, and the order of all items will be reversed:
def string_to_list(string): '''function takes actual string and put each word of string in a list''' list_ = [] x = 0 #Here x tracks the starting of word while y look after the end of word. for y in range(len(string)): if string[y]==" ": list_.append(string[x:y]) x = y+1 elif y==len(string)-1: list_.append(string[x:y+1]) return list_ def list_to_reverse(list_): '''Function takes the list of words and reverses that list''' reversed_list = [] for element in list_[::-1]: reversed_list.append ...
May 3, 2015 · Consider that the string contains 3 parts: prefix, the part you want to reverse and suffix. Using Python notation, that means. s = "abcde" s[:1] # prefix s[1:4] # part to be reversed s[4:] # suffix Therefore, in order to reverse a substring in a string, you want to define the substring by left and right boundary, called lb and rb.
Nov 3, 2013 · read() returns the whole file in a single string. That's why when you reverse it, it reverses the lines themselves too, not just their order. You want to reverse only the order of lines, you need to use readlines() to get a list of them (as a first approximation, it is equivalent to s = f.read().split('\n')):
Here is a function based on the best, fastest, and most Pythonic answer above and in current Python 3 syntax: def reverse_hex(hex_string): if isinstance(hex_string, str): input_is_string = True hex_string = hex_string.encode() a = array.array('H', hex_string) a.reverse() output = a.tobytes() if input_is_string: return output.decode() else ...