Skip to content

Commit f69a7f3

Browse files
committed
more backtick formatting fixes
1 parent b0f14c5 commit f69a7f3

5 files changed

Lines changed: 29 additions & 29 deletions

File tree

exercises/practice/reverse-string/.approaches/additional-approaches/content.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def reverse(text):
3636
This strategy encodes the string into a UTF-8 [`bytearray`][bytearray].
3737
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`.
3838
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.
4040
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_).
4141

4242
This uses `O(n)` space for the output array.
@@ -56,14 +56,14 @@ def reverse(text):
5656
return "".join(output)
5757
```
5858

59-
This strategy uses two lists.
59+
This strategy uses two `list`s.
6060
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.
6363
Finally, `output` is joined via `str.join()` to create the reversed string.
6464

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.
6767
It also takes up `O(n)` auxiliary space with the `output` list.
6868

6969

@@ -134,8 +134,8 @@ As a (very) rough comparison, below is a timing table for these functions vs the
134134
| reverse bytes | 1.92e-06 | 3.82e-06 | 7.36e-06 | 1.65e-05 | 2.17e-05 | 2.71e-05 | 4.47e-05 | 5.17e-04 | 6.10e-03 | 2.16e-01 |
135135

136136

137-
As you can see, the reverse using two lists and the reverse using a bytearray are orders of magnitude slower than using a reverse slice.
138-
For the largest inputs measured, the dual list solution was almost 55x slower, and the bytearray solution was almost 1800x slower.
137+
As you can see, the reverse using two lists and the reverse using a `bytearray` are orders of magnitude slower than using a reverse slice.
138+
For the largest inputs measured, the dual list solution was almost 55x slower, and the `bytearray` solution was almost 1800x slower.
139139
Timings for strings over 142 characters could not be run for the recursive strategy, due to Python's 1000 call recursion limit.
140140

141141

exercises/practice/reverse-string/.approaches/built-in-list-reverse/content.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def reverse(text):
1010

1111
These approaches start with turning the text into a `list` of codepoints.
1212
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.
1414

1515
This takes `O(n)` time complexity because `list.reverse()` and `str.join()` iterate through the entire `list`.
1616
It uses `O(n)` space for the output `list`.
@@ -28,7 +28,7 @@ def reverse(text):
2828
return "".join(output)
2929
```
3030

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.
3232
This does add some time and space overhead.
3333

3434

@@ -57,8 +57,8 @@ Calling the constructor is also quite a bit faster than using a "written out" `f
5757

5858
As a (very) rough comparison, below is a timing table for these functions vs the canonical reverse slice:
5959

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.
6262

6363

6464
| **string length >>>>** | 5 | 11 | 22 | 52 | 66 | 86 | 142 | 1420 | 14200 | 142000 |

exercises/practice/reverse-string/.approaches/built-in-reversed/content.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def reverse(text):
1818
```
1919

2020
This version uses `reversed()` to reverse a `range()` object rather than feed a `start`/`stop`/`step` to `range()` itself.
21-
It then uses the reverse range to iterate over the input string and concatenate each code point to a new `output` string.
21+
It then uses the reverse `range` to iterate over the input string and concatenate each code point to a new `output` string.
2222
This has over-complicated `reversed()`, as it can be called directly on the input string with almost no overhead.
2323
This has also incurs the performance hit of repeated concatenation to the `output` string.
2424

exercises/practice/reverse-string/.approaches/introduction.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ In this introduction, we cover six general approaches and an additional group of
1414
1. Sequence Slice with Negative Step
1515
2. Iteration with String Concatenation
1616
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()`
1919
6. Use the built-in `reversed()`
2020
7. Other [interesting approaches][approach-additional-approaches]
2121

@@ -72,7 +72,7 @@ This is essentially the same technique as the approach above, but incurs slightl
7272

7373
For very long strings, this approach will still degrade to `O(n**2)` performance, due to the use of string concatenation.
7474
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.
7676

7777

7878
## Approach: Create a `list` and Use `str.join()` to make new String
@@ -87,8 +87,8 @@ def reverse(text):
8787
return "".join(output)
8888
```
8989

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`.
9292
Finally, `str.join()` is used to re-assemble the `list` into a string.
9393

9494
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):
104104
return "".join(output)
105105
```
106106

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.
109109

110110
For more details, see the [built-in `list.reverse()`][approach-built-in-list-reverse] approach.
111111

exercises/practice/reverse-string/.approaches/list-and-join/content.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff 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
22

33
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.
44
This avoids the `O(n**2)` danger of repeated shifting/reallocation when concatenating long strings.
@@ -14,9 +14,9 @@ def reverse(text):
1414
return "".join(output)
1515
```
1616

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.
1818
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.
2020
A small re-write using `range()` to change the iteration direction will boost performance:
2121

2222

@@ -33,12 +33,12 @@ def reverse(text):
3333
return "".join(output)
3434
```
3535

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)`.
3838
This also takes `O(n)` space for the output `list`.
3939

4040

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
4242

4343
```python
4444
def reverse(text):
@@ -54,7 +54,7 @@ def reverse(text):
5454

5555
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.
5656
`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.
5858
It is still `O(n)` time complexity because `list()` and `str.join()` both iterate over the entire length of the input string.
5959

6060

@@ -101,7 +101,7 @@ Because of this issue, no timings are available for this variation.
101101
For code that keeps bytes together correctly, see the `bytearray` variation in the [additional approaches][approach-additional-approaches] approach.
102102

103103

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`
105105

106106
```python
107107
def reverse(text):
@@ -111,7 +111,7 @@ def reverse(text):
111111
```
112112

113113
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.
115115
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.
116116

117117

0 commit comments

Comments
 (0)