Works, but CC fails it

#1

I’m testing following code on python 2.7 and it works. But Code Academy fails this code.
Could you explain why?

def reverse(x):
return [ x[i * -1] for i in range(1, len(x) + int(1)) ]

2 Likes
FAQ: Learn Python - Practice Makes Perfect - reverse
FAQ: Learn Python - Practice Makes Perfect - reverse
#2

Does the exercise instruct to return a string, in reverse of the input? The above returns a list.

Another concern is that it uses a concept (comprehension) that is not introduced until the Advanced Topics unit. Clearly the author believes we still have enough tools to complete this task.

Not sure what effect is expected? Is 1 not already an integer?

Say we want your code to return a string, the advanced nature of list comprehension aside. Have we by this point learned about the join() method?

For simplicity, consider that,

i * -1  ==  -i

so that,

>>> x = "Python is fun!"
>>> [x[-i] for i in range(1, len(x) + 1)]
['!', 'n', 'u', 'f', ' ', 's', 'i', ' ', 'n', 'o', 'h', 't', 'y', 'P']
>>> ''.join([x[-i] for i in range(1, len(x) + 1)])
'!nuf si nohtyP'
>>> 

This should be sufficient to pass the lesson check if in fact a string return is expected.

Define a function called reverse that takes a string text and returns that string in reverse. For example: reverse("abcd") should return "dcba" .

You may not use reversed or [::-1] to help you with this.

Given that slicing and the built-in function are disallowed, the above is more than a leap forward, but is on the right track… Negative indices.

How do we get this into basic form without the comprehension?

>>> y = ''
>>> for i in range(1, len(x) + 1):
	y += x[-i]

	
>>> y
'!nuf si nohtyP'
>>> 

Can we remove some of the ambiguity by generating a reversed range such that,

y += x[i - 1]

?

# range (start, stop, step)
>>> range(len(x), 0, -1)
[14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> y = ''
>>> for i in range(len(x), 0, -1):
	y += x[i - 1]

	
>>> y
'!nuf si nohtyP'
>>> 

Notice that now the index is positive, but the stride is negative?

3 Likes
#3

Thank you for great explanation!

3 Likes
#4

You’re welcome!

Be sure to drill into every aspect of the range() function in your review study.

2 Likes
#5

I was able to generate the correct answer, but CC fails it. why?

new =
def reverse(text):
index = len(text) -1
while index >= 0:
new.append(text[index])
index -= 1
return ‘’.join(new)

print reverse(‘poo’)

#6

Creating the list outside of the function means it can never be reset. Declare the array inside the function so it starts fresh on each new call.

#7

I have the same problem. I was able to generate the correct answer but CC fails it.

def reverse(text):
list =
for letr in text:
list.append(str(letr))
n = len(list)
while n > 0:
print list[n - 1],
n -= 1

#8

We are expected to return the reverse of the input string. What you code does is print, and returns None.

Some things to consider.

  1. text is a string.
  2. The length of a string can be determined without iterating it using len(string).
  3. There is no need to cast a string character to str().
  4. We can build a list from the front end using insert().

Start by defining the function and include the return line…

def reverse(text):
    result = []
    # loop code
    return result

Now consider this loop…

    for letter in text:
        result.insert(letter)

One final consideration is that the return value also needs to be a string. How would we convert a list to a string?

1 Like
#9

Thank you so much for the response!
Now I know where the problems are. I forgot that you can also add strings into a variable by simply using +=, so I don’t actually need to build a while loop outside, I can just use .join() instead. So thanks for that tip also.

1 Like