Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NASA rover challenge #50

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
141 changes: 141 additions & 0 deletions maribel/public/Class25_Exercises_NASARoverChallenge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
## NASA Rover Challenge

![img](../assets/clase25/9286f625-27c8-4c25-88d6-425510bb04e8.jpg)

**Context**
The purpose of this test is to enable you to demonstrate your proficiency in solving problems using software engineering tools and processes. Read the specification below and produce a solution.

Your solution should be in the form of completed code. The problem specified below requires a solution that receives input, does some processing and then returns some output.

**Specification**
A robotic rover is to be landed by NASA on a plateau on Mars. This plateau, which is curiously square, must be navigated by the rover so that its on board cameras can get a complete view of the surrounding terrain to send back to Earth.

A rover's position is represented by a combination of an x and y coordinates and a letter representing one of the four cardinal compass points.

The plateau is divided up into a grid to simplify navigation.

An example position might be 0, 0, N, which means the rover is in the bottom left corner and facing North. In order to control a rover, NASA sends a simple string of letters. The possible letters are 'L', 'R' and 'M'.

'L' and 'R' makes the rover spin 90 degrees left or right respectively, without moving from its current spot. 'M' means move forward one grid point, and maintain the same heading.

Assume that the square directly North from (x, y) is (x, y+1).

**Input**
The first parameter is the size of the square (the lower-left coordinates are assumed to be 0,0).

The rest of parameters is information pertaining to the rover that has been deployed.

The second argument gives the rover's position, and the third is a series of instructions telling the rover how to explore the plateau. The position is made up of two integers and a letter, corresponding to the x and y coordinates and the rover's orientation.

**Output**
The output should be the final coordinates and heading.

**Example**
function setup(5, “1 2 N”, [“L”, “M”, “L”, “M”, “L”, “M”, “L”, “M”, “M”])
Expected Output: “1 3 N”

```javascript
var machine = {
squareSize: 5,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No pondría valores por defecto...

position: {
x: 0,
y: 0
},
instructions: [],
state: "N",
transitions: {
"N" : {
M: function(){
this.changePositionTo(this.position.x, this.position.y + 1);
},
L: function(){
this.changeStateTo("W");
},
R: function(){
this.changeStateTo("E");
}
},
"E" : {
M: function(){
this.changePositionTo(this.position.x + 1, this.position.y);
},
L: function(){
this.changeStateTo("N");
},
R: function(){
this.changeStateTo("S");
}
},
"S": {
M: function(){
this.changePositionTo(this.position.x, this.position.y - 1);
},
L: function(){
this.changeStateTo("E");
},
R: function(){
this.changeStateTo("W");
}
},
"W": {
M: function(){
this.changePositionTo(this.position.x - 1, this.position.y);
},
L: function(){
this.changeStateTo("S");
},
R: function(){
this.changeStateTo("N");
}
}
},

dispatch(actionName) {
const action = this.transitions[this.state][actionName];

if (action) {
action.apply(machine);
}
},

changeStateTo(newState) {
this.state = newState;
},

changePositionTo(newX, newY) {
var newXIsValid = (newX >= 0 && newX < this.squareSize);
var newYIsValid = (newY >= 0 && newY < this.squareSize);
if (newXIsValid && newYIsValid) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Muy bien la gestión de errores! 🌟

this.position.x = newX;
this.position.y = newY;
}
},

executeInstructions() {
this.instructions.forEach(instr => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No se gestionan los posibles errores en la secuencia. P.j.. que se salga del mapa o similares

machine.dispatch(instr);
});

var finalResult = [this.position.x, this.position.y, this.state].join(" ");
console.log(finalResult);
}
}

function setup(squareSize, initialPosition, instructions) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Muy buen enfoque!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aunque igual sería interesante hacer una validación de los parámetros.. para evitar sorpresas al ser tan extraña la forma de pasarnos los argumentos

machine.squareSize = squareSize;

initialPosition = initialPosition.split(" ");
machine.position.x = parseInt(initialPosition[0]);
machine.position.y = parseInt(initialPosition[1]);
machine.state = initialPosition[2];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Igual heading sería más correcto que state como nombre 👍


machine.instructions = instructions;
}

function start() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bravo! Muy bien conectado y coherente con respecto a machine 👌

machine.executeInstructions();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sería interesante validar el estado d ela maquina antes de inicializar la marcha...

}

setup(5, "1 2 N", ["L", "M", "L", "M", "L", "M", "L", "M", "M"]);
start();
```