Skip to content

Commit 3e41c7c

Browse files
mohanrajanrErikSchierboom
authored andcommitted
Implement new Concept Exercise: none exercism#1031 (#2283)
* Added Python Concept Exercise None * Ran Prettier for Linting * Updated Story and new Exercises * Review 1 None-Intro.md * Review Changes 2
1 parent 0e8b832 commit 3e41c7c

File tree

9 files changed

+367
-0
lines changed

9 files changed

+367
-0
lines changed

exercises/concept/none/.docs/after.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
All variables that are assigned `None` point to the same object. New instances of `None` are not created, that is `None` by nature is of type Singleton.
2+
3+
Example, when you assign 2 variables with `None`, both point to the same object.
4+
5+
```python
6+
a = None
7+
b = None
8+
a == b
9+
#=> True
10+
```
11+
12+
Python will also return `None` from a function that doesn't already have a stated `return` value, making it easy to check if the function completed without errors.
13+
14+
```python
15+
def test_func():
16+
pass
17+
test_func()
18+
#=> returns None
19+
20+
def test_func2():
21+
return
22+
#=> returns None
23+
```
24+
25+
The `None` Object is a singleton of class `NoneType`. So when you find the type of a variable which is assigned with `None` it will always be of type `NoneType`.
26+
27+
```python
28+
a = None
29+
type(a)
30+
#=> <class 'NoneType'>
31+
```
32+
33+
When you toggle a variable between a value and `None`, what basically happens is a reset of the variable.
34+
35+
```python
36+
37+
a = [] #=> Variable 'a' is pointing to a list
38+
39+
a = None #=> Variable 'a' is reset to an empty state. the value of a is now absent.
40+
41+
```

exercises/concept/none/.docs/hints.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## General
2+
3+
- In Python, `None` is frequently used as a placeholder to represent the **absence of a value** for a variable, object, or argument.
4+
5+
## 1. Make New Seating Chart
6+
7+
Remember, you will need to create an empty dictionary first. Next, fill the dictionary _keys_ with however many seats are available and set their _values_ to a **placeholder** to indicate that they are available but unassigned.
8+
9+
## 2. Arrange Reservations
10+
11+
- If there isn't a guest list, you will want to return a `new_seating_chart()` filled with placeholders. A `default argument` for your function might be helpful here. If you do have a guest list, you can start with a `new_seating_chart()`, then loop from 1 to the number of guests and assign them to seats in order.
12+
13+
## 3. Find all Available Seats
14+
15+
- You can loop through all the (key, value) pairs in a dictionary by calling `dict.items()`. You can verify that a variable or value is None through an `if statement` -- `if var is None` will return True if the value is `None`. You can add things to a `list` by calling `list.append()`
16+
17+
## 4. Current seating capacity
18+
19+
- You can loop through all of the values in the dict object by calling `dict.values()`. Seats are available when their value is `None`.
20+
21+
## 5. Accommodate Waiting Guests
22+
23+
- You need to find the current number of empty seats and check to see if it is greater than or equal to the number of guests waiting. If the guests can be accommodated, you will need to call `find_all_available_seats()` to get a list of seat numbers you can assign guests to. Remember that `range()` can take any sort of number as an argument....**including** the number returned from calling `len()` on a list. Also remember that using `list[index_number]` will return the **value** that is located at the **index number** inside the brackets.
24+
25+
## 6. Empty the Seats
26+
27+
- Given the seating chart `dict`, and a `list` of seat numbers, you'll want to indicate the seat is now available for another guest by replacing their name with a placeholder. Looping through the seat number list and looking for those seats in the dictionary might be helpful here.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
The exercise has 6 challenges to complete.
2+
3+
You are the Maître D' of a hotel restaurant. Your task is to manage the seating arrangements for the dining room according to the number of seats available today, number of reservations, and the "walk in" guests currently waiting to be seated. For the purposes of this exercise, seating is assigned by first available empty seat.
4+
5+
You have 6 different dining room organization challenges to complete.
6+
7+
## 1. Make Today's Seating Chart
8+
9+
Define the `new_seating_chart()` function that takes a size argument representing the number of seats that will be set in the dining room today. If no `size` is given, the function should return a seating chart with 22 seats. Seat values should have a placeholder of `None` to indicate they are available to assign to a guest.
10+
11+
## 2. Arrange Reservations
12+
13+
Define the `arrange_reservations()` function with 1 parameter for a list of guest names. This represents the number of people who've reserved places in the dining room today.
14+
15+
This function should return a `dict` seating chart of default size (22 seats), with guests assigned to seats in the order they appear on the reservation list. All unassigned seats should be set to `None`. If there are no guests, an "empty" seating chart with all `None` placeholders should be returned.
16+
17+
```python
18+
arrange_reservatons(guests=["Walter", "Frank", "Jenny", "Carol", "Alice", "George"])
19+
#=> {1: 'Walter', 2: 'Frank', 3: 'Jenny', 4: 'Carol', 5: 'Alice', 6: 'George', 7: None, 8: None, 9: None, 10: None, 11: None, 12: None, 13: None, 14: None, 15: None, 16: None, 17: None, 18: None, 19: None, 20: None, 21: None, 22: None}
20+
```
21+
22+
## 3. Find All the "Empty" Seats
23+
24+
Define the `find_all_available_seats(seats)` function that takes 1 parameter (_a seating chart dictionary_) and returns a `list` of seat numbers that are available for guests that are currently waiting.
25+
If a seat is empty, It will be of `None` value in the dictionary. Occupied seats will have the name of the guest.
26+
27+
```python
28+
seats = {1: None, 2: 'Frank', 3: 'Jenny', 4: None, 5: 'Alice', 6: 'George', 7: None, 8: 'Carol', 9: None, 10: None, 11: None, 12: 'Walter'}
29+
find_all_available_seats(seats)
30+
#>>>[1,4,7,9,10,11]
31+
```
32+
33+
## 4. Current Empty Seating Capacity
34+
35+
Define the `curr_empty_seat_capacity()` function that takes 1 parameter. The first parameter will list all the seats.
36+
37+
You need to find out what is the total number of seats that are empty.
38+
39+
```python
40+
curr_empty_seat_capacity(seats={1: "Occupied", 2: None, 3: "Occupied"})
41+
# => 1
42+
can_accomodate_seats(seats={1: "Occupied", 2: None, 3: None})
43+
# => 2
44+
```
45+
46+
## 5. Should we wait?
47+
48+
Define the `accommodate_waiting_guests(seats, guests)` function that takes two parameters. The first parameter will be a seating chart `dict`. The second parameter will be a `list` of guests who have "walked in" unexpectedly.
49+
You'll first need to find out how many seats are available and whether or not you can even give the unannounced guests seats at this time.
50+
51+
If you do not have enough seats, return `False`.
52+
53+
If seats are available, you will want to give the guests places on the seating chart, and return it updated.
54+
Tip: You can use previously defined functions to do the calculations for you.
55+
56+
```python
57+
starting_reservations = {1: 'Carol', 2: 'Alice', 3: 'George', 4: None, 5: None, 6: None, 7: 'Frank', 8: 'Walter'}
58+
accomodate_guests(starting_reservations, ["Mort", "Suze", "Phillip", "Tony"])
59+
#>>>False
60+
starting_reservations = {1: None, 2: None, 3: None, 4: 'Carol', 5: 'Alice', 6: 'George', 7: None, 8: None, 9: None, 10: None, 11: None, 12: None, 13: None, 14: None, 15: None, 16: None, 17: None, 18: 'Frank', 19: 'Jenny', 20: None, 21: None, 22: 'Walter'}
61+
accomodate_guests(starting_reservations, ["Mort", "Suze", "Phillip", "Tony"])
62+
#>>>{1: 'Mort', 2: 'Suze', 3: 'Phillip', 4: 'Carol', 5: 'Alice', 6: 'George', 7: 'Tony', 8: None, 9: None, 10: None, 11: None, 12: None, 13: None, 14: None, 15: None, 16: None, 17: None, 18: 'Frank', 19: 'Jenny', 20: None, 21: None, 22: 'Walter'}
63+
```
64+
65+
## 6. Empty Seats
66+
67+
Define the `empty_seats()` function that takes two parameters. The first parameter will be a seating chart dictionary. The second parameter is a list of seat numbers you need to "free up" or empty -- that is, you need to assign the seat number value to `None`.
68+
69+
Return the `dict` of seats sent as the parameter after updating the empty seat values.
70+
71+
```python
72+
empty_seats(seats={1: "Alice", 2: None, 3: "Bob", 4: "George", 5: "Gloria"}, seat_numbers=[5,3,1])
73+
# => {1: None, 2: None, 3: None, 4: "George", 5: None}
74+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
In Python, `None` is frequently used to represent the absence of a value -- a placeholder to define a `null` (empty) variable, object, or argument.
2+
3+
If you've heard about or used a `NULL` or `nil` type in another programming language, then this usage of `None` in Python will be familiar to you. `None` helps you to declare variables or function arguments that you don't yet have values for. These can then be re-assigned to specific values later as needed.
4+
5+
```python
6+
a = None
7+
print(a)
8+
#=> None
9+
type(a)
10+
#=> <class 'NoneType'>
11+
12+
# Adding a Default Argument with `None`
13+
def add_to_todos(new_task, todo_list=None):
14+
if todo_list is None:
15+
todo_list = []
16+
todo_list.append(new_task)
17+
return todo_list
18+
19+
```
20+
21+
`None` will evaluate to `False` when used in a conditional check, so it is useful for validating the "presence of" or "absence of" a value - _any_ value -- a pattern frequently used when a function or process might hand back an error object or message.
22+
23+
```python
24+
a = None
25+
if a: #=> a will be evaluated to False when its used in a conditional check.
26+
print("This will not be printed")
27+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"authors": [
3+
{
4+
"github_username": "mohanrajanr",
5+
"exercism_username": "mohanrajanr"
6+
},
7+
{
8+
"github_username": "BethanyG",
9+
"exercism_username": "BethanyG"
10+
}
11+
]
12+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Goal
2+
3+
The goal of this exercise is to teach the student what None means and how None is used in Python.
4+
5+
## Learning objectives
6+
7+
- create a `None` object via literal declaration
8+
- check if an object is `None`
9+
- use a `None` object as a "False statement"
10+
- use `None` as a default parameter value
11+
12+
## Out of scope
13+
14+
- catching/raising Exceptions
15+
16+
## Concepts
17+
18+
- **none:**
19+
20+
## Prerequisites
21+
22+
- `bracket notation`,
23+
- `dicts`,
24+
- `dict-methods`,
25+
- `indexing`,
26+
- `len()`,
27+
- `lists`,
28+
- `list-methods`,
29+
- `loops()`,
30+
- `range()`
31+
32+
## Resources
33+
34+
- [the null object](https://docs.python.org/3/library/stdtypes.html#the-null-object)
35+
- [edspresso_none](https://www.educative.io/edpresso/what-is-the-none-keyword-in-python)
36+
- [none_keyword](https://docs.python.org/3/library/constants.html#None)
37+
- [None (Stackoverflow)](https://stackoverflow.com/questions/19473185/what-is-a-none-value)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
def new_seating_chart(size=22):
2+
seats = dict()
3+
for number in range(1, size+1):
4+
seats[number] = None
5+
return seats
6+
7+
def arrange_reservations(guests=None):
8+
if guests is None:
9+
return new_seating_chart()
10+
else:
11+
seats = new_seating_chart()
12+
for seat_number in range(1, len(guests)):
13+
seats[seat_number] = guests[seat_number]
14+
return seats
15+
16+
def find_all_available_seats(seats):
17+
available = []
18+
for seat_num, value in seats.items():
19+
if value is None:
20+
available.append(seat_num)
21+
return available
22+
23+
def curr_empty_seat_capacity(seats):
24+
count = 0
25+
for value in seats.values():
26+
if value is None:
27+
count += 1
28+
return count
29+
30+
def accommodate_waiting_guests(seats, guests):
31+
curr_empty_seats = curr_empty_seat_capacity(seats)
32+
empty_seat_list = find_all_available_seats(seats)
33+
if len(guests) > curr_empty_seats:
34+
return False
35+
else:
36+
for index in range(len(guests)):
37+
seats[empty_seat_list[index]] = guests[index]
38+
return seats
39+
40+
def empty_seats(seats, seat_numbers):
41+
for seat in seat_numbers:
42+
seats[seat] = None
43+
return seats

exercises/concept/none/none.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def new_seating_chart():
2+
pass
3+
4+
def arrange_reservations():
5+
pass
6+
7+
def find_all_available_seats():
8+
pass
9+
10+
def current_seating_capacity():
11+
pass
12+
13+
def accommodate_waiting_guests():
14+
pass
15+
def empty_seats():
16+
pass

exercises/concept/none/none_test.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import unittest
2+
from none import *
3+
4+
5+
class TestNoneType(unittest.TestCase):
6+
7+
def test_new_seating_chart_1(self):
8+
self.assertDictEqual(
9+
new_seating_chart(3),
10+
{1: None, 2: None, 3: None},
11+
msg="The New Seating chart does not match with the expected."
12+
)
13+
14+
def test_new_seating_chart_2(self):
15+
self.assertDictEqual(
16+
new_seating_chart(),
17+
{1: None, 2: None, 3: None, 4: None, 5: None, 6: None, 7: None, 8: None, 9: None, 10: None, 11: None, 12: None, 13: None, 14: None, 15: None, 16: None, 17: None, 18: None, 19: None, 20: None, 21: None, 22: None},
18+
msg="The New Seating chart does not match with the expected."
19+
)
20+
21+
def test_arrange_reservations_1(self):
22+
self.assertDictEqual(
23+
arrange_reservations(guests=["Walter", "Frank", "Jenny", "Carol", "Alice", "George"]),
24+
{1: 'Frank', 2: 'Jenny', 3: 'Carol', 4: 'Alice', 5: 'George', 6: None, 7: None, 8: None, 9: None, 10: None, 11: None, 12: None, 13: None, 14: None, 15: None, 16: None, 17: None, 18: None, 19: None, 20: None, 21: None, 22: None},
25+
msg="The reservation dict is incorrect"
26+
)
27+
28+
def test_arrange_reservations_2(self):
29+
self.assertDictEqual(
30+
arrange_reservations(),
31+
{1: None, 2: None, 3: None, 4: None, 5: None, 6: None, 7: None, 8: None, 9: None, 10: None, 11: None, 12: None, 13: None, 14: None, 15: None, 16: None, 17: None, 18: None, 19: None, 20: None, 21: None, 22: None},
32+
msg="The reservation dict is incorrect"
33+
)
34+
35+
def test_find_all_available_seats_1(self):
36+
self.assertListEqual(
37+
find_all_available_seats(seats = {1: None, 2: 'Frank', 3: 'Jenny', 4: None, 5: 'Alice', 6: 'George', 7: None, 8: 'Carol', 9: None, 10: None, 11: None, 12: 'Walter'}),
38+
[1,4,7,9,10,11],
39+
msg="The Available Seat list is incorrect"
40+
)
41+
42+
def test_find_all_available_seats_2(self):
43+
self.assertListEqual(
44+
find_all_available_seats(seats = {1: None, 2: None, 3: None, 4: None, 5: 'Alice', 6: None, 7: None, 8: None, 9: None, 10: None, 11: None, 12: None}),
45+
[1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12],
46+
msg="The Available Seat list is incorrect"
47+
)
48+
49+
def test_curr_empty_seat_capacity_1(self):
50+
self.assertIs(
51+
curr_empty_seat_capacity({1: "Occupied", 2: None, 3: "Occupied"}),
52+
1,
53+
msg="the index of the seat which is empty is invalid."
54+
)
55+
56+
def test_curr_empty_seat_capacity_2(self):
57+
self.assertIs(
58+
curr_empty_seat_capacity({1: "Occupied", 2: "Occupied", 3: None, 4: "Occupied", 5: None}),
59+
2,
60+
msg="the index of the seat which is empty is invalid."
61+
)
62+
63+
def test_accommodate_waiting_guests_1(self):
64+
starting_reservations = {1: 'Carol', 2: 'Alice', 3: 'George', 4: None, 5: None, 6: None, 7: 'Frank', 8: 'Walter'}
65+
self.assertFalse(
66+
accommodate_waiting_guests(starting_reservations, ["Mort", "Suze", "Phillip", "Tony"]),
67+
msg="The Accomodation of waiting guests are incorrect"
68+
)
69+
70+
def test_accommodate_waiting_guests_2(self):
71+
starting_reservations = {1: None, 2: None, 3: None, 4: 'Carol', 5: 'Alice', 6: 'George', 7: None, 8: None, 9: None, 10: None, 11: None, 12: None, 13: None, 14: None, 15: None, 16: None, 17: None, 18: 'Frank', 19: 'Jenny', 20: None, 21: None, 22: 'Walter'}
72+
self.assertDictEqual(
73+
accommodate_waiting_guests(starting_reservations, ["Mort", "Suze", "Phillip", "Tony"]),
74+
{1: 'Mort', 2: 'Suze', 3: 'Phillip', 4: 'Carol', 5: 'Alice', 6: 'George', 7: 'Tony', 8: None, 9: None, 10: None, 11: None, 12: None, 13: None, 14: None, 15: None, 16: None, 17: None, 18: 'Frank', 19: 'Jenny', 20: None, 21: None, 22: 'Walter'},
75+
msg="The Accomodation of waiting guests are incorrect"
76+
)
77+
78+
def test_empty_seats_1(self):
79+
self.assertDictEqual(
80+
empty_seats(seats={1: "Alice", 2: None, 3: "Bob", 4: "George", 5: "Gloria"}, seat_numbers=[5,3,1]),
81+
{1: None, 2: None, 3: None, 4: "George", 5: None},
82+
msg="Seats are not emptied properly"
83+
)
84+
85+
def test_empty_seats_2(self):
86+
self.assertDictEqual(
87+
empty_seats(seats={1: "Alice", 2: None, 3: "Bob", 4: "George", 5: "Gloria"}, seat_numbers=[]),
88+
{1: "Alice", 2: None, 3: "Bob", 4: "George", 5: "Gloria"},
89+
msg="Seats are not emptied properly"
90+
)

0 commit comments

Comments
 (0)