a robot is programmed to move forward 'F' meters and backwards again say 'B' meters in a straight line .the robt covers 1 meter in T units of time on robot path there is a ditch at a distance FD from intial oposition in forward direction as well as a ditch at a distance 'BD' from intial postion in backward direction this forward and backward movements is performed repeatedly by the robot

your task is to calculate amount of time taken before the robot falls either ditch if at all it falls in a ditch

Dani AI

Generated

As suggested, treat this as arithmetic first; as said, a small simulation is useful for testing corner cases. Below is a concise, exact approach and a tiny Python routine (per ) to compute the time in O(1) rather than iterating.

Key idea (notation): start at x=0. After n full cycles (forward F then back B) the start-of-forward position is s_n = n*(F - B). Check the forward move of each cycle first, then the backward move.

Rules (apply in order)

  • If FD <= F the robot falls on the very first forward move: time = FD * T.
  • If B >= F + BD the robot falls during the first backward move: time = (2F + BD) T.
  • Let D = F - B.
    • If D > 0 (net forward drift) the earliest forward hit is the smallest integer n >= 0 with nD + F >= FD. Compute n = ceil((FD - F) / D). Time = (FD + 2nB) T.
    • If D == 0 the robot oscillates between 0 and F; if FD > F and BD > 0 it never reaches either ditch.
    • If D < 0 (net backward drift) and the first-cycle checks failed, the backward ditch will eventually be hit. Let k = ceil(BD / (B - F)) (this is n+1). Time = (BD + 2Fk) * T.

A compact Python implementation:

import math

def time_to_fall(F, B, FD, BD, T):
    if T <= 0: raise ValueError("T must be > 0")
    if FD <= F:
        return FD * T
    if B >= F + BD:
        return (2*F + BD) * T
    D = F - B
    if D > 0:
        n = math.ceil((FD - F) / D)
        return (FD + 2*n*B) * T
    if D == 0:
        return None   # oscillates between 0 and F; never reaches ditches
    # D < 0
    k = math.ceil(BD / (B - F))
    return (BD + 2*F*k) * T

Notes: validate inputs (nonnegative distances, T>0). Use integer-aware ceilings for exact integer data to avoid floating errors. Use a short simulation to verify special cases as recommended.

Recommended Answers

All 4 Replies

Your task is to show some effort, show us your code and then ask us a question we can answer.

This is not actually a programming question (or rather, to solve this by simulating the behaviour of the robot is a bad way to solve this). This is a simple maths question. Solve it on paper first.

This is not actually a programming question (or rather, to solve this by simulating the behaviour of the robot is a bad way to solve this). This is a simple maths question. Solve it on paper first.

Actually, this is a good program to tackle by computer simulation ... as it makes you think about the many cases ... and assumptions one might make about the given initial conditions.

For example ...

  • can either ditch be reached by one forward move
    or one forward move followed by the backward move?
    If not, what constraint does this set upon all the initial givens?

  • if either ditch not reached in 'first moves' ...
    what does this imply about the meaning of (F-B) vs
    distance from inital spot to either ditch?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.