1

Thank you for all the answers. I got why my code is wrong.

Please don't provide me with solutions because I do have a few but then I want to understand my code is not working.

I think it is due to while x <= -len(split_result):" but then the I think the logic is correct. What is wrong with my code?

O_string = ("Me name is Mr_T")
split_result = O_string.split()
print(split_result)

x=0
list=[]

while x <= -len(split_result):
    list.append(split_result[x-1])
    x = x-1

result=" ".join(list)
print (result)
Alexis
  • 19
  • 1
  • 1
  • 3
  • 2
    `' '.join('Hello world'.split()[::-1])` – Klaus D. Jan 05 '17 at 12:13
  • 1
    You're checking if `x` is less than or equal to a negative value after initialising as `x=0`, so you'll never enter your `while` loop – asongtoruin Jan 05 '17 at 12:20
  • This was closed as a duplicate of a question asking how to reverse the order of *characters* in a *string*. However, this question is different: it's asking how to reverse the order of *words* (separated by a space character) in a *sentence*. – Anthony Geoghegan May 06 '19 at 18:06

6 Answers6

8

You can reverse a list with [::-1]:

print(' '.join(O_string.split()[::-1]))

Output:

'Mr_T is name Me'

Here [::-1] means take everything from the beginning to the end with a step size of minus one.

Alternatively, you can use the built-in function reversed:

>>> ' '.join(reversed(O_string.split()))
'Mr_T is name Me'

About your algorithm. In my opinion it is always more difficult to think in negative indices. I would suggest to go positive:

O_string = ("Me name is Mr_T")
split_result = O_string.split()

res = []
x = len(split_result) - 1
while x >= 0:
    res.append(split_result[x])
    x = x-1

result=" ".join(res)
print (result) 

Output:

'Mr_T is name Me'

Here:

x = len(split_result) - 1

gives you the last index of your list. We start indexing with 0. So you need to subtract 1 from the length of the list.

You count it down with:

x = x-1

and stop as soon as you get a negative index:

while x >= 0:

Hint: Don't use list as a variable name. It is a built-in and should better not be used for naming own objects. If you do, you cannot easily use list() anymore (in the same namespace).

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
1

can make use of the reverse function as well as str.join

O_string = ("Me name is Mr_T")
split_result = O_string.split()
split_result.reverse()
print " ".join(split_result)
e4c5
  • 52,766
  • 11
  • 101
  • 134
1

In python, list[::-1] will give you the list in which all the elements of the list stored in reverse index position. ie.) reverseList1=list1[::-1] use this.

宏杰李
  • 11,820
  • 2
  • 28
  • 35
1

To answer "What is wrong with my code?", let's look at what happens in a shell:

>>> O_string = ("Me name is Mr_T")
split_result = O_string.split()
print(split_result)
['Me', 'name', 'is', 'Mr_T']
>>> x=0
>>> len(split_result)
4
>>> -len(split_result)
-4
>>> x
0
>>> 0 <= 4
True
>>> 0 <= -4
False
>>> x <= -len(split_result)
False
>>> while False: print('this will not get printed')

So, your while loop condition will never be true and the loop will never happen. Here is an example of what works:

x = -1
while x >= -len(split_result):
    list.append(split_result[x])
Alex Hall
  • 34,833
  • 5
  • 57
  • 89
0

You can try:

>>> O_string = ("Me name is Mr_T")
>>> O_list = O_string.split()
>>> O_list.reverse()
>>> " ".join(O_list)
'Mr_T is name Me'
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
0

You can use reversed to reverse the sentence:

o_string = "Me name is Mr_T"
words = sentence.split()
o_string = " ".join(reversed(words))
print(o_string)
Vagif
  • 224
  • 1
  • 3
  • 17