bennettandy / MarsRobotExample

Code exercise to process robot movement instructions and determine final positions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MarsRobotExample

Time limited Code exercise to process robot movement instructions and determine final positions

No UI yet for this test, Components can be tested in AndroidStudio. Right click on app/src/test and select Run tests. The tests can also be run via gradle: gradle testDebug and the results will be found in the following location: MarsRobotExample/app/build/reports/tests/testDebugUnitTest/index.html

The core class is RobotPositionCommandProcessor.

We start with a list of command lines, use line 1 to define the planet bounds and drop this from the list. We then call a tail recursive function that consumes the list 3 lines at a time until all the commands are processed. During the recursion we build a list of results that we .joinToString() to produce the expected output.

fun processRobotCommandString(inputString: String): String {
val lines = inputString.lines()
val bounds = getBoundsInteractor(lines.first())

        return processRobotCommand(bounds = bounds, commands = lines.drop(1), results = emptyList())
            .joinToString(separator = "\n")
    }

Robot command processor class: Again using tail recursion to process each command, followed by a bounds check function to detect loss of robot. Loss of robot prevents further movements.

RobotCommandProcessor.kt

test_screenshot

Test Data and Expected Result String:

private val TestInput = """
            53
            11E 
            RFRFRFRF
            
            32N 
            FRRFLLFFRRFLL
            
            03W 
            LLFFFLFLFL

        """.trimIndent()

        private val ExpectedOutput = """
            11E
            33NLOST
            33NLOST
        """.trimIndent()

See: TestRobotPositionCommandProcessor.kt

About

Code exercise to process robot movement instructions and determine final positions


Languages

Language:Kotlin 100.0%