• mission idea "Spacecraft"

 

I post mission idea "Spacecraft". I would like to get some feedback.

description : You have to plan unmanned spacecraft touch down on the asteroid and return to Earth.

You are given propellants, batteries and position of the asteroid. Spacecraft consume 1 propellant by each acceleration. Batteries mean time(step) limit. Earth's position is always 0 and spacecraft start from Earth.

At each step, spacecraft can accelerate positive( or negative ) direction. if spacecraft position is equal to the asteroid or Earth, he touch down or return. (When last of each step, evaluate spacecraft position)

You should return the plan by sequence of '+', '-' and blank.

  • '+' : acceleration positive direction
  • '-' : acceleration negative direction
  • blank : no acceleration

specification : spacecraft(propellants, batteries, asteroid_position) == plan

ex : spacecraft(3, 5, 2) == "+ -- "

test :

EARTH = 0
THRUSTER = {' ': 0, '-': -1, '+': 1}


def spacecraft(propellants, batteries, asteroid):

    return my_plan

if __name__ == '__main__':

    def checker(propellants, batteries, asteroid, func):
        plan = func(propellants, batteries, asteroid)
        vec = 0
        po = EARTH
        touchdown = False
        for t in plan:
            if not batteries:
                print('battery is dead.')
                return False

            if propellants and t != ' ':
                propellants -= 1
                vec += THRUSTER[t]

            po += vec
            batteries -= 1

            if po == asteroid and not touchdown:
                print('touch down!')
                touchdown = True
            elif po == EARTH and touchdown:
                print('return my earth :)\n')
                return True

        print('mission incomplete.')
        return False

    assert checker(3, 5, 2, spacecraft) is True, 'basic'
    assert checker(3, 7, 3, spacecraft) is True, 'position 3'
    assert checker(7, 8, 6, spacecraft) is True, 'more acceleration'
    assert checker(6, 11, 8, spacecraft) is True, 'position 8'
    assert checker(4, 6, -3, spacecraft) is True, 'negative position'