Hello party people! 🙂

Today I was wondering whether I have forgotten to code in Python after 4 weeks of coding marathon in VBA and I have decided to try a small portion of the weekly challenge from CodeForces!

So, the second part of the challenge looked really easy, even for me, thus I have decided to give it a try. The original challenge is here. This is how it looks like:

Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built *n* towers in a row. The *i*-th tower is made of*h*_{i} identical blocks. For clarification see picture for the first sample.

Limak will repeat the following operation till everything is destroyed.

Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time.

Limak is ready to start. You task is to count how many operations will it take him to destroy all towers.

The first line contains single integer *n* (1 ≤ *n* ≤ 10^{5}).

The second line contains *n* space-separated integers *h*_{1}, *h*_{2}, …, *h*_{n} (1 ≤ *h*_{i} ≤ 10^{9}) — sizes of towers.

Print the number of operations needed to destroy all towers.

1 2 |
6 2 1 4 6 2 2 |

1 |
3 |

1 2 |
7 3 3 3 1 3 3 3 |

1 |
2 |

The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color.

So, before going to the code, which was even not re-edited once it was working, what did I do? First, I have decided that my data structure would be a list of list and the way to resolve the puzzle would be to make a function, removing the blocks, returning a new structure. The function was going to be ran until the structure existed (e.g. the list of list was with a length that would return a positive boolean 😀 ). True story. The rest was just some time to code it and some time to remember how to get element from a list in Python and plenty of similar searches. Once the structure achieved what I wanted, I have decided to publish it and to watch TV series 🙂

So, here comes the code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
def remove_blocks(lst_towers): for i in range(1, len(lst_towers)-1): for k in range(0, len(lst_towers[i])): b_remove = False #check height if k == len(lst_towers[i])-1: b_remove = True #check left and right if k > len(lst_towers[i-1])-1 or k> len(lst_towers[i+1])-1: b_remove = True if b_remove: lst_towers[i][k] = 0 lst_towers[0] = [0] lst_towers[-1] = [0] lst_new = [] for k in range(0,len(lst_towers)-1): temp = [] temp = [x for x in lst_towers[k] if x != 0] if len(temp): lst_new.append(temp) return lst_new lines = int(input()) lst_first_line = list(map(int, input().split())) lst_towers = [] for i in range(0,lines): lst_temp = list() for z in range(0, lst_first_line[i]): lst_temp.append(1) lst_towers.append(lst_temp) counter = 0 while len(lst_towers): lst_towers = remove_blocks(lst_towers) counter += 1 print(counter) |

Enjoy it also in GitHub.

😀