Previous Post

Next Post

In the previous post, I commented about by experience at pre-round at Google Code Jam.

The exercise B was Lawnmover:

https://code.google.com/codejam/contest/2270488/dashboard#s=p1

Problem

Alice and Bob have a lawn in front of their house, shaped like an N metre by M metre rectangle. Each year, they try to cut the lawn in some interesting pattern. They used to do their cutting with shears, which was very time-consuming; but now they have a new automatic lawnmower with multiple settings, and they want to try it out.

The new lawnmower has a height setting – you can set it to any height h between 1 and 100 millimetres, and it will cut all the grass higher than h it encounters to height h. You run it by entering the lawn at any part of the edge of the lawn; then the lawnmower goes in a straight line, perpendicular to the edge of the lawn it entered, cutting grass in a swath 1m wide, until it exits the lawn on the other side. The lawnmower’s height can be set only when it is not on the lawn.

Alice and Bob have a number of various patterns of grass that they could have on their lawn. For each of those, they want to know whether it’s possible to cut the grass into this pattern with their new lawnmower. Each pattern is described by specifying the height of the grass on each 1m x 1m square of the lawn.

The grass is initially 100mm high on the whole lawn.

I solved it, see:

https://github.com/ajlopez/TddOnTheRocks/tree/master/Lawnmover

As usual, the commit history by test:

https://github.com/ajlopez/TddOnTheRocks/commits/master/Lawnmover

I could solve the first small set provided by Google. But I failed with the big set, because my algorithm was not efficient enough. The big set should be solved in 8 minutes, and if you cannot, there is no other chance. My initial algorithm (see comments) attacked the largest number in cells. My second algorithm switched to start with the minimal numbers (my first guess was that both algorithms had the same time, but I was wrong). I learnt: some problems in Google are hard to test by hand, we don’t have the use case (input and expected output), that is the main difficult.

More katas are comming.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

### Like this:

Like Loading...

*Related*

[…] Previous Post Next Post […]

Pingback by TDD Kata (3): Tic-Tac-Toe-Tomek | Angel "Java" Lopez on Blog — May 10, 2013 @ 9:53 am

[…] TDD Kata (4): Lawnmover […]

Pingback by TDD Kata (4): Lawnmover | MVPs de LATAM — May 16, 2013 @ 5:22 pm

[…] Previous Post […]

Pingback by TDD Kata (5): Tree Search | Angel "Java" Lopez on Blog — June 30, 2013 @ 3:29 pm

[…] Previous Post […]

Pingback by TDD Kata (5): Tree Search | MVPs de LATAM — July 2, 2013 @ 3:04 pm