Yahoo India Web Search

Search results

  1. 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. 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].

  3. 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 ...

  4. Feb 7, 2014 · For string[0::-1], it's because you tell Python to start at index 0 and go backwards. Of course it doesn't reverse the string. You should do string[len(string)-1::-1] (or surprisingly also string[len(string)::-1]) to get the string reversed like mhlester has said in his answer. – justhalf. Feb 7, 2014 at 1:37.

  5. 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 ...

  6. 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:

  7. I think this answer is wrong. It gives the index of the first 'I', not what the OP wanted. To find the 2nd 'I', s.find('I', s.find('I')+1) returns 16. To find the last 'I', s.rfind('I') would work. To find the indeces of all 'I' occurrences: locs = [idx for idx,ltr in enumerate(s) if ltr == 'I'] – John.

  8. 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 ...

  9. 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 ...

  10. Mar 17, 2015 · Ever since Python 1.4, the slicing syntax has supported an optional third step'' orstride'' argument. For example, these are all legal Python syntax: L[1:10:2], L[:-1:1], L[::-1]. This was added to Python at the request of the developers of Numerical Python, which uses the third argument extensively.

  1. People also search for