-
Python Bruteforces
Hi guys,
Right now CheckiO has around 250,000 shared solutions for different missions. That is a pretty decent amount and I think we can make something out of it.
Idea - pick a module and see how this it’s being used in CheckiO solutions. I chose itertools, which has about 5,000 solutions, or about 2% of total. Let me show you the using-ratings of how often each function was used.
product - 1351
count - 1077
permutations - 915
chain - 822
repeat - 648
groupby - 210
zip_longest - 207
cycle - 141
starmap - 125
combinations_with_replacement - 116
takewhile - 76
izip - 71
accumulate - 68
islice - 62
compress - 51
tee - 38
izip_longest - 36
dropwhile - 26
imap - 22
ifilter - 15
filterfalse - 15
ifilterfalse - 1
Well, I’m not surprised that the top of the most popular functions here are from the generators family. With the exception of combinations_with_replacement, probably because the function is too long to use :)
Let’s quickly go through 4 functions from the generators family. For each function, I found solutions on CheckiO that are using those functions.
I’ll use list function just to show you the results right away. In real solutions you probably don’t need to use list function after any of itertools function.
In: list(itertools.combinations([1,2,3], 2)) Out: [(1, 2), (1, 3), (2, 3)]
Gives you possible combinations of elements
In: list(itertools.combinations([1,2,3], 3)) Out: [(1, 2, 3)] In: list(itertools.combinations([1,2,3,4], 3)) Out: [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
Solutions with combinations: The Longest Palindromic by gyahun_dash, Count Inversions by gyahun_dash
In: list(itertools.combinations_with_replacement([1,2,3], 2)) Out: [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)]
combinations_with_replacement looks the same as combinations but one element can be used more than once
In: list(itertools.combinations_with_replacement([1,2,3], 3)) Out: [(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 2), (1, 2, 3), (1, 3, 3), (2, 2, 2), (2, 2, 3), (2, 3, 3), (3, 3, 3)] In: list(itertools.combinations_with_replacement([1,2], 2)) Out: [(1, 1), (1, 2), (2, 2)]
Solution with combinations_with_replacement: Probably Dice by zero_loss
In : list(itertools.product([1,2], repeat=2)) Out: [(1, 1), (1, 2), (2, 1), (2, 2)]
product works the same as combinations_with_replacement but orders of those elements matters. You can also use more than once iter here
In: list(itertools.product([1,2], [1,2])) Out: [(1, 1), (1, 2), (2, 1), (2, 2)] In: list(itertools.product([1,2], ['A', 'B'])) Out: [(1, 'A'), (1, 'B'), (2, 'A'), (2, 'B')]
You can think about it as it is nested FORs.
In: list((x, y) for x in [1,2] for y in ['A', 'B']) Out: [(1, 'A'), (1, 'B'), (2, 'A'), (2, 'B')]
Solutions with product: Break Rings by veky, Life Counter by gyahun_dash (I start thinking that this guy starts his every solution with “import itertools” for just in case :) )
In: list(itertools.permutations([1,2])) Out: [(1, 2), (2, 1)]
permutations is like product but without replacement, so each element can be used only once
In: list(itertools.permutations([1,2, 3], 2)) Out: [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
Solution with permutations: The end of other by Arkadiusz
Do you want to know more about how Python batteries are used in CheckiO solutions?
Welcome to CheckiO - games for coders where you can improve your codings skills.
The main idea behind these games is to give you the opportunity to learn by exchanging experience with the rest of the community. Every day we are trying to find interesting solutions for you to help you become a better coder.
Join the Game