asigalov61 commited on
Commit
2742b7b
·
verified ·
1 Parent(s): 62fabe0

Upload TMIDIX.py

Browse files
Files changed (1) hide show
  1. TMIDIX.py +100 -1
TMIDIX.py CHANGED
@@ -51,7 +51,7 @@ r'''############################################################################
51
 
52
  ###################################################################################
53
 
54
- __version__ = "25.8.7"
55
 
56
  print('=' * 70)
57
  print('TMIDIX Python module')
@@ -13695,6 +13695,105 @@ def flip_list_columns(lst):
13695
 
13696
  ###################################################################################
13697
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13698
  print('Module loaded!')
13699
  print('=' * 70)
13700
  print('Enjoy! :)')
 
51
 
52
  ###################################################################################
53
 
54
+ __version__ = "25.8.21"
55
 
56
  print('=' * 70)
57
  print('TMIDIX Python module')
 
13695
 
13696
  ###################################################################################
13697
 
13698
+ def exists(sub, lst):
13699
+ sub_len = len(sub)
13700
+ return any(lst[i:i + sub_len] == sub for i in range(len(lst) - sub_len + 1))
13701
+
13702
+ ###################################################################################
13703
+
13704
+ def exists_noncontig(sub, lst):
13705
+ it = iter(lst)
13706
+ return all(x in it for x in sub)
13707
+
13708
+ ###################################################################################
13709
+
13710
+ def exists_ratio(sub, lst, ratio):
13711
+ matches = sum(x in set(lst) for x in sub)
13712
+ return matches / len(sub) >= ratio
13713
+
13714
+ ###################################################################################
13715
+
13716
+ def chunk_by_threshold_mode(nums, threshold=0, normalize=False):
13717
+
13718
+ if not nums:
13719
+ return []
13720
+
13721
+ chunks = []
13722
+ chunk = []
13723
+ freq = defaultdict(int)
13724
+ max_freq = 0
13725
+ mode_val = None
13726
+
13727
+ def try_add_and_validate(value):
13728
+
13729
+ nonlocal max_freq, mode_val
13730
+
13731
+ chunk.append(value)
13732
+ freq[value] += 1
13733
+ new_max_freq = max_freq
13734
+ candidate_mode = mode_val
13735
+
13736
+ if freq[value] > new_max_freq:
13737
+ new_max_freq = freq[value]
13738
+ candidate_mode = value
13739
+
13740
+ mode = candidate_mode
13741
+ valid = True
13742
+
13743
+ for v in chunk:
13744
+ if abs(v - mode) > threshold:
13745
+ valid = False
13746
+ break
13747
+
13748
+ if not valid:
13749
+
13750
+ chunk.pop()
13751
+ freq[value] -= 1
13752
+ if freq[value] == 0:
13753
+ del freq[value]
13754
+
13755
+ return False
13756
+
13757
+ max_freq = new_max_freq
13758
+ mode_val = mode
13759
+ return True
13760
+
13761
+ for num in nums:
13762
+ if not chunk:
13763
+ chunk.append(num)
13764
+ freq[num] = 1
13765
+ mode_val = num
13766
+ max_freq = 1
13767
+
13768
+ else:
13769
+ if not try_add_and_validate(num):
13770
+ if normalize:
13771
+ normalized_chunk = [mode_val] * len(chunk)
13772
+ chunks.append(normalized_chunk)
13773
+
13774
+ else:
13775
+ chunks.append(chunk[:])
13776
+
13777
+ chunk.clear()
13778
+ freq.clear()
13779
+
13780
+ chunk.append(num)
13781
+ freq[num] = 1
13782
+ mode_val = num
13783
+ max_freq = 1
13784
+
13785
+ if chunk:
13786
+ if normalize:
13787
+ normalized_chunk = [mode_val] * len(chunk)
13788
+ chunks.append(normalized_chunk)
13789
+
13790
+ else:
13791
+ chunks.append(chunk)
13792
+
13793
+ return chunks
13794
+
13795
+ ###################################################################################
13796
+
13797
  print('Module loaded!')
13798
  print('=' * 70)
13799
  print('Enjoy! :)')