You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: exercises/practice/reverse-string/.approaches/additional-approaches/content.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ def reverse(text):
36
36
This strategy encodes the string into a UTF-8 [`bytearray`][bytearray].
37
37
It then uses a `while` loop to iterate through the text, calculating the length of a sequence (or 'window') to slice from `given` and prepend to `output`.
38
38
The `index` counter is then incremented by the length of the 'window'.
39
-
Once the `index` is greater than the length of `given`, the `output``bytearray` is decoded into a UTF-8 string and returned.
39
+
Once the `index` is greater than the length of `given`, the `output` bytearray is decoded into a UTF-8 string and returned.
40
40
This is (_almost_) the same set of operations as described in the next approach, but operating on bytes in a `bytearray`, as opposed to text/codepoints in a `list` — although this strategy does not use `list.pop()` (_`bytearray` objects do not have a pop method_).
41
41
42
42
This uses `O(n)` space for the output array.
@@ -56,14 +56,14 @@ def reverse(text):
56
56
return"".join(output)
57
57
```
58
58
59
-
This strategy uses two lists.
59
+
This strategy uses two `list`s.
60
60
One `list` for the codepoints in the text, and one to hold the codepoints in reverse order.
61
-
First, the input text is turned into the `codepoints``list`, and iterated over.
62
-
Each codepoint is `pop()`ped from `codepoints` and appended to the `output``list`.
61
+
First, the input text is turned into the `codepoints` list, and iterated over.
62
+
Each codepoint is `pop()`ped from `codepoints` and appended to the `output` list.
63
63
Finally, `output` is joined via `str.join()` to create the reversed string.
64
64
65
-
While this is a straightforward and readable approach, it creates both memory and performance overhead, due to the creation of the lists and the use of `str.join()`.
66
-
This is much faster than the bytearray strategy or using string concatenation, but is still slightly slower than the slicing strategy.
65
+
While this is a straightforward and readable approach, it creates both memory and performance overhead, due to the creation of the `list`s and the use of `str.join()`.
66
+
This is much faster than the `bytearray` strategy or using string concatenation, but is still slightly slower than the slicing strategy.
67
67
It also takes up `O(n)` auxiliary space with the `output` list.
68
68
69
69
@@ -134,8 +134,8 @@ As a (very) rough comparison, below is a timing table for these functions vs the
Copy file name to clipboardExpand all lines: exercises/practice/reverse-string/.approaches/built-in-list-reverse/content.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ def reverse(text):
10
10
11
11
These approaches start with turning the text into a `list` of codepoints.
12
12
Rather than use a loop and `list.append()` to then reverse the text, the [`list.reverse()`][list-reverse-method] method is used to perform an in-place reversal.
13
-
`str.join()` is then used to turn the list into a string.
13
+
`str.join()` is then used to turn the `list` into a string.
14
14
15
15
This takes `O(n)` time complexity because `list.reverse()` and `str.join()` iterate through the entire `list`.
16
16
It uses `O(n)` space for the output `list`.
@@ -28,7 +28,7 @@ def reverse(text):
28
28
return"".join(output)
29
29
```
30
30
31
-
This variation is essentially the same as the solution above, but makes a codepoints list to keep the original codepoint ordering of the input text.
31
+
This variation is essentially the same as the solution above, but makes a codepoints `list` to keep the original codepoint ordering of the input text.
32
32
This does add some time and space overhead.
33
33
34
34
@@ -57,8 +57,8 @@ Calling the constructor is also quite a bit faster than using a "written out" `f
57
57
58
58
As a (very) rough comparison, below is a timing table for these functions vs the canonical reverse slice:
59
59
60
-
As you can see, using `list.reverse()` after converting the input text to a list is much slower than using a reverse slice.
61
-
Iterating in a loop to create the output list also adds even more time.
60
+
As you can see, using `list.reverse()` after converting the input text to a `list` is much slower than using a reverse slice.
61
+
Iterating in a loop to create the output `list` also adds even more time.
Copy file name to clipboardExpand all lines: exercises/practice/reverse-string/.approaches/introduction.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,8 +14,8 @@ In this introduction, we cover six general approaches and an additional group of
14
14
1. Sequence Slice with Negative Step
15
15
2. Iteration with String Concatenation
16
16
3. Reverse Iteration with `range()`
17
-
4. Make a list and Use `str.join()`
18
-
5. Make a list and use `list.reverse()`
17
+
4. Make a `list` and Use `str.join()`
18
+
5. Make a `list` and use `list.reverse()`
19
19
6. Use the built-in `reversed()`
20
20
7. Other [interesting approaches][approach-additional-approaches]
21
21
@@ -72,7 +72,7 @@ This is essentially the same technique as the approach above, but incurs slightl
72
72
73
73
For very long strings, this approach will still degrade to `O(n**2)` performance, due to the use of string concatenation.
74
74
Using `str.join()` here can avoid the concatenation penalty.
75
-
For more information and relative performance timings for this group, check out the [backwards iteration with range][approach-backward-iteration-with-range] approach.
75
+
For more information and relative performance timings for this group, check out the [backwards iteration with `range()`][approach-backward-iteration-with-range] approach.
76
76
77
77
78
78
## Approach: Create a `list` and Use `str.join()` to make new String
@@ -87,8 +87,8 @@ def reverse(text):
87
87
return"".join(output)
88
88
```
89
89
90
-
This approach either breaks the string up into a list of codepoints to swap or creates an empty list as a "parking place" to insert or append codepoints.
91
-
It then iterates over the text, swapping, inserting, or appending each codepoint to the output list.
90
+
This approach either breaks the string up into a `list` of codepoints to swap or creates an empty `list` as a "parking place" to insert or append codepoints.
91
+
It then iterates over the text, swapping, inserting, or appending each codepoint to the output `list`.
92
92
Finally, `str.join()` is used to re-assemble the `list` into a string.
93
93
94
94
For more variations and relative performance timings for this group, check out the [`list` and `str.join()`][approach-list-and-join] approach.
@@ -104,8 +104,8 @@ def reverse(text):
104
104
return"".join(output)
105
105
```
106
106
107
-
This approach turns the string into a list of codepoints and then uses the `list.reverse()` method to re-arrange the list _in place_.
108
-
After the reversal of the list, `str.join()` is used to create the reversed string.
107
+
This approach turns the string into a `list` of codepoints and then uses the `list.reverse()` method to re-arrange the `list`_in place_.
108
+
After the reversal of the `list`, `str.join()` is used to create the reversed string.
109
109
110
110
For more details, see the [built-in `list.reverse()`][approach-built-in-list-reverse] approach.
Copy file name to clipboardExpand all lines: exercises/practice/reverse-string/.approaches/list-and-join/content.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Create a List and Use `str.join()` to Make A New String
1
+
# Create a `list` and Use `str.join()` to Make A New String
2
2
3
3
To avoid performance issues with concatenating to a string, this group of approaches uses one or more `list`s to perform swaps or reversals before joining the codepoints back into a string.
4
4
This avoids the `O(n**2)` danger of repeated shifting/reallocation when concatenating long strings.
@@ -14,9 +14,9 @@ def reverse(text):
14
14
return"".join(output)
15
15
```
16
16
17
-
The code above iterates over the codepoints in the input text and uses `list.insert()` to insert each one into the output list.
17
+
The code above iterates over the codepoints in the input text and uses `list.insert()` to insert each one into the `output` list.
18
18
Note that `list.insert(0, codepoint)`_prepends_, which is very inefficient for `lists`, while appending takes place in (amortized) `O(1)` time.
19
-
So this code incurs a time penalty because it forces repeated shifts of every element in the list with every insertion.
19
+
So this code incurs a time penalty because it forces repeated shifts of every element in the `list` with every insertion.
20
20
A small re-write using `range()` to change the iteration direction will boost performance:
21
21
22
22
@@ -33,12 +33,12 @@ def reverse(text):
33
33
return"".join(output)
34
34
```
35
35
36
-
This code iterates backward over the string using `range()`, and can therefore use `list.append()` to append to the output list in (amortized) constant time.
37
-
However, the use of `str.join()` to unpack the list and create a string still makes this `O(n)`.
36
+
This code iterates backward over the string using `range()`, and can therefore use `list.append()` to append to the `output` list in (amortized) constant time.
37
+
However, the use of `str.join()` to unpack the `list` and create a string still makes this `O(n)`.
38
38
This also takes `O(n)` space for the output `list`.
39
39
40
40
41
-
## Variation #2: Convert Text to List and Use `range()` to Iterate Over Half the String, Swapping Values
41
+
## Variation #2: Convert Text to a `list` and Use `range()` to Iterate Over Half the String, Swapping Values
42
42
43
43
```python
44
44
defreverse(text):
@@ -54,7 +54,7 @@ def reverse(text):
54
54
55
55
This variation calculates the midpoint which is then used with `range()` in a `for loop` to iterate over _half_ the indexes in the `output` list, swapping values into their reversed places.
56
56
`str.join()` is then used to create a new string.
57
-
This technique is quite speedy, and re-arranges the list of codepoints _in place_, avoiding expensive string concatenation.
57
+
This technique is quite speedy, and re-arranges the `list` of codepoints _in place_, avoiding expensive string concatenation.
58
58
It is still `O(n)` time complexity because `list()` and `str.join()` both iterate over the entire length of the input string.
59
59
60
60
@@ -101,7 +101,7 @@ Because of this issue, no timings are available for this variation.
101
101
For code that keeps bytes together correctly, see the `bytearray` variation in the [additional approaches][approach-additional-approaches] approach.
102
102
103
103
104
-
## Variation #5: Use Generator Expression with Join to Iterate Backwards Over Codepoints List
104
+
## Variation #5: Use Generator Expression with `str.join()` to Iterate Backwards Over Codepoints `list`
105
105
106
106
```python
107
107
defreverse(text):
@@ -111,7 +111,7 @@ def reverse(text):
111
111
```
112
112
113
113
This variation puts the for/while loop used in other strategies directly into `str.join()` using a generator expression.
114
-
The text is first converted to a list and the generator-expression "swaps" the codepoints over the whole `list`, using `range()` for the indexes.
114
+
The text is first converted to a `list` and the generator-expression "swaps" the codepoints over the whole `list`, using `range()` for the indexes.
115
115
Interestingly, because of the work to create and manage the generator, this variation is actually _slower_ than using an auxiliary `list` and `loop` to manage codepoints and then calling `str.join()` separately.
0 commit comments