I need a python or postgres script that will check if string contains duplicate of 6 or more digits in it. For example "811811113111711","822222222255555" should give true, and "812345678901234" should give false. String is a number, always of the same length - 16 and starts with 8, that is validated before this.
Currently have this but it’s a very basic solution and only work for the characters next to each other and I need all in the number.
a_string = "811111111111111"
matches = ["000000", "111111", "222222", "333333", "444444", "555555", "666666", "777777", "888888", "999999"]
if any([x in a_string for x in matches]):
print('true')
else:
print('false')
>Solution :
You likely want to use collections.Counter. This can be thought like a multiset, i.e. a set that can have multiple occurences of a value. It’s implemented with a dictionary where the keys are the set items and the values are the number of occurenecs.
For example,
>>> from collections import Counter
>>> Counter("11333111333")
Counter({'3': 6, '1': 5})
From there one you can filter the counter to see if you have relevant data:
some_counter = Counter(some_string)
duplicate_digits = {k : v for k,v in some_counter.items() if k in "0123456789" and v >= 6}
if duplicate_digits is not empty, you have counted successfully a digit more than six times.