Skip to content

Growing Plant

Caring for a plant can be hard work, but since you tend to it regularly, you have a plant that grows consistently. Each day, its height increases by a fixed amount represented by the integer upSpeed. But due to lack of sunlight, the plant decreases in height every night, by an amount represented by downSpeed.

Since you grew the plant from a seed, it started at height 0 initially. Given an integer desiredHeight, your task is to find how many days it'll take for the plant to reach this height.

Example

For upSpeed = 100, downSpeed = 10, and desiredHeight = 910, the output should be

growing_plant(upSpeed, downSpeed, desiredHeight) = 10
#DayNight
110090
2190180
3280270
4370360
5460450
6550540
7640630
8730720
9820810
10910900

The plant first reaches a height of 910 on day 10.

Solution

py
def growing_plant(up_speed, down_speed, desired_height):
    height = 0
    day = 0
    while height < desired_height:
        day += 1
        height += up_speed
        if height >= desired_height:
            break
        height -= down_speed
    return day


print(growing_plant(6, 5, 10))
js
function growingPlant(upSpeed, downSpeed, desiredHeight) {
  let days = 0;
  let height = 0;
  while (height < desiredHeight) {
    days++;
    height += upSpeed;
    if (height >= desiredHeight) {
      break;
    }
    height -= downSpeed;
  }
  return days;
}

console.log(growingPlant(6, 5, 10));

my thoughts are neither my employer's nor my wife's