0

For example,

# list A
A = [1, 2, 3, 3, 4, 4, 4, 5, 6]

# list B
B = [1, 2, 3, 3, 4, 5, 5, 5, 6]

are given, and we have to check if elements of the lists have the pattern of [x, x, y, y, y].

list A should return "True": [1, 2, 3, 3, 4, 4, 4, 5, 6]

list B should return "False" as '4' intercepts in the middle: [1, 2, 3, 3, 4, 5, 5, 5, 6]

Michael S.
  • 3,050
  • 4
  • 19
  • 34
CescoDesu
  • 21
  • 3
  • Hint: If you can loop through the list n-items at a time, you can check all items in n for a pattern, see https://stackoverflow.com/questions/434287/how-to-iterate-over-a-list-in-chunks – alvas Sep 06 '22 at 09:18
  • itertools.groupby & then check if [2, 3] exists there – Mustafa Aydın Sep 06 '22 at 09:19
  • @CescoDesu I disagree with this question being closed. Here is my answer: https://pastebin.pl/view/b172e3de – Tom McLean Sep 06 '22 at 09:37
  • Thank you guys for the answers, I am very touched by the number of helpers. I have seen all the hints and codes, but I guess I'll need more time to fully understand how it works. I will try first by myself with the hints given by sir Alvas and Mustafa Aydin, and will try to compare with the code that sir Tom McLean gave. Once again, thank you for the reply! – CescoDesu Sep 06 '22 at 11:35

2 Answers2

1

Just check every possible subarray of length 5 for the pattern. Straightforward way would be:

a = [1, 2, 3, 3, 4, 4, 4, 5, 6]
b = [1, 2, 3, 3, 4, 5, 5, 5, 6]


def is_pattern(alist):
    for i in range(0, len(alist) - 4):
        sub = alist[i:i + 5]
        if sub[0] == sub[1] and (sub[2] == sub[3] == sub[4]):
            return True
    return False


assert is_pattern(a) is True
assert is_pattern(b) is False
funnydman
  • 9,083
  • 4
  • 40
  • 55
  • First of all, thank you for the reply! I see your code is shortest among all the replies, and it has almost the same concept with my code (which was failed, but I'm still on to it!). I can see that this is much shorter and far advanced than my code, because mine actually had numbers of if conditions which were unfinished. Once again, thank you for the reply! – CescoDesu Sep 06 '22 at 11:43
  • @CescoDesu you're welcome! If some answer of the provided was helpful to you don't forget to approve it. – funnydman Sep 06 '22 at 11:47
0

O(n) solution is to iterate the list and count X and Y appearances. Once it breaks the pattern, restore the values accordingly.

Once you find 2 or more appearances you can count Y appearances and return true once it reaches 3.

Otherwise, if count x is less than 2, restore it. If count x is 2 or more but current value doesn't equal to the earlier y, set count x to be count y and set count y to be 1.

a = [1, 2, 3, 3, 3, 4, 4, 4, 5, 6]
b = [1, 2, 3, 3, 4, 5, 5, 5, 6]
c = [1,1,1,1,1]
d = [1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 6]

def is_x_y(arr):
    count_x, count_y = 0, 0
    x, y = None, None
    
    for val in arr:
        if not x:
            x = val
        if x == val:
            count_x += 1
        elif count_x >= 2:
            if not y:
                y = val
            if y == val:
                count_y +=1
            else:
                count_x = count_y
                x = y
                count_y = 1
                y = val
        else:
            count_x = 0
            x = None
        if count_x >= 2 and count_y == 3:
            return True
    return False


print(is_x_y(a))
print(is_x_y(b))
print(is_x_y(c))
print(is_x_y(d))

True
False
False
True

if x, x, x, x, x also match your pattern, change the return True condition to also return True on count_x = 5:

if count_x == 5 or (count_x >= 2 and count_y == 3):
    return True
  • Thank you for the reply, sir Yoav! I find your answer very detailed so I think if I'll look at your code few times more, then I can understand the operations. My code also had many if conditions to separate the task step by step, but I was also lost somewhere in the ifs. I will take your answer as your teaching for finishing my code, for me to follow our old quote; "Teach a man to fish, and you feed him for a lifetime"! – CescoDesu Sep 06 '22 at 12:08
  • @CescoDesu good luck. If you are not looking for optimized efficiency I would go with the other solutions mentioned up here - O(nlog5n) or O(n^2), assuming your input lists are not big. If this is part of an academic task, I would recommend using my solution that requires single iteration (and probably enhancing it as I may missed some edge cases) – Yoav Sheetrit Sep 06 '22 at 15:00