-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[New Concept Exercise] : Generators (Plane Tickets) #2729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
138 commits
Select commit
Hold shift + click to select a range
81a7c22
Update TESTS.md
J08K 6548ec9
Update TESTS.md
J08K 2b51829
Update TESTS.md
J08K d447645
Start of TOOLS.md
J08K 60b84f3
Finished writing VS Code part
J08K 1a72ef0
Added Pycharm docs & relative img links
J08K 7b87a4f
Update TOOLS.md
J08K 1021bb8
Added Spyder Docs
J08K 3d66c2b
Added VENV docs
J08K 09b1813
Added conda environments docs
J08K 9c1d1a9
Wrote Windows tutorial for adding to path
J08K 3c18af3
Improved shared test file and added disclaimer to TOOLS.md
J08K 5ac3fa2
Added docs for JupyterLab and Sublime Text
J08K ae2de41
Added Virtualenvwrapper piece.
J08K b8c8a9a
Cleaned up.
J08K 553b6d7
Updated image paths
J08K 68eca1f
Fixed Images x2
J08K 430bac3
Images have absolute path
J08K a433864
Update docs/TESTS.md
J08K d2612d9
Update docs/TESTS.md
J08K 584fd07
Update docs/TESTS.md
J08K 2e15fac
Update docs/TESTS.md
J08K 782c5df
Update docs/TESTS.md
J08K d00a764
Update docs/TESTS.md
J08K 5400ae3
Update docs/TOOLS.md
J08K 3e9b506
Update docs/TOOLS.md
J08K fcc43f9
Update docs/TESTS.md
J08K 7dc6c9c
Update docs/TOOLS.md
J08K 15ae0db
Update docs/TOOLS.md
J08K d6d57f5
Update exercises/shared/.docs/tests.md
J08K d770ab8
Update exercises/shared/.docs/tests.md
J08K 994ad50
Fixed capitalization
J08K f4224c0
Update docs/TOOLS.md
J08K 75b0ad6
Fixed language
J08K cb990e3
Update docs/TOOLS.md
J08K 092a25b
Update docs/TOOLS.md
J08K 95b28cc
Update docs/TOOLS.md
J08K 1ff0be9
Update docs/TESTS.md
J08K 6c1328c
Update docs/TOOLS.md
J08K 170d298
Update docs/TOOLS.md
J08K 92c6c4e
Update docs/TESTS.md
J08K 85ae17e
Update docs/TESTS.md
J08K f053a22
Update docs/TESTS.md
J08K b8ad4b5
Update docs/TOOLS.md
J08K 568de19
Layed out ground work
J08K f2d797a
Write introduction
J08K e6b8588
Update docs/TESTS.md
J08K c913f75
Added pytest.ini docs
J08K 81abb80
Update docs/TESTS.md
J08K 3684cdd
Update docs/TESTS.md
J08K 3d66a5c
Update docs/TESTS.md
J08K 8f2692b
Update docs/TOOLS.md
J08K 822a54b
Apply suggestions from code review
J08K 0ee4359
Update docs/TOOLS.md
J08K 0c53083
Update docs/TOOLS.md
J08K cde80ca
Update docs/TOOLS.md
J08K f2ea08d
Update docs/TOOLS.md
J08K 4cc8d6e
Start on instructions
J08K 1db4ce9
Update headers and links
J08K 5d20728
Removed template
J08K 6e937d0
Task 1
J08K d9dc03d
Merge branch 'exercism:main' into main
J08K 5fda574
Merge branch 'exercism:main' into main
J08K 176a1a3
Merge branch 'exercism:main' into main
J08K 924c083
sync with PCs
J08K ac50632
Sync
J08K d999678
Task 1 Exemplar
J08K 79b9fb6
Exemplar now also skips row 13
J08K 5bfd965
Fixed (skip row 13)
J08K 4650a4a
Finished Task 1 & 2
J08K 5c3940a
Remove Typing
J08K c5fb961
Merge remote-tracking branch 'upstream/main'
J08K 3c47c4c
Merge branch 'main' into J08K-New-Concept-Exercise
J08K b8ea390
Cleanup
J08K 8cafa69
Add some data in ./concepts
J08K 19c471c
Add exercise to config.json
J08K fedabbe
fixing
J08K 1394f9a
Update concepts/generators/.meta/config.json
J08K 0859ccc
Update concepts/generators/introduction.md
J08K 46ca557
Update exercises/concept/plane-tickets/.docs/instructions.md
J08K 81aef4c
Update exercises/concept/plane-tickets/.docs/introduction.md
J08K 87dbc72
Update concepts/generators/introduction.md
J08K 38e4351
Fixed all pylint warnings in Exemplar
J08K 7f9f6b3
Merge branch 'J08K-New-Concept-Exercise' of https://github.com/J08K/p…
J08K 74fc18a
Update exercises/concept/plane-tickets/.meta/exemplar.py
J08K 69e96c2
Merge branch 'exercism:main' into J08K-New-Concept-Exercise
J08K 7ea0eb4
Committed suggestions and Added new task 3
J08K 6557816
Apply suggestions from code review
BethanyG e20949c
Apply suggestions from code review WHOOOOP
J08K d751ec4
Add tests for task 3
J08K cdc58be
Update config.json
J08K 997c30f
Merge remote-tracking branch 'origin/J08K-New-Concept-Exercise' into …
J08K b5ac150
Better test case 1; add test for type task 3
J08K 0060913
Don't you love pytests?
J08K b809473
Merge branch 'main' into pr/2729
BethanyG b21b0aa
🤖 Sync org-wide files to upstream repo
exercism-bot 324e499
Layed out ground work
J08K 8694216
Write introduction
J08K fabe7c4
Start on instructions
J08K 96bee10
Removed template
J08K ca21760
Task 1
J08K 9b554a4
sync with PCs
J08K c086767
Sync
J08K 191b57c
Task 1 Exemplar
J08K a38f8f3
Exemplar now also skips row 13
J08K e329bff
Fixed (skip row 13)
J08K 3159215
Finished Task 1 & 2
J08K 9a60e00
Remove Typing
J08K 5ea95df
Update TESTS.md
J08K 46923d8
Update TESTS.md
J08K ccee77f
Update TESTS.md
J08K be697ad
Start of TOOLS.md
J08K 177aba2
Finished writing VS Code part
J08K 6db7b17
Added Pycharm docs & relative img links
J08K 385b8bd
Update TOOLS.md
J08K 1675034
Added VENV docs
J08K 6400263
Wrote Windows tutorial for adding to path
J08K a3ba686
Improved shared test file and added disclaimer to TOOLS.md
J08K f619980
Update docs/TOOLS.md
J08K 823737f
Cleanup
J08K 48f0a79
Add some data in ./concepts
J08K f658f95
Add exercise to config.json
J08K d4a5d54
fixing
J08K 7c420d0
Fixed all pylint warnings in Exemplar
J08K ca6047d
Update concepts/generators/.meta/config.json
J08K fdf30cb
Update concepts/generators/introduction.md
J08K a992d8c
Update exercises/concept/plane-tickets/.docs/instructions.md
J08K 29fd11b
Update exercises/concept/plane-tickets/.docs/introduction.md
J08K 5291c3a
Update concepts/generators/introduction.md
J08K 09f6e29
Update exercises/concept/plane-tickets/.meta/exemplar.py
J08K 6e3f4bf
Committed suggestions and Added new task 3
J08K 17c25b1
Apply suggestions from code review
BethanyG 35744f1
Apply suggestions from code review WHOOOOP
J08K 07f3535
Add tests for task 3
J08K 85cfc47
Better test case 1; add test for type task 3
J08K 6ee6ccc
Don't you love pytests?
J08K cf56cce
Merge branch 'J08K-New-Concept-Exercise' of https://github.com/J08K/p…
BethanyG abf417f
Update config.json
BethanyG File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
name: Configlet CI | ||
name: Configlet | ||
|
||
on: [push, pull_request, workflow_dispatch] | ||
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- main | ||
workflow_dispatch: | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
configlet: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Fetch configlet | ||
uses: exercism/github-actions/configlet-ci@main | ||
|
||
- name: Configlet Linter | ||
run: configlet lint | ||
uses: exercism/github-actions/.github/workflows/configlet.yml@main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"blurb": "TODO: add blurb for this concept", | ||
"authors": ["bethanyg", "cmccandless"], | ||
"blurb": "Learn about generators by assigning seats to passengers.", | ||
"authors": ["J08K"], | ||
"contributors": [] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,132 @@ | ||
#TODO: Add about for this concept. | ||
# About | ||
|
||
## Constructing a generator | ||
|
||
Generators are constructed much like other looping or recursive functions, but require a [`yield` expression](#the-yield-expression), which we will explore in depth a bit later. | ||
|
||
|
||
An example is a function that returns the _squares_ from a given list of numbers. | ||
As currently written, all input must be processed before any values can be returned: | ||
|
||
|
||
```python | ||
>>> def squares(list_of_numbers): | ||
>>> squares = [] | ||
>>> for number in list_of_numbers: | ||
>>> squares.append(number ** 2) | ||
>>> return squares | ||
``` | ||
|
||
You can convert that function into a generator like this: | ||
|
||
```python | ||
def squares(list_of_numbers): | ||
for number in list_of_number: | ||
yield number ** 2 | ||
``` | ||
|
||
The rationale behind this is that you use a generator when you do not need all the values _at once_. | ||
|
||
This saves memory and processing power, since only the value you are _currently working on_ is calculated. | ||
|
||
|
||
## Using a generator | ||
|
||
Generators may be used in place of most `iterables` in Python. This includes _functions_ or _objects_ that require an `iterable`/`iterator` as an argument. | ||
|
||
To use the `squares()` generator: | ||
|
||
```python | ||
>>> squared_numbers = squares([1, 2, 3, 4]) | ||
|
||
>>> for square in squared_numbers: | ||
>>> print(square) | ||
1 | ||
4 | ||
9 | ||
16 | ||
``` | ||
|
||
Values within a generator can also be produced/accessed via the `next()` function. | ||
`next()` calls the `__next__()` method of a generator object, "advancing" or evaluating the generator code up to its `yield` expression, which then "yields" or returns the value. | ||
|
||
```python | ||
square_generator = squares([1, 2]) | ||
|
||
>>> next(square_generator) | ||
1 | ||
>>> next(square_generator) | ||
4 | ||
``` | ||
|
||
When a `generator` is fully consumed and has no more values to return, it throws a `StopIteration` error. | ||
|
||
```python | ||
>>> next(square_generator) | ||
Traceback (most recent call last): | ||
File "<stdin>", line 1, in <module> | ||
StopIteration | ||
``` | ||
|
||
### Difference between iterables and generators | ||
|
||
J08K marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Generators are a special sub-set of _iterators_. | ||
`Iterators` are the mechanism/protocol that enables looping over _iterables_. | ||
Generators and and the iterators returned by common Python (`iterables`)[https://wiki.python.org/moin/Iterator] act very similarly, but there are some important differences to note: | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
- Generators are _one-way_; there is no "backing up" to a previous value. | ||
|
||
- Iterating over generators consume the returned values; no resetting. | ||
- Generators (_being lazily evaluated_) are not sortable and can not be reversed. | ||
|
||
- Generators do _not_ have `indexes`, so you can't reference a previous or future value using addition or subtraction. | ||
|
||
- Generators cannot be used with the `len()` function. | ||
|
||
- Generators can be _finite_ or _infinite_, be careful when collecting all values from an _infinite_ generator. | ||
|
||
## The yield expression | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The [yield expression](https://docs.python.org/3.8/reference/expressions.html#yield-expressions) is very similar to the `return` expression. | ||
|
||
_Unlike_ the `return` expression, `yield` gives up values to the caller at a _specific point_, suspending evaluation/return of any additional values until they are requested. | ||
|
||
When `yield` is evaluated, it pauses the execution of the enclosing function and returns any values of the function _at that point in time_. | ||
|
||
The function then _stays in scope_, and when `__next__()` is called, execution resumes until `yield` is encountered again. | ||
|
||
Note: _Using `yield` expressions is prohibited outside of functions._ | ||
|
||
```python | ||
>>> def infinite_sequence(): | ||
>>> current_number = 0 | ||
>>> while True: | ||
>>> yield current_number | ||
>>> current_number += 1 | ||
|
||
>>> lets_try = infinite_sequence() | ||
>>> lets_try.__next__() | ||
0 | ||
>>> lets_try.__next__() | ||
1 | ||
``` | ||
|
||
## Why generators? | ||
|
||
Generators are useful in a lot of applications. | ||
|
||
When working with a large collection, you might not want to put all of its values into `memory`. | ||
A generator can be used to work on larger data piece-by-piece, saving memory and improving performance. | ||
|
||
Generators are also very helpful when a process or calculation is _complex_, _expensive_, or _infinite_: | ||
|
||
```python | ||
>>> def infinite_sequence(): | ||
>>> current_number = 0 | ||
>>> while True: | ||
>>> yield current_number | ||
>>> current_number += 1 | ||
``` | ||
|
||
Now whenever `__next__()` is called on the `infinite_sequence` object, it will return the _previous number_ + 1. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
#TODO: Add introduction for this concept. | ||
# Introduction | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
A generator in Python is a _callable function_ that returns a [lazy iterator](https://en.wikipedia.org/wiki/Lazy_evaluation). | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
_Lazy iterators_ are similar to `lists`, and other `iterators`, but with one key difference: They do not store their `values` in memory, but _generate_ their values when needed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,10 @@ | ||
[ | ||
{ | ||
"url": "http://example.com/", | ||
"description": "TODO: add new link (above) and write a short description here of the resource." | ||
"url": "https://docs.python.org/3.8/reference/expressions.html#yield-expressions", | ||
"description": "Official Python 3.8 docs for the yield expression." | ||
}, | ||
{ | ||
"url": "http://example.com/", | ||
"description": "TODO: add new link (above) and write a short description here of the resource." | ||
}, | ||
{ | ||
"url": "http://example.com/", | ||
"description": "TODO: add new link (above) and write a short description here of the resource." | ||
}, | ||
{ | ||
"url": "http://example.com/", | ||
"description": "TODO: add new link (above) and write a short description here of the resource." | ||
"url": "https://en.wikipedia.org/wiki/Lazy_evaluation", | ||
"description": "Wikipedia page about lazy evaluation" | ||
} | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Hints | ||
|
||
## 1. Generate an amount of seats | ||
|
||
- The returned value should be of _type_ `generator`. | ||
- Row `13` should be skipped, so go from `12` to `14`. | ||
- Keep in mind that the returned values should be ordered from low to high. `1A, 1B, 2A, ...` | ||
|
||
## 2. Assign seats to passengers | ||
|
||
- Make sure your seat numbers do not have any space in them. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Instructions | ||
|
||
Conda airlines is the programming-world's biggest airline, with over 10.000 flights a day! | ||
|
||
They are currently assigning all seats to passengers by hand, this will need to automated. | ||
|
||
They have asked _you_ to create software to automate the assigning of seats to passengers. They require your software to be memory efficient and performant. | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Conda's airplanes have up to _4 seats_ in each row, and each airplane has many rows. | ||
|
||
While the rows are defined using numbers, seats in each row are defined using letters from the alphabet, with `seat A` being the first _seat_ in the row. | ||
|
||
You can use this table as a guide: | ||
|
||
| x | 1 | 2 | | ||
| :----: | :----: | :----:| | ||
| Row | 5 | 21 | | ||
| Seat letter | A | D | | ||
| Result | 5A | 21D | | ||
|
||
## 1. Generate an amount of seats | ||
|
||
Implement the `generate_seats()` function that returns an _iterable_ of seats given the following variable: | ||
|
||
`amount`: The amount of seats to be generated. | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Many airlines do not have _row_ number 13 on their flights, due to superstition amongst passengers. | ||
Conda Airlines also follows this convention, so make sure you _don't_ generate seats for _row_ number 13. | ||
|
||
_Note: The returned seats should be ordered, like: 1A 1B 1C._ | ||
|
||
```python | ||
>>> seats = generate_seats(10) | ||
>>> next(seats) | ||
"1A" | ||
>>> next(seats) | ||
"1B" | ||
``` | ||
|
||
## 2. Assign seats to passengers | ||
|
||
Implement the `assign_seats()` function that returns a _dictionary_ of `passenger` as _key_, and `seat_number` as _value_. Given is the following _list_: | ||
|
||
`passengers`: A list containing passenger names. | ||
|
||
```python | ||
>>> passengers = ['Jerimiah', 'Eric', 'Bethaney', 'Byte', 'SqueekyBoots', 'Bob'] | ||
|
||
>>> assign_seats(passengers) | ||
{'Jerimiah': '1A', 'Eric': '1B', 'Bethaney': '1C', 'Byte': '1D', 'SqueekyBoots': '2A', 'Bob': '2B'} | ||
``` | ||
|
||
## 3. Ticket codes | ||
|
||
Each ticket has a _12_ character long string code for identification. | ||
|
||
This code begins with the `assigned_seat` followed by the `flight_id`. The rest of the code is appended by `0s`. | ||
|
||
Implement a `generator` that yields a `ticket_number` given the following arguments: | ||
|
||
`seat_numbers`: A _list_ of *seat_numbers*. | ||
`flight_id`: A string containing the flight identification. | ||
|
||
```python | ||
>>> seat_numbers = ['1A', '17D'] | ||
>>> flight_id = 'CO1234' | ||
>>> ticket_ids = generate_codes(seat_numbers, flight_id) | ||
|
||
>>> next(ticket_ids) | ||
'1ACO12340000' | ||
>>> next(ticket_ids) | ||
'17DCO1234000' | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Introduction | ||
|
||
A generator in Python is a _callable function_ that returns a [lazy iterator](https://en.wikipedia.org/wiki/Lazy_evaluation). | ||
|
||
_Lazy iterators_ are similar to iterables such as `lists`, and other types of `iterators` in Python -- but with one key difference: `generators` do not store their `values` in memory, but _generate_ their values as needed or when called. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"blurb": "Learn about generators by assigning seats to passengers.", | ||
"authors": ["J08K"], | ||
"icon": "poker", | ||
"contributors": ["BethanyG"], | ||
"files": { | ||
"solution": ["plane_tickets.py"], | ||
"test": ["plane_tickets_test.py"], | ||
"exemplar": [".meta/exemplar.py"] | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.