From 8346353fde4d9c153c20b1783ae97dbacd551e2f Mon Sep 17 00:00:00 2001 From: minicatsCB <19836547+minicatsCB@users.noreply.github.com> Date: Thu, 20 Dec 2018 13:29:17 +0100 Subject: [PATCH 1/9] Add class 25 exercises: NASA rover challenge --- .../Class25_Exercises_NASARoverChallenge.md | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 maribel/public/Class25_Exercises_NASARoverChallenge.md diff --git a/maribel/public/Class25_Exercises_NASARoverChallenge.md b/maribel/public/Class25_Exercises_NASARoverChallenge.md new file mode 100644 index 0000000..320e207 --- /dev/null +++ b/maribel/public/Class25_Exercises_NASARoverChallenge.md @@ -0,0 +1,119 @@ +## 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 = { + position: { + x: 1, + y: 2 + }, + state: "north", + transitions: { + "north" : { + move: function(){ + this.changePositionTo(this.position.x, this.position.y + 1); + }, + left: function(){ + this.changeStateTo("west"); + }, + right: function(){ + this.changeStateTo("east"); + } + }, + "east" : { + move: function(){ + this.changePositionTo(this.position.x + 1, this.position.y); + }, + left: function(){ + this.changeStateTo("north"); + }, + right: function(){ + this.changeStateTo("south"); + } + }, + "south": { + move: function(){ + this.changePositionTo(this.position.x, this.position.y - 1); + }, + left: function(){ + this.changeStateTo("east"); + }, + right: function(){ + this.changeStateTo("west"); + } + }, + "west": { + move: function(){ + this.changePositionTo(this.position.x - 1, this.position.y); + }, + left: function(){ + this.changeStateTo("south"); + }, + right: function(){ + this.changeStateTo("north"); + } + } + }, + + dispatch(actionName, ...payload) { + const actions = this.transitions[this.state]; + const action = this.transitions[this.state][actionName]; + + if (action) { + action.apply(machine, ...payload); + } + }, + + changeStateTo(newState) { + this.state = newState; + console.log("State changed to: ", this.state); + }, + + changePositionTo(newX, newY) { + this.position.x = newX; + this.position.y = newY; + console.log("Position changed to: ", this.position); + } +} + +function setup(squareSize, initialPosition, instructions) { + instructions.forEach(instr => { + machine.dispatch(instr); + }); +} + +setup(5, "1 2 N", ["left", "move", "left", "move", "left", "move", "left", "move", "move"]); +``` From 4b2e8504a055ffe826bc5d60bcc13b9e1b3af5aa Mon Sep 17 00:00:00 2001 From: minicatsCB <19836547+minicatsCB@users.noreply.github.com> Date: Thu, 20 Dec 2018 14:04:35 +0100 Subject: [PATCH 2/9] Remove unused code --- maribel/public/Class25_Exercises_NASARoverChallenge.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/maribel/public/Class25_Exercises_NASARoverChallenge.md b/maribel/public/Class25_Exercises_NASARoverChallenge.md index 320e207..97c999d 100644 --- a/maribel/public/Class25_Exercises_NASARoverChallenge.md +++ b/maribel/public/Class25_Exercises_NASARoverChallenge.md @@ -88,12 +88,11 @@ var machine = { } }, - dispatch(actionName, ...payload) { - const actions = this.transitions[this.state]; + dispatch(actionName) { const action = this.transitions[this.state][actionName]; if (action) { - action.apply(machine, ...payload); + action.apply(machine); } }, From 475738e6de3527f66b5c7438ef3427d0a53f98ca Mon Sep 17 00:00:00 2001 From: minicatsCB <19836547+minicatsCB@users.noreply.github.com> Date: Thu, 20 Dec 2018 14:16:07 +0100 Subject: [PATCH 3/9] Separate setup and start events --- .../Class25_Exercises_NASARoverChallenge.md | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/maribel/public/Class25_Exercises_NASARoverChallenge.md b/maribel/public/Class25_Exercises_NASARoverChallenge.md index 97c999d..ca53fc8 100644 --- a/maribel/public/Class25_Exercises_NASARoverChallenge.md +++ b/maribel/public/Class25_Exercises_NASARoverChallenge.md @@ -36,10 +36,12 @@ Expected Output: “1 3 N” ```javascript var machine = { + squareSize: 5, position: { x: 1, y: 2 }, + instructions: [], state: "north", transitions: { "north" : { @@ -105,14 +107,30 @@ var machine = { this.position.x = newX; this.position.y = newY; console.log("Position changed to: ", this.position); + }, + + executeInstructions() { + this.instructions.forEach(instr => { + machine.dispatch(instr); + }); } } function setup(squareSize, initialPosition, instructions) { - instructions.forEach(instr => { - machine.dispatch(instr); - }); + machine.squareSize = squareSize; + + initialPosition = initialPosition.split(" "); + machine.position.x = parseInt(initialPosition[0]); + machine.position.y = parseInt(initialPosition[1]); + machine.state = initialPosition[2]; + + machine.instructions = instructions; +} + +function start() { + machine.executeInstructions(); } -setup(5, "1 2 N", ["left", "move", "left", "move", "left", "move", "left", "move", "move"]); +setup(5, "1 2 north", ["left", "move", "left", "move", "left", "move", "left", "move", "move"]); +start(); ``` From e1bfce672a560cc62dd7c4f18ee36b9f81063753 Mon Sep 17 00:00:00 2001 From: minicatsCB <19836547+minicatsCB@users.noreply.github.com> Date: Thu, 20 Dec 2018 23:37:54 +0100 Subject: [PATCH 4/9] Change state and actions identifiers to those specified in the exercise --- .../Class25_Exercises_NASARoverChallenge.md | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/maribel/public/Class25_Exercises_NASARoverChallenge.md b/maribel/public/Class25_Exercises_NASARoverChallenge.md index ca53fc8..65cb12d 100644 --- a/maribel/public/Class25_Exercises_NASARoverChallenge.md +++ b/maribel/public/Class25_Exercises_NASARoverChallenge.md @@ -42,50 +42,50 @@ var machine = { y: 2 }, instructions: [], - state: "north", + state: "N", transitions: { - "north" : { - move: function(){ + "N" : { + M: function(){ this.changePositionTo(this.position.x, this.position.y + 1); }, - left: function(){ - this.changeStateTo("west"); + L: function(){ + this.changeStateTo("W"); }, - right: function(){ - this.changeStateTo("east"); + R: function(){ + this.changeStateTo("E"); } }, - "east" : { - move: function(){ + "E" : { + M: function(){ this.changePositionTo(this.position.x + 1, this.position.y); }, - left: function(){ - this.changeStateTo("north"); + L: function(){ + this.changeStateTo("N"); }, - right: function(){ - this.changeStateTo("south"); + R: function(){ + this.changeStateTo("S"); } }, - "south": { - move: function(){ + "S": { + M: function(){ this.changePositionTo(this.position.x, this.position.y - 1); }, - left: function(){ - this.changeStateTo("east"); + L: function(){ + this.changeStateTo("E"); }, - right: function(){ - this.changeStateTo("west"); + R: function(){ + this.changeStateTo("W"); } }, - "west": { - move: function(){ + "W": { + M: function(){ this.changePositionTo(this.position.x - 1, this.position.y); }, - left: function(){ - this.changeStateTo("south"); + L: function(){ + this.changeStateTo("S"); }, - right: function(){ - this.changeStateTo("north"); + R: function(){ + this.changeStateTo("N"); } } }, @@ -131,6 +131,6 @@ function start() { machine.executeInstructions(); } -setup(5, "1 2 north", ["left", "move", "left", "move", "left", "move", "left", "move", "move"]); +setup(5, "1 2 N", ["L", "M", "L", "M", "L", "M", "L", "M", "M"]); start(); ``` From 35122fef4f84d089bf33c2a0d29e5d01fa5e79aa Mon Sep 17 00:00:00 2001 From: minicatsCB <19836547+minicatsCB@users.noreply.github.com> Date: Thu, 20 Dec 2018 23:46:46 +0100 Subject: [PATCH 5/9] Check if each new position is valid --- maribel/public/Class25_Exercises_NASARoverChallenge.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/maribel/public/Class25_Exercises_NASARoverChallenge.md b/maribel/public/Class25_Exercises_NASARoverChallenge.md index 65cb12d..614f2a0 100644 --- a/maribel/public/Class25_Exercises_NASARoverChallenge.md +++ b/maribel/public/Class25_Exercises_NASARoverChallenge.md @@ -104,9 +104,13 @@ var machine = { }, changePositionTo(newX, newY) { - this.position.x = newX; - this.position.y = newY; - console.log("Position changed to: ", this.position); + var newXIsValid = (newX >= 0 && newX < this.squareSize); + var newYIsValid = (newY >= 0 && newY < this.squareSize); + if (newXIsValid && newYIsValid) { + this.position.x = newX; + this.position.y = newY; + console.log("Position changed to: ", this.position); + } }, executeInstructions() { From 1a9b2d75d816de449802a6601be788ef679c166a Mon Sep 17 00:00:00 2001 From: minicatsCB <19836547+minicatsCB@users.noreply.github.com> Date: Thu, 20 Dec 2018 23:47:55 +0100 Subject: [PATCH 6/9] Set default position to (0,0) --- maribel/public/Class25_Exercises_NASARoverChallenge.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maribel/public/Class25_Exercises_NASARoverChallenge.md b/maribel/public/Class25_Exercises_NASARoverChallenge.md index 614f2a0..115b881 100644 --- a/maribel/public/Class25_Exercises_NASARoverChallenge.md +++ b/maribel/public/Class25_Exercises_NASARoverChallenge.md @@ -38,8 +38,8 @@ Expected Output: “1 3 N” var machine = { squareSize: 5, position: { - x: 1, - y: 2 + x: 0, + y: 0 }, instructions: [], state: "N", From 67c297d913d2e05459b0565e3549ac71ab1e0178 Mon Sep 17 00:00:00 2001 From: minicatsCB <19836547+minicatsCB@users.noreply.github.com> Date: Fri, 21 Dec 2018 14:20:40 +0100 Subject: [PATCH 7/9] Modify logs to fit the required output format --- maribel/public/Class25_Exercises_NASARoverChallenge.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/maribel/public/Class25_Exercises_NASARoverChallenge.md b/maribel/public/Class25_Exercises_NASARoverChallenge.md index 115b881..a1af1e2 100644 --- a/maribel/public/Class25_Exercises_NASARoverChallenge.md +++ b/maribel/public/Class25_Exercises_NASARoverChallenge.md @@ -100,7 +100,6 @@ var machine = { changeStateTo(newState) { this.state = newState; - console.log("State changed to: ", this.state); }, changePositionTo(newX, newY) { @@ -109,7 +108,6 @@ var machine = { if (newXIsValid && newYIsValid) { this.position.x = newX; this.position.y = newY; - console.log("Position changed to: ", this.position); } }, @@ -117,6 +115,9 @@ var machine = { this.instructions.forEach(instr => { machine.dispatch(instr); }); + + var finalResult = [this.position.x, this.position.y, this.state].join(" "); + console.log(finalResult); } } From 16e4485169184a4c6a569ad04ebee27982cece7d Mon Sep 17 00:00:00 2001 From: minicatsCB <19836547+minicatsCB@users.noreply.github.com> Date: Tue, 22 Jan 2019 23:16:49 +0100 Subject: [PATCH 8/9] Add class 35 exercises: Regular expresssions --- Class35_Exercises_Regex.md | 59 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Class35_Exercises_Regex.md diff --git a/Class35_Exercises_Regex.md b/Class35_Exercises_Regex.md new file mode 100644 index 0000000..5fc102e --- /dev/null +++ b/Class35_Exercises_Regex.md @@ -0,0 +1,59 @@ +## Expresiones regulares (Regex) +**1 -** Captura los emails del siguiente texto. +``` +demo@demo.com, demo_demo@demo.com.ar, demo-demo12312@sub.dom.com.ar, demo@novalido, novalido>@demo.com, demo@novalido-.com, demo@-novalido.com +``` + +```javascript +// Tu solución +``` + + +**2 -** Captura el DNI y NIE +- Formato DNI: 11223344-A (Guión opcional). + +``` +Válidos: 12345678-A, 11223344A, +No válidos: A11223344, 1234567K +``` +```javascrip +let regex = /^[\d]{8}-?[\w]$/; +``` + +- Formato para el NIE: X-1223344-A (Guión opcional). + - El inicio puede ser X, Y o Z. + +``` +Válidos: X-1234567-A, X1234567A, Z1234567M +No válidos: X-1233456, 1234567 +``` + +```javascript +let regex = /^x?y?z?-?[\d]{7}-?[\w]$/; +``` + +**3 -** Comprobar la seguridad de una contraseña + +De esta forma comprobaremos: +- Contraseñas que contengan al menos una letra mayúscula. +- Contraseñas que contengan al menos una letra minúscula. +- Contraseñas que contengan al menos un número +- Contraseñas que contengan al menos un caracter especial @#$%. +- Contraseñas cuya longitud sea como mínimo 6 caracteres. +- Contraseñas cuya longitud máxima sea 20 caracteres. + +```javascript +const test = `Válidas: +Z%C2Uacgw_4weL@Q +QZ6UttU-&r4t%R+J +KK8a%K^9seQ$Qc8X +*Q#*9-CP%?JkXQSs +#1234abCD@ + +No válidas: +?mT6JmKpTu6m_=g= +=G4T!v-J2_6aS^EW +perrito +perrito123 +Perrito1234`; +``` From 74969d3c53a4f19c9a5a4a69f8965ebc95473d5e Mon Sep 17 00:00:00 2001 From: minicatsCB <19836547+minicatsCB@users.noreply.github.com> Date: Tue, 22 Jan 2019 23:24:32 +0100 Subject: [PATCH 9/9] Delete Class35_Exercises_Regex --- Class35_Exercises_Regex.md | 59 -------------------------------------- 1 file changed, 59 deletions(-) delete mode 100644 Class35_Exercises_Regex.md diff --git a/Class35_Exercises_Regex.md b/Class35_Exercises_Regex.md deleted file mode 100644 index 5fc102e..0000000 --- a/Class35_Exercises_Regex.md +++ /dev/null @@ -1,59 +0,0 @@ -## Expresiones regulares (Regex) -**1 -** Captura los emails del siguiente texto. -``` -demo@demo.com, demo_demo@demo.com.ar, demo-demo12312@sub.dom.com.ar, demo@novalido, novalido>@demo.com, demo@novalido-.com, demo@-novalido.com -``` - -```javascript -// Tu solución -``` - - -**2 -** Captura el DNI y NIE -- Formato DNI: 11223344-A (Guión opcional). - -``` -Válidos: 12345678-A, 11223344A, -No válidos: A11223344, 1234567K -``` -```javascrip -let regex = /^[\d]{8}-?[\w]$/; -``` - -- Formato para el NIE: X-1223344-A (Guión opcional). - - El inicio puede ser X, Y o Z. - -``` -Válidos: X-1234567-A, X1234567A, Z1234567M -No válidos: X-1233456, 1234567 -``` - -```javascript -let regex = /^x?y?z?-?[\d]{7}-?[\w]$/; -``` - -**3 -** Comprobar la seguridad de una contraseña - -De esta forma comprobaremos: -- Contraseñas que contengan al menos una letra mayúscula. -- Contraseñas que contengan al menos una letra minúscula. -- Contraseñas que contengan al menos un número -- Contraseñas que contengan al menos un caracter especial @#$%. -- Contraseñas cuya longitud sea como mínimo 6 caracteres. -- Contraseñas cuya longitud máxima sea 20 caracteres. - -```javascript -const test = `Válidas: -Z%C2Uacgw_4weL@Q -QZ6UttU-&r4t%R+J -KK8a%K^9seQ$Qc8X -*Q#*9-CP%?JkXQSs -#1234abCD@ - -No válidas: -?mT6JmKpTu6m_=g= -=G4T!v-J2_6aS^EW -perrito -perrito123 -Perrito1234`; -```