Problem Statement (For a more detailed description (including pictures) please visit the link):

A series of seven units in length has red blocks with a minimum

Length of three units placed on it, leaving two red blocks

(which may be different lengths) are separated by at least

a gray square. There are exactly seventeen ways to do this.

In how many ways can a fifty-unit long row be filled?

Code:

```
from scipy import special
import time
import itertools
def partitionfunc(n,k,l=3):
'''n is the integer to partition, k is the
length of partitions, l is the min partition element size'''
if k < 1:
return 0
if k == 1:
if n >= l:
yield (n,)
return 0
for i in range(l,n+1):
for result in partitionfunc(n-i,k-1,i):
yield (i,)+result
def valid_partitions(p):
total = p
count = 0 #Max. number of tiles that can be placed on a row.
while True:
count += 1
total -=3
if total <= 3:
break
total-=1
'''Find all the valid partitions with length (1,count) that can be placed on the row with length p'''
data = ()
for k in range(1,count+1):
min_part = k*3
for n in range(min_part,p+1):
Allowed = ()
LIST = list(partitionfunc(n,k))
for b in LIST:
if sum(b) + (len(b)-1) <= p:
Allowed.append(b)
data+= Allowed
return data
def count_permutations(array):
'''Counts how many possible permutations are there for the particular partition'''
get_unique_elements = set(array)
total_length = len(array)
lengths = (array.count(x) for x in get_unique_elements)
answer = 1
for b in lengths:
answer*= special.comb(total_length, b)
total_length-= b
return answer
def calculate_ways(m,n):
return special.comb(n-m+1,n-2*m+1)
def final(w):
total_variations = 0
data = valid_partitions(w)
for q in data:
m = len(q)
remain = w - sum(q) - m + 1
n = 2*m -1 + remain
total_variations+= calculate_ways(m,n)*count_permutations(q)
return int(total_variations+1)
if __name__ == '__main__':
start = time.time()
print('Answer: {}'.format(final(50)))
print(time.time()-start)
```

I explain the reasons for the code with an example from Euler:

**Step 1**

First, we find the maximum number of tiles that can be placed in the 7-length row. At most 2 tiles can be placed, so the number is 2 (name this number **number**).

Then we go through (1,**number**) and find all valid partitions of number seven:

In our case:

1 Tile: (3), (4), (5), (6), (7) (The reason why we omit (1) and (2) is that the minimum length of the red tile is 3).

2 tiles: (3, 3) (Note that while (3,4) is also a partition of 7, in our case it does not work because it states that there must be a partition *at least 1 tile gap* between two red tiles.

**Step 2.**

For each partition obtained in step 1, we calculate the number of ways that the partition can be placed in the row.

For example:

(5) Represents 1 tile of length 5. There are 3 ways to place such a tile.

(3,3) Represents 2 tiles, both of length 3. There is a way to place them in the row.

If we add the numbers we get 16. We have to add 1 because (I'm curious why) the line that does not contain *any* Red tiles are also a valid case. The final answer 17.

I believe that exists *a lot of* of things that can be improved. I am pleased about suggestions for improvement!

S. The problem I would like to highlight is the variable / function names. I believe there is a way to get it through *More* descriptive / clear then they are now.