Challenge: Choosing Optimal Data Structures
Opgave
Swipe to start coding
Your task is to choose the most appropriate data structure (list
, ndarray
, set
, or tuple
) for each of the different scenarios below. Based on the nature of the data and the specific requirements for each collection, fill in the blanks (___
) to create the correct data structures. Make sure to use the appropriate brackets for each structure, and if creating a NumPy array, initialize it based on a list.
- For
monthly_sales
, the data is numerical, and you will need to perform frequent calculations on it. user_ids
, each ID should be unique, and you will need to frequently check if a certain ID is present.product
should be a fixed (unchangeable) record.tasks
should be an ordered collection and allow for frequent additions and removals.
Løsning
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import numpy as np
# Store monthly sales data (numerical data requiring calculations)
monthly_sales = np.array([320, 450, 500, 480, 350, 610, 720, 640, 510, 470, 420, 580])
# Keep track of unique user IDs who logged in during a day
user_ids = {1023, 2045, 1098, 2045, 3098, 1023}
# Define a product record
product = (2134, 'Wireless Mouse')
# Store an ordered collection of tasks where new tasks can be added or removed frequently
tasks = ['Send report', 'Review code', 'Update website', 'Fix bug']
print(type(monthly_sales))
print(type(user_ids))
print(type(product))
print(type(tasks))
Var alt klart?
Tak for dine kommentarer!
Sektion 2. Kapitel 3
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import numpy as np
# Store monthly sales data (numerical data requiring calculations)
monthly_sales = ___320, 450, 500, 480, 350, 610, 720, 640, 510, 470, 420, 580
# Keep track of unique user IDs who logged in during a day
user_ids = ___1023, 2045, 1098, 2045, 3098, 1023
# Define a product record
product = ___2134, 'Wireless Mouse'
# Store an ordered collection of tasks where new tasks can be added or removed frequently
tasks = ___'Send report', 'Review code', 'Update website', 'Fix bug'
print(type(monthly_sales))
print(type(user_ids))
print(type(product))
print(type(tasks))
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat