Upload TMIDIX.py
Browse files
TMIDIX.py
CHANGED
|
@@ -48,7 +48,7 @@ r'''
|
|
| 48 |
|
| 49 |
###################################################################################
|
| 50 |
|
| 51 |
-
__version__ = "26.5.
|
| 52 |
|
| 53 |
###################################################################################
|
| 54 |
|
|
@@ -18917,6 +18917,53 @@ def find_best_ngram_match(
|
|
| 18917 |
|
| 18918 |
###################################################################################
|
| 18919 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18920 |
print('Module loaded!')
|
| 18921 |
print('=' * 70)
|
| 18922 |
print('Enjoy! :)')
|
|
|
|
| 48 |
|
| 49 |
###################################################################################
|
| 50 |
|
| 51 |
+
__version__ = "26.5.18" # TMIDIX version
|
| 52 |
|
| 53 |
###################################################################################
|
| 54 |
|
|
|
|
| 18917 |
|
| 18918 |
###################################################################################
|
| 18919 |
|
| 18920 |
+
def remove_repeating_patterns(lst: list[int]) -> list[int]:
|
| 18921 |
+
"""
|
| 18922 |
+
Removes consecutive repeating values AND consecutive repeating patterns
|
| 18923 |
+
strictly from left to right.
|
| 18924 |
+
|
| 18925 |
+
Examples:
|
| 18926 |
+
[1, 2, 2, 3] -> [1, 2, 3]
|
| 18927 |
+
[1, 2, 3, 4, 3, 3, 2, 1, 2, 1, 2] -> [1, 2, 3, 4, 3, 2, 1, 2]
|
| 18928 |
+
[1, 2, 1, 2, 1, 2] -> [1, 2]
|
| 18929 |
+
[1, 2, 3, 1, 2, 3] -> [1, 2, 3]
|
| 18930 |
+
[1, 1, 1, 1] -> [1]
|
| 18931 |
+
"""
|
| 18932 |
+
n = len(lst)
|
| 18933 |
+
if n <= 1:
|
| 18934 |
+
return lst[:]
|
| 18935 |
+
|
| 18936 |
+
# deque provides O(1) appends and O(1) pops from the right end,
|
| 18937 |
+
# while perfectly preserving the original left-to-right history.
|
| 18938 |
+
res = deque()
|
| 18939 |
+
res.append(lst[0])
|
| 18940 |
+
|
| 18941 |
+
for i in range(1, n):
|
| 18942 |
+
val = lst[i]
|
| 18943 |
+
res.append(val)
|
| 18944 |
+
|
| 18945 |
+
# The maximum possible pattern length is half the current sequence length
|
| 18946 |
+
max_len = len(res) >> 1
|
| 18947 |
+
|
| 18948 |
+
# Check from the longest possible pattern down to 1
|
| 18949 |
+
for p_len in range(max_len, 0, -1):
|
| 18950 |
+
match = True
|
| 18951 |
+
for j in range(p_len):
|
| 18952 |
+
# Compare the right-most p_len elements with the p_len elements just before them
|
| 18953 |
+
if res[-p_len + j] != res[-2 * p_len + j]:
|
| 18954 |
+
match = False
|
| 18955 |
+
break
|
| 18956 |
+
|
| 18957 |
+
if match:
|
| 18958 |
+
# Pattern found! Pop the matching elements off the end.
|
| 18959 |
+
for _ in range(p_len):
|
| 18960 |
+
res.pop()
|
| 18961 |
+
break
|
| 18962 |
+
|
| 18963 |
+
return list(res)
|
| 18964 |
+
|
| 18965 |
+
###################################################################################
|
| 18966 |
+
|
| 18967 |
print('Module loaded!')
|
| 18968 |
print('=' * 70)
|
| 18969 |
print('Enjoy! :)')
|