Advertisement
mmandrille

Untitled

Aug 18th, 2022 (edited)
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. '''
  2. Tenes un vector con las temperaturas de cada día.
  3. Por ejemplo: [20, 23, 22, 20, 19, 24, 24]
  4. Qué algoritmo implementarías para saber, para cada día, cuantos días tenes que esperar para que la temperatura sea mayor o igual a la de ese día.
  5. El vector de salida pra el ejemple debería ser [1, 4, 3, 2, 1, 0]
  6. En caso de que no haya un dia de mayor temperatura devolver 0.
  7. '''
  8. #package imports
  9. from queue import Queue
  10.  
  11. # Definitions
  12. weather = [20, 23, 22, 20, 19, 24, 24, 21]
  13. expected = [1, 4, 3, 2, 1, 1, 0, 0]
  14.  
  15. # Define our dict
  16. class TempsDict(dict):
  17.     def add_day(self, day, temp):
  18.         if temp not in self:
  19.             self[temp] = []
  20.         self[temp].append(day)
  21.    
  22.     def get_higher(self, day, temp):
  23.         # Example of self: {20: [0, 3], 23: [1], 22: [2], 19: [4], 24: [5, 6]}
  24.         valids = set()
  25.         [
  26.             [
  27.                 valids.add(d-day) for d in self[t] if d>day # 2. We only add the higher days (No itself)
  28.             ] for t in (
  29.                 t for t in self.keys() if t>=temp # 1. We get all temps higher or equal to param
  30.             )
  31.         ]
  32.         # Conditional return
  33.         return min(valids) if valids else 0
  34.  
  35. # Run:
  36. if __name__ == "__main__":
  37.     print(f"Weather is: {weather}")
  38.     print(f"We expected: {expected}")
  39.    
  40.     print("First Attempt:")
  41.     # Generate dict --> First loop
  42.     temp_dict = TempsDict()
  43.     for idx in range(len(weather)):
  44.         temp_dict.add_day(idx, weather[idx])
  45.     print(f"Dict Generated: {temp_dict}")
  46.     # Resolve:
  47.     response = []
  48.     for idx in range(len(weather)):
  49.         response.append(temp_dict.get_higher(idx, weather[idx]))
  50.     # Check result
  51.     print(f"Checking Response: {response}")
  52.     assert response == expected
  53.     print("Success:\n")
  54.    
  55.     print("Second Attempt:")
  56.     # We will use a Stack
  57.     stack = Queue() # FIFO stack
  58.     response = []
  59.     max = 0
  60.     # Weather is: [20, 23, 22, 20, 19, 24, 24]
  61.     for idx in range(len(weather)): # First loop
  62.         # if higher We load the stack
  63.         if not stack or (weather[idx] >= max): # If no stack or temp is higher than last
  64.             max = weather[idx] # We save max value
  65.             # We load it on stack
  66.             stack.put((weather[idx],idx)) # Keep loading
  67.             print(f"We loaded items to stack: {stack.queue}, results: {response}")
  68.             # We generate results:
  69.             last = stack.get()
  70.             response += [d+1 for d in range(last[1]-len(response))[::-1]]
  71.     # We need to append 0 to not responded ones:
  72.     response += [0] * (len(weather) - len(response))
  73.     # Check result
  74.     print(f"Checking Response: {response}")
  75.     assert response == expected
  76.     print("Success:\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement