Abhik Chakraborty
2 min readDec 18, 2024

--

Not sure if the question had those brackets as strings but if they did, this code should work:

```

def get_pattern(l1,l2):

from collections import Counter

# Getting a dictionary with the count of each number

num_dict = Counter(l1)

# Getting a dictionary with the remainder of each number

remainder_dict = {k:k%len(l2) for k in num_dict.keys()}

result = []

# Looping through the dictionary and each number

for key in num_dict:

# If the value of the number is less than length of the bracket list, we are getting the relevant

# bracket from the list l2 by subtracing 1 from the key to get the index of the bracket

if key < len(l2):

bracket = l2[key-1]

# If the value of the number is greater than the length of the bracket list, we are getting the remainder (i)

else:

i = key%len(l2)

# If the remainder is less than the length of the bracket list, we are using the logic above and getting

# the bracket by subtracing 1 from the remainder to get the index of the bracket

if i < len(l2):

bracket = l2[i-1]

# If remainder is 0 it means we have to get the last bracket from the list so we are getting the last bracket

# from the list l2 by subtracing 1 from the length of the list l2 to get the index of the last bracket

elif i==0:

bracket = l2[len(l2)-1]

# Getting the pattern by joining the bracket with the number of times the number is present in the list

# by using the join function and converting the number to string with the help of str function and multiplying

# the string with the number of times the number is present in the list and separating the numbers with a comma

# and adding the bracket at the start and end of the pattern

pattern = bracket[0]+",".join(str(key)*num_dict[key])+bracket[1]

# Appending the pattern to the result list

result.append(pattern)

return result

if __name__ == "__main__":

# Test case

A = [1,2,3,4,5,5,1,1,3,6,7,7,7,7,7,7,8,8,4,4,4,3,3,9,9,9,9,9]

B = ["{}","[]","()"]

print(get_pattern(A,B))

```

--

--

Abhik Chakraborty
Abhik Chakraborty

Written by Abhik Chakraborty

Data Engineering Manager at Expedia | Ex - Amazon -------- Book a session with me: https://topmate.io/abhik_chakraborty10/1331501

Responses (1)