Advertisement
Acer1968

Redukce streamu na min a max hodnotu

Jul 5th, 2022
852
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None
  1. data = ['X-1', 'Y-1', 'Y-2', 'X0', 'X-2', 'X-4', 'Y0', 'Y3', 'Y6', 'Y66', 'X-3', 'Y-3', 'X-4', 'Y-4', 'X-5', 'Y-5', 'Y-4', 'X-337', 'X-22', 'X-3', 'X-2', 'X-1', 'X0', 'Y0', 'Y100']
  2.  
  3. def read_seq(position, d_in):
  4.     temp_pos = position
  5.     counter = 1
  6.     first_letter = d_in[position][0]
  7.     values = []
  8.     while temp_pos < len(d_in):
  9.         temp = d_in[temp_pos]
  10.         letter = temp[0]
  11.         value = int(temp[1:])
  12.         if letter != first_letter:
  13.             if len(values) > 1:
  14.                 my_min_index = values.index(min(values))
  15.                 my_max_index = values.index(max(values))
  16.                 if my_min_index < my_max_index:
  17.                     return temp_pos, [first_letter+str(min(values)), first_letter+str(max(values))]
  18.                 else:
  19.                     return temp_pos, [first_letter+str(max(values)), first_letter+str(min(values))]
  20.             else:
  21.                 return temp_pos, [first_letter+str(values[0])]
  22.         else:
  23.                 print(f"{counter:03d}...Pozice: {temp_pos:03d}. Písmeno {letter} s hodnotou {value}")
  24.  
  25.         values.append(value)
  26.         temp_pos += 1
  27.         counter += 1
  28.     else:
  29.         if len(values) > 1:
  30.             my_min_index = values.index(min(values))
  31.             my_max_index = values.index(max(values))
  32.             if my_min_index < my_max_index:
  33.                 return temp_pos, [first_letter+str(min(values)), first_letter+str(max(values))]
  34.             else:
  35.                 return temp_pos, [first_letter+str(max(values)), first_letter+str(min(values))]
  36.         else:
  37.             return temp_pos, [first_letter+str(values[0])]
  38.  
  39.  
  40. readed_position = 0
  41. result = []
  42. while len(data) > readed_position:
  43.     print(f"Zpracovávám od {readed_position:03d}... Nová sekvence")
  44.     readed_position, new_minmax = read_seq(readed_position, data)
  45.     result.extend(new_minmax)
  46.     print(f"Zredukovaná sada {result}")
  47.  
  48.  
Advertisement
RAW Paste Data Copied
Advertisement