Everytime you write a piece of code, I expect it to be wrapped in a function, or a class
For randomization, you should use package random. In details, on each of the scripts that you might have, do the following:
import random
random.seed(420)
Meaning, if you have 2 scrips, all 2 scripts should have those two lines at the beginning. This is for reproducibility: you will random the same things over and over again as you execute your code.
For constant thing, like the following distance dictionairy assignment (see below), you could write a function that populate the dictionairy, return it, and assign a variable to it
For example:
def populate_distance_dict() -> dict[str, dict[str, typing.Any]]:
# insert your logic here
answer = ....
return answer
distance_dict = populate_distance_dict()
4. [OPTIONAL] Write test with pytest to ensure every contraints mentioned below are met. If you cannot meet this optional requirement with the deadline mentioned in the next cell, you will be asked to do it in 0.5 week from the date you finish this assignment. In other words, you can't run away from this.
Here are the problems:
- Write python script that populate trip table. In addition, it will have these following columns:
1. trip_id: str (primary key)
2. name: str
3. source_location: str
4. destination_location: str
6. duration_mins: float
For example:
def populate_table():
do stuff
populate_table()
- Populate a nested distance dictionairy between two streets from this given list of streets
> [street_1, street_2, street_3, ... street_50,000]
. This will follow product rules: https://docs.python.org/3/library/itertools.html#itertools.product
The distance between any two streets is a random number between 50 and 800.
Expected dict answer for list of [street_1, street_2, street_3]:
```distance = {
'street_1': {
'street_2': 50,
'street_3': 100,
}
'street_2': {
'street_1': 50,
'street_3': 85,
},
'street_3': {
'street_1': 100,
'street_2': 85
}
}
Rules on how to populate these columns:
trip_id: a unique number from 1 to 100,000. Meaning the table will have 100,000 unique records
name: a random string chosen from (alon, sarah, hailey, tyler, chaos, birtie, shiner, huy, jon, luis)
source_location: a random string chosen from the above distance keys
destination_location: a random string chosen at distance[source_location]
For example, if source_location is randomly chosen as 'street_1', destination_location is randomly chosen as either 'street_2' or 'street_3'
duration_mins: a random float that depends on distance (which is deducted from step 3 and 4) with the following rules, in the following order of priorities:
If distance is from 50 to 100: 50.0
If distance is from 100 to 200: 80.0
If distance is from 200 to 300: 250.0
If distance is above 300:
If name is shiner or birtie or chaos: 30.0
If name has no letter 'e' and distance below 350: 150
If the number of letters in name is no more than 4 and distance below 420: 420
- Overal expection:
random
. In details, on each of the scripts that you might have, do the following:Meaning, if you have 2 scrips, all 2 scripts should have those two lines at the beginning. This is for reproducibility: you will random the same things over and over again as you execute your code.
distance_dict = populate_distance_dict()
def populate_table():
do stuff
populate_table()