Answer the question
In order to leave comments, you need to log in
How to optimize python code?
There is a list of daily temperatures T. For each day in the input, you need to determine how many days you have to wait for a warmer temperature. If there is no warmer temperature in the following days, the number of days will be 0.
Input:
T - list of daily temperatures, temperature values from 30 to 100
Output:
array with the number of days
Example:
T = [73, 74, 75, 71, 69, 72, 76, 73]
daysNumber( T ) --> [1, 1, 4, 2, 1, 1, 0, 0]
Implement the daily_temperatures function
t = [73, 74, 75, 71, 69, 72, 76, 73]
def daily_temperatures(lst):
list_count = []
for start_index in range(len(lst)):
count = 0
for letter in range(start_index + 1, len(lst)):
if lst[letter] > lst[start_index]:
count += 1
list_count.append(letter - start_index)
break
if count == 0:
list_count.append(0)
return list_count
daily_temperatures(t)
Answer the question
In order to leave comments, you need to log in
days = []
for ind, temp in enumerate(t):
temp_greater = next(((i, val) for i, val in enumerate(t[ind+1:], ind+1) if val > temp), 0)
day = temp_greater[0]-ind if temp_greater else 0
days.append(day)
One of the options "on the forehead" (without using additional functions like takewhile):
def days_till_warming(T):
counts = []
for i, curr_temp in enumerate(T):
for j, next_temp in enumerate(T[i+1:], i + 1):
if next_temp > curr_temp:
delta = j - i
break
else:
delta = 0
counts.append(delta)
return counts
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question