7. Reverse


Hi guys
Idk why this is happening, but this runs smoothly in my local Python interpreter. And also, the error message is not consistent with the output. Please check the image.


your function prints the reversed string, it doesn't return the reversed string


Thank you, i feel stupid now.


Hi, i have almost same code as was swritten above but i have a Python error

Why so?


look at the error in the output window? Why did you indent the line while loop more then the line above?


Can someone please explain me why on line 5 we should type:

result += text[i - 1]

and not just: result += text[i] ?

I have a problem understanding how text[i - 1] can indicate that the loop starts from the last letter of a word and not the second to last !


because strings are zero indexed based, while i (containing length of string) starts counting at one. So if we have a string with a length of 5 (for example: hello), the length is 5, while the indexes are 0 1 2 3 and 4, this is why we subtract one


Thank you so much for your quick and clear answer ! It all makes sense now.


I thought of solving it in a slightly different way:

def reverse(text):
  n = 1
  length = len(text) + 1
  reversedtext = ''
  while length > n:
    reversedtext = reversedtext + text[-n]
    n += 1
  return reversedtext

How much does it matter in how many lines a problem should be solved? Should I be working harder towards using less lines of code?


lines of code is not the only relevant factor, other factors:


If you really want to go there already, the first thing i would improve on your program. look at the below examples:

# example 1
new_string = ''
new_string += 'a'
new_string += 'b'

# example 2 
new_list = []

So, for example 1 we add a to new string. so new string now contains one character (one byte), then we add b to new string. But strings are immutable. So a new string with the combined result is created. This means, in memory i have now 1 byte (a) and 2 bytes (ab), totaling 3 bytes

for example 2, lists are mutable, so when we have cd, our list takes 2 bytes (vs 3 bytes for new_string)

this goes pretty rapidly once we add more letters. If we add another letter to new_string, we would have 6 bytes (1 + 2 + 3) vs only 3 bytes for new_list

now, for a program this small it doesn't matter, this difference won't be noticeable. But as your program increases and a whole lot of text need reversing, you will start to notice this

additional information:
8 bits == 1 byte
1024 bytes == 1kb
1024kb == 1mb


Thank you for the tip stetim94!

I hadn't heard it talked about in ways of using less bytes. That makes total sense.

I see now why lists are so important!


string manipulation is expensive, always. If you are optimizing, always be very careful about strings, on the other hand, to quote donald knuth:

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.”

Sometimes, worrying about small parts of your program takes more time of the programmer then that it yields in code efficiency.


I like that.
I'll keep that in mind.
Creating the solution quickly should be the main focus for time's sake.