#389 · Recursion and backtracking · Advanced · 140 XP · about 30 min
You have some matchsticks, and sticks[i] is the length of stick i. Write can_make_square(sticks) that returns True if you can lay out all of them as the four sides of a square, and False otherwise.
Every stick must be used exactly once. Several sticks can be laid end to end along one side, but no stick may be broken or shared between two sides.
can_make_square([1, 1, 2, 2, 2]) # returns True can_make_square([5, 1, 4, 2, 3, 5]) # returns True can_make_square([3, 3, 3, 3, 4]) # returns False
In the second example the sides are 5, 5, 1 + 4 and 2 + 3.
0 <= len(sticks) <= 151 <= sticks[i] <= 10**8A square needs four sides of positive length, so fewer than four sticks can never make one.
First solves only, ranked by stars and then time.