Skip to content

Commit 4eea484

Browse files
committed
blank lines after headers and remove trailing whitespace
1 parent 36858d7 commit 4eea484

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

exercises/practice/robot-name/.approaches/introduction.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# Introduction
2+
23
Robot Name in Python is an interesting exercise for practicing randomness.
34

45
## General Guidance
6+
57
Two ways immediately come to mind: generate all the possible names and then return them sequentially, or generate a random name and ensure that it's not been previously used.
68

79
Randomness can be a little, well, random, so **it's very easy to have an incorrect solution and still pass the tests**. It's strongly recommended to submit your solution for Code Review.
810

911
## Approach: mass name generation
12+
1013
We'd first have to generate all the possible names, shuffle them, and then use `next` (the simplest way) or maintain a `current_index` and get the name.
1114
Here's a possible way to do it:
1215

@@ -30,6 +33,7 @@ Note that selecting randomly from the list of all names would be incorrect, as t
3033
For more detail and explanation of the code, [read here][approach-mass-name-generation].
3134

3235
## Approach: name on the fly
36+
3337
Another approach is to generate the name on the fly and add it to a cache or a store, checking if the generated name hasn't been used previously.
3438

3539

exercises/practice/robot-name/.approaches/mass-name-generation/content.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Mass Name Generation
2+
23
We first generate all the possible names, shuffle them, and then either use `next` (the simplest way) or maintain a `current_index` to get the name.
34
Note that selecting randomly from the list of all names would be incorrect, as there is a possibility of the name being repeated.
45

@@ -31,14 +32,14 @@ numbers = (str(i).zfill(3) for i in range(1000))
3132
names = [l + n for l in letter_pairs for n in numbers]
3233
```
3334

34-
After the name generation, the names are shuffled - using the [default `seed`][random-seed] in the `random` module (the current timestamp).
35+
After the name generation, the names are shuffled - using the [default `seed`][random-seed] in the `random` module (the current timestamp).
3536
When the tests reseed `random`, this has no effect as the names were shuffled before that.
3637

37-
We then set `NAMES` to the iterable of names, and in `reset`, set the robot's name to the `next(name)`.
38+
We then set `NAMES` to the iterable of names, and in `reset`, set the robot's name to the `next(name)`.
3839
If you are interested, you can read more on [`iter` and `next`][iter-and-next].
3940

4041
Unlike the [on the fly approach][approach-name-on-the-fly], this has a relatively short "generation" time, because we are merely giving the `next` name instead of generating it.
41-
However, this has a huge startup memory and time cost, as 676,000 strings have to be calculated and stored.
42+
However, this has a huge startup memory and time cost, as 676,000 strings have to be calculated and stored.
4243
For an approximate calculation, 676,000 strings * 5 characters / string * 1 byte / character gives 3380000 bytes or 3.38 MB of RAM - and that's just the memory aspect of it.
4344
Sounds small, but this might be a relatively significant startup cost.
4445

exercises/practice/robot-name/.approaches/name-on-the-fly/content.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Find name on the fly
2+
23
We generate the name on the fly and add it to a cache or a store, checking to make sure that the generated name has not been used previously.
34

45
A possible way to implement this:
@@ -10,7 +11,7 @@ cache = set()
1011

1112

1213
class Robot:
13-
def __get_name(self):
14+
def __get_name(self):
1415
return ''.join(choices(ascii_uppercase, k=2) + choices(digits, k=3))
1516

1617
def reset(self):
@@ -19,7 +20,7 @@ class Robot:
1920
cache.add(name)
2021
self.name = name
2122

22-
def __init__(self):
23+
def __init__(self):
2324
self.reset()
2425
```
2526
We use a `set` for the cache as it has a low access time, and because we do not need the preservation of order or the ability to access by index.
@@ -53,7 +54,7 @@ This has almost no startup time and memory, apart from declaring an empty `set`.
5354
Note that the _generation_ time is the same as the [mass generation approach][approach-mass-name-generation], as a similar method is used.
5455
However, as the name is generated at the time of setting/resetting, the method time itself is higher.
5556

56-
In the long run, if many names are generated, this is inefficient, since collisions will start being generated more often than unique names.
57+
In the long run, if many names are generated, this is inefficient, since collisions will start being generated more often than unique names.
5758

5859
[walrus-operator]: https://realpython.com/python-walrus-operator/
5960
[private-helper-methods]: https://www.geeksforgeeks.org/private-methods-in-python/

0 commit comments

Comments
 (0)