Number of days with traffic intensity from tuples in a list
Question:
I’m trying to write a function that uses another function called traffic_intensity and takes a list of tuples, returning the number of days with the given traffic intensity. This is what I have at the moment:
def traffic_intensity(count):
"""Returns string indicating intensity level given by number of
vehicles"""
level_name = ""
if(count < 5000):
level_name = "Very Low"
elif(count >= 5000 and count < 10000):
level_name = "Low"
elif(count >= 10000 and count < 18000):
level_name = "Moderate"
elif(count >= 18000):
level_name = "High"
return(level_name)
def n_days_at_intensity(vehicle_records, intensity):
"""Returns number of days with the given traffic intensity level"""
days = 0
for number in intensity:
traffic = traffic_intensity(number)
if traffic == intensity:
days+= 1
return days
This is currently giving me an error
But it is supposed to return me an output of
3
for the test code:
vehicle_records = [('2010-01-01',1),
('2010-01-02',2),
('2010-01-03',3)]
days = n_days_at_intensity(vehicle_records, 'Very Low')
print(days)
and an output of
0
for test code
voiture_records = [('2010-01-01',1),
('2010-01-02',2),
('2010-01-03',3)]
days = n_days_at_intensity(voiture_records, 'Moderate')
print(days)
Can someone please tell me how my code can be fixed to get these outputs?
Answers:
For n_days_at_intensity
, the vehicle_records
you passed in is a list of tuples
. Try looping through the list of tuples
as below:
def n_days_at_intensity (vehicle_records, intensity):
"""Returns number of days with the given traffic intensity level"""
days = 0
# loop through list of tuples
for r in vehicle_records:
r_date = r[0]
r_count = r[1]
traffic = traffic_intensity(r_count)
if traffic == intensity:
days += 1
return days
Then calling the below should returns 3
:
vehicle_records = [('2010-01-01',1),
('2010-01-02',2),
('2010-01-03',3)]
days = n_days_at_intensity(vehicle_records, 'Very Low')
print(days)
I’m trying to write a function that uses another function called traffic_intensity and takes a list of tuples, returning the number of days with the given traffic intensity. This is what I have at the moment:
def traffic_intensity(count):
"""Returns string indicating intensity level given by number of
vehicles"""
level_name = ""
if(count < 5000):
level_name = "Very Low"
elif(count >= 5000 and count < 10000):
level_name = "Low"
elif(count >= 10000 and count < 18000):
level_name = "Moderate"
elif(count >= 18000):
level_name = "High"
return(level_name)
def n_days_at_intensity(vehicle_records, intensity):
"""Returns number of days with the given traffic intensity level"""
days = 0
for number in intensity:
traffic = traffic_intensity(number)
if traffic == intensity:
days+= 1
return days
This is currently giving me an error
But it is supposed to return me an output of
3
for the test code:
vehicle_records = [('2010-01-01',1),
('2010-01-02',2),
('2010-01-03',3)]
days = n_days_at_intensity(vehicle_records, 'Very Low')
print(days)
and an output of
0
for test code
voiture_records = [('2010-01-01',1),
('2010-01-02',2),
('2010-01-03',3)]
days = n_days_at_intensity(voiture_records, 'Moderate')
print(days)
Can someone please tell me how my code can be fixed to get these outputs?
For n_days_at_intensity
, the vehicle_records
you passed in is a list of tuples
. Try looping through the list of tuples
as below:
def n_days_at_intensity (vehicle_records, intensity):
"""Returns number of days with the given traffic intensity level"""
days = 0
# loop through list of tuples
for r in vehicle_records:
r_date = r[0]
r_count = r[1]
traffic = traffic_intensity(r_count)
if traffic == intensity:
days += 1
return days
Then calling the below should returns 3
:
vehicle_records = [('2010-01-01',1),
('2010-01-02',2),
('2010-01-03',3)]
days = n_days_at_intensity(vehicle_records, 'Very Low')
print(days)