from itertools import *
import collections
for i in combinations_with_replacement(['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'],15):
b = (''.join(i))
freq = collections.Counter(b)
for k in freq:
if freq [k] < 5:
print(k)
this code most print chars what count if less than 5
what i try do , cheek if at string from join at fly if there is repeated any of characters les than x times at any possition of that string and print strings only what true to that.
Problem is no mater what i try do , or its print all and ignore if ... or print notting. how do it right , or maybe at python exist simple solution ?
Result most be as example les than 5
False - fffaaffbbdd ( repeat 5 titemes f)
False - fffffaaaaac ( repeat 5 times a and f)
True - aaabbbccc11 ( no any character repeated more than 4 times )
More clear explain qustion - filter all string with characters more than x repetions before give to next function. As examble - there is simple print that strings , and not print strings what not at rule.
a more constructive approach (meaning: i do not iterate over all possible combinations - i construct the valid combinations directly).
you need to have
sympy
installed for this to work.in the example i only use the elements
"abcdef"
and restrict the repetitions to be strictly smaller thanMAX = 4
. i fix the length of the strings to be output atM = 6
.i start by getting all the
partitions
ofM
with restricted repetitionsk=MAX - 1
and not constisting of more thanm=N
parts. i immediately convert those to a list:of those lists i iterate over the multiset permutations - i mean those to represent the elements that i select and how often they are repeated: e.g:
the result you want is then the multiset permutation of those strings.
for your case you'd then need to change:
but the complexity of that is huge (but way better that the one of the original approach)!