Skip to main content
added 212 characters in body
Source Link
joseville
  • 516
  • 2
  • 10

UPDATE: Thanks everyone for the suggestions! I made a follow up version here which fixes the bugs, incorporates the feedback, and has more tests.


A ReversibleList behaves like a normal list when ReversibleList.reversed == False, but it behaves as if it had been reversed when ReversibleList.reversed == True. The trick is that the underlying list is never actually reversed. This can be a boon for performance [citation needed] if whatever one's doing requires reversing and unreversing a particular list many times, or reversing many lists.

A ReversibleList behaves like a normal list when ReversibleList.reversed == False, but it behaves as if it had been reversed when ReversibleList.reversed == True. The trick is that the underlying list is never actually reversed. This can be a boon for performance [citation needed] if whatever one's doing requires reversing and unreversing a particular list many times, or reversing many lists.

UPDATE: Thanks everyone for the suggestions! I made a follow up version here which fixes the bugs, incorporates the feedback, and has more tests.


A ReversibleList behaves like a normal list when ReversibleList.reversed == False, but it behaves as if it had been reversed when ReversibleList.reversed == True. The trick is that the underlying list is never actually reversed. This can be a boon for performance [citation needed] if whatever one's doing requires reversing and unreversing a particular list many times, or reversing many lists.

corrected case of attribute in post's body to match code
Source Link
AJNeufeld
  • 35.3k
  • 5
  • 41
  • 103

A ReversibleList behaves like a normal list when ReversibleList.REVERSEDreversed == False, but it behaves as if it had been reversed when ReversibleList.REVERSEDreversed == True. The trick is that the underlying list is never actually reversed. This can be a boon for performance [citation needed] if whatever one's doing requires reversing and unreversing a particular list many times, or reversing many lists.

A ReversibleList behaves like a normal list when ReversibleList.REVERSED == False, but it behaves as if it had been reversed when ReversibleList.REVERSED == True. The trick is that the underlying list is never actually reversed. This can be a boon for performance [citation needed] if whatever one's doing requires reversing and unreversing a particular list many times, or reversing many lists.

A ReversibleList behaves like a normal list when ReversibleList.reversed == False, but it behaves as if it had been reversed when ReversibleList.reversed == True. The trick is that the underlying list is never actually reversed. This can be a boon for performance [citation needed] if whatever one's doing requires reversing and unreversing a particular list many times, or reversing many lists.

Became Hot Network Question
Tweeted twitter.com/StackCodeReview/status/1565670882474860545
formatting
Source Link
Mast
  • 13.8k
  • 12
  • 57
  • 127
a = ReversibleList([0, 1, 2, 3, 4, 5])
print(a[1]) # 1
print(a) # [0, 1, 2, 3, 4, 5]
print(a[:]) # [0, 1, 2, 3, 4, 5]
print(repr(a)) # ReversibleList([0, 1, 2, 3, 4, 5])

a.reverse()
print(a[1]) # 4
print(a) # [5, 4, 3, 2, 1, 0]
print(a[:]) # [5, 4, 3, 2, 1, 0]
print(repr(a)) # ReversibleList([0, 1, 2, 3, 4, 5])

a[:] = a
print(a) # [5, 4, 3, 2, 1, 0]

a[2:5] = a[2:5]
print(a) # [5, 4, 3, 2, 1, 0]

print(a[2:5]) # [3, 2, 1]

a[2:5] = [3, 2, 1]
print(a) # [5, 4, 3, 2, 1, 0]

a[2:5] = ReversibleList([1, 2, 3])
print(a) # [5, 4, 3, 2, 1, 0]
```
a = ReversibleList([0, 1, 2, 3, 4, 5])
print(a[1]) # 1
print(a) # [0, 1, 2, 3, 4, 5]
print(a[:]) # [0, 1, 2, 3, 4, 5]
print(repr(a)) # ReversibleList([0, 1, 2, 3, 4, 5])

a.reverse()
print(a[1]) # 4
print(a) # [5, 4, 3, 2, 1, 0]
print(a[:]) # [5, 4, 3, 2, 1, 0]
print(repr(a)) # ReversibleList([0, 1, 2, 3, 4, 5])

a[:] = a
print(a) # [5, 4, 3, 2, 1, 0]

a[2:5] = a[2:5]
print(a) # [5, 4, 3, 2, 1, 0]

print(a[2:5]) # [3, 2, 1]

a[2:5] = [3, 2, 1]
print(a) # [5, 4, 3, 2, 1, 0]

a[2:5] = ReversibleList([1, 2, 3])
print(a) # [5, 4, 3, 2, 1, 0]
```
a = ReversibleList([0, 1, 2, 3, 4, 5])
print(a[1]) # 1
print(a) # [0, 1, 2, 3, 4, 5]
print(a[:]) # [0, 1, 2, 3, 4, 5]
print(repr(a)) # ReversibleList([0, 1, 2, 3, 4, 5])

a.reverse()
print(a[1]) # 4
print(a) # [5, 4, 3, 2, 1, 0]
print(a[:]) # [5, 4, 3, 2, 1, 0]
print(repr(a)) # ReversibleList([0, 1, 2, 3, 4, 5])

a[:] = a
print(a) # [5, 4, 3, 2, 1, 0]

a[2:5] = a[2:5]
print(a) # [5, 4, 3, 2, 1, 0]

print(a[2:5]) # [3, 2, 1]

a[2:5] = [3, 2, 1]
print(a) # [5, 4, 3, 2, 1, 0]

a[2:5] = ReversibleList([1, 2, 3])
print(a) # [5, 4, 3, 2, 1, 0]
Source Link
joseville
  • 516
  • 2
  • 10
Loading