Reverse string issue plz help


#1

https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/reverse?action=lesson_resume

def reverse(x):
  p=len(x)
  q=p/2
  for  i in range(q):
    x[i]=x[p-1]
  return x

it aint wrking


#2

Think your code out step by step. Write out what you expect to happen to each variable after each statement. What do you expect the output to be? What is your actual output?


#3
q = p / 2

That to me suggests a swing pivot. By knowing a pivot point we can swing the data points within range around that point. However it will need to be qualified as to swing length. Even and odd lengths are treated differently in the logic.


#4

I’d implement something more like this

A c++ example

string backwardString(string stringObject)
{
	string backString = stringObject;
	for (int i=0; i < stringObject.size(); i++)
		backString[stringObject.size()-1 -i] = stringObject[i];
	return backString;
}

#5

One would have to start from scratch to re-create what was once a QBasic game of Reversii that I called Golfer. It was originally conceived in OS-9 on a CoCo3. Some years later I was trying to conceive it again on a PC and wrote out the algorithm on paper while in the crapper.

If I ever find a copy of it again, I’ll be sure to exploit the REPL IT ability to run old code. But wait… Didn’t I post the code a year or two back?

Update

Don’t know where it is locally, but lo, it came up in a search of this site…

Irfanview, QBasic items from the archives

Off to see if it still works…

Well, not exactly. Wrinkles right out of the gate. It’s been at least 15 years since doing any QBasic, so it will take some brushing up. Hopefully I can get it up and running over the holidays.


#6

I want a string like " abcd " to get reversed into “dcba”


#7

You can do that by “pythoning” up the c++ code I posted.
It’s not that much different at all. The science is the same.

Once again,
Which parts are you struggling with?
There are several ways to do anything in programming it is best that you understand why they work, not simply ‘if’ they work.


#8
def reverse(x):
  p = len(x)
  q = p / 2
  for i in range(q):
    x[i] = x[p - 1]
  return x

Looking at the code we can predict the exception… Strings are immutable, and the above attempts to modify the string, in place.

Traceback (most recent call last):
  File "python", line 9, in <module>
  File "python", line 5, in reverse
TypeError: 'str' object does not support item assignment

What happens if we convert the string to a list?

def reverse(x):
  x = list(x)
  p = len(x)
  q = p / 2
  for i in range(q):
    x[i] = x[p - 1]
  return ''.join(x)

No errors but we can see from the output something is wrong…

!!!hon!

Couple things we can try, first, reduce p by 1, and second, change p - 1 to p - i

def reverse(x):
  x = list(x)
  p = len(x) - 1
  q = p / 2
  for i in range(q):
    x[i] = x[p - i]
  return ''.join(x)

Still not working correctly.

!nohon!

Getting closer. Now we can see what is happening, and what is not happening. We are not swapping characters.

x[i] = x[p - i]

should be,

x[i], x[p - i] = x[p - i], x[i]

That solves it!

!nohtyP

The following is a slightly different take on the same concept…

def reverse(text):
  text = list(text)
  for x in range(int(len(text) / 2)):
    y = -1 - x
    text[x], text[y] = text[y], text[x]
  return ''.join(text)

print (reverse("Python!"))

#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.