-2

Documentation for a fictional module:

weird.reverse(text, [start, [end]])

Returns a copy of text reversed in the range start to end.

If the optional arguments start and end are specified, this function reverses the slice text[start:end], but leaves the text outside of the slice unchanged. For example:

>>> weird.reverse('abCDEfg',2,5)
'abEDCfg'

If either start or end are not specified, then the reversed slice starts at the beginning of the string, or ends at the end of the string, respectively. For example:

>>> weird.reverse('abCDEfg',2)
'abgfEDC'

Parameters:
text (str) – The text to (partially) reverse start (int) – The start of the slice to reverse end (int) – The end of the slice to reverse Returns:
A copy of text reversed in the range start to end.

Return type:
str

Question: What do these expressions return?

weird.reverse('abcef',-3)
weird.reverse('abcef',end=2)    

I have banged my head over and over.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
KV1
  • 65
  • 7
  • Which of the two examples is unclear to you and why? – mkrieger1 Feb 17 '21 at 00:09
  • Both are unclear to me. weird.reverse('abCDEfg',2) in the documentation returns 'abgfEDC'. Using that rationale, I would think that weird.reverse('abcef',-3) in the top expression would mean that I leave the last 3 characters alone and reverse the first 2, however, that is incorrect. I have tried every combo I could think of. – KV1 Feb 17 '21 at 00:10
  • 1
    Do you understand the two examples shown inside the fictional documentation? – mkrieger1 Feb 17 '21 at 00:12
  • My guess is that it is "range start to end" that is throwing you. The question is easier to answer if you already have an idea of how string slicing works (since it is a slice that you are reversing). Perhaps you could take a break from this problem and review what something like `s[i:j]` means. – John Coleman Feb 17 '21 at 00:12
  • My apologies for not showing my work and my thought process up to this point. I am new to this and really working hard to learn. I don't want 'just the answer'. I want to make sure I understand WHY that's the answer. Thank you guys for the comments so far. – KV1 Feb 17 '21 at 00:16
  • @KaraV That's not so bad. But it's the *start* that is at -3 (meaning 3 positions from the *right*). So the first 2 characters should be unchanged and the last 3 should be reversed. – mkrieger1 Feb 17 '21 at 00:17
  • This should answer your question: https://stackoverflow.com/questions/509211/understanding-slice-notation (since you seem to understand the part about reversing the characters, just not *which* characters) – mkrieger1 Feb 17 '21 at 00:23
  • 1
    Thank you so much for your help. Your explanation helped me to wrap my head around the reason for the answer. Much appreciated. It also helped me come up with the answer to the 2nd expression on my own. :). _end=2_ (meaning only reverse the first 2 positions starting from the left) – KV1 Feb 17 '21 at 00:28

1 Answers1

0

Here is a potential implementation:

def reverse(text, start=None, end=None):
    if start is None and end is None:
        return text[::-1]
    if end is None:
        return text[:start] + text[start:][::-1]
    if start is None:
        return text[:end][::-1] + text[end:]
    return text[:start] + text[start:end][::-1] + text[end:]

Using this code, running

print(reverse('abcef',-3))
print(reverse('abcef',end=2))

gives:

abfec
bacef

Link to documentation on slicing: https://python-reference.readthedocs.io/en/latest/docs/brackets/slicing.html

Andrew McClement
  • 1,171
  • 5
  • 14