Question: TestDome - Grasshopper

15 February 2018

Views: 21

/* Question: TestDome - Grasshopper Solution by Arash Partow 2014

Imagine a row of leaves (numbered 1 to N) and a grasshopper feeding on leaf X. After he eats leaf X, he will move either two leaves to the left or two leaves to the right, skipping the leaves he ate. The grasshopper will always move to an existing leaf.

Implement a class that models the grasshopper's behaviour and can report its location at any time.

For example, Grasshopper(5, 2) creates a scenario with a row of 5 leaves (numbered 1 to 5) and a grasshopper feeding on leaf 2 (1 2* 3 4 5). A call to eatAndHopRight() marks the current leaf (leaf 2) as eaten and moves the grasshopper two leaves to the right to leaf 4 (1 3 4* 5). A call to eatAndHopLeft() marks the current leaf (leaf 4) as eaten and moves the grasshopper two leaves to the left to leaf 1 (1* 3 5), skipping leaves marked as eaten.*/