Simply put, I would like to know if there is a way to print out coin flip combinations that only meet a certain criteria. In this case, only those with <=n consecutive H's or T's gets printed.
For example, here's all possible combinations of 5 coin flips using itertools.product*:
>>> list(itertools.product('HT', repeat=5))
[('H', 'H', 'H', 'H', 'H'), ('H', 'H', 'H', 'H', 'T'), ('H', 'H', 'H', 'T', 'H'), ('H', 'H', 'H', 'T', 'T'),
('H', 'H', 'T', 'H', 'H'), ('H', 'H', 'T', 'H', 'T'), ('H', 'H', 'T', 'T', 'H'), ('H', 'H', 'T', 'T', 'T'),
('H', 'T', 'H', 'H', 'H'), ('H', 'T', 'H', 'H', 'T'), ('H', 'T', 'H', 'T', 'H'), ('H', 'T', 'H', 'T', 'T'),
('H', 'T', 'T', 'H', 'H'), ('H', 'T', 'T', 'H', 'T'), ('H', 'T', 'T', 'T', 'H'), ('H', 'T', 'T', 'T', 'T'),
('T', 'H', 'H', 'H', 'H'), ('T', 'H', 'H', 'H', 'T'), ('T', 'H', 'H', 'T', 'H'), ('T', 'H', 'H', 'T', 'T'),
('T', 'H', 'T', 'H', 'H'), ('T', 'H', 'T', 'H', 'T'), ('T', 'H', 'T', 'T', 'H'), ('T', 'H', 'T', 'T', 'T'),
('T', 'T', 'H', 'H', 'H'), ('T', 'T', 'H', 'H', 'T'), ('T', 'T', 'H', 'T', 'H'), ('T', 'T', 'H', 'T', 'T'),
('T', 'T', 'T', 'H', 'H'), ('T', 'T', 'T', 'H', 'T'), ('T', 'T', 'T', 'T', 'H'), ('T', 'T', 'T', 'T', 'T')]
However, I want the printout to show only the results with no more than three H's or T's in a row. Like this:
[('H', 'H', 'H', 'T', 'H'), ('H', 'H', 'H', 'T', 'T'), ('H', 'H', 'T', 'H', 'H'), ('H', 'H', 'T', 'H', 'T'),
('H', 'H', 'T', 'T', 'H'), ('H', 'H', 'T', 'T', 'T'), ('H', 'T', 'H', 'H', 'H'), ('H', 'T', 'H', 'H', 'T'),
('H', 'T', 'H', 'T', 'H'), ('H', 'T', 'H', 'T', 'T'), ('H', 'T', 'T', 'H', 'H'), ('H', 'T', 'T', 'H', 'T'),
('H', 'T', 'T', 'T', 'H'), ('T', 'H', 'H', 'H', 'T'), ('T', 'H', 'H', 'T', 'H'), ('T', 'H', 'H', 'T', 'T'),
('T', 'H', 'T', 'H', 'H'), ('T', 'H', 'T', 'H', 'T'), ('T', 'H', 'T', 'T', 'H'), ('T', 'H', 'T', 'T', 'T'),
('T', 'T', 'H', 'H', 'H'), ('T', 'T', 'H', 'H', 'T'), ('T', 'T', 'H', 'T', 'H'), ('T', 'T', 'H', 'T', 'T'),
('T', 'T', 'T', 'H', 'H'), ('T', 'T', 'T', 'H', 'T')]
*Obviously this isn't the only method to implement this, it just happen to be the one I came across since I'm still new at programming.
~Update~
dabljues suggested the following:
In [38]: all_combinations = itertools.product('HT', repeat=5)
...: not_more_than_3_combinations = []
...: for combination in all_combinations:
...: for _, group in itertools.groupby(combination):
...: if sum(1 for _ in group) <= 3:
...: not_more_than_3_combinations.append(combination)
...: break
...: print(not_more_than_3_combinations)
Not sure how to implement it yet (again, I'm a beginner) but it looks the most promising.
Thanks.