Last active
October 11, 2024 15:36
-
-
Save Samu31Nd/e37a5b7ed1f10afda308698ced7fed94 to your computer and use it in GitHub Desktop.
Practica 5 - Logica de primer orden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Ejemplo 1 | |
| /** | |
| * Funcion que aplica un predicado en cada uno de los elementos | |
| * y tiene que cumplirse en todos para devolver true. | |
| * @param {Function} predicate predicado | |
| * @param {Array<Number>} arr arreglo | |
| * @returns Number | |
| */ | |
| const all = (predicate, arr) => { | |
| return arr.every(predicate); | |
| } | |
| //creamos el arreglo | |
| const X = [1,2,3,4,5,6]; | |
| //función anonima para verificar el predicado P | |
| const P = (x) => x%1 == 0; | |
| const todos_cumplen = all(P,X); | |
| console.log(todos_cumplen); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Ejemplo 2 | |
| /** | |
| * Funcion que aplica un predicado en cada uno de los elementos | |
| * y tiene que cumplirse en todos para devolver true. | |
| * @param {Function} predicate predicado | |
| * @param {Array<Number>} arr arreglo | |
| * @returns Number | |
| */ | |
| const all = (predicate, arr) => { | |
| return arr.every(predicate); | |
| } | |
| const X = [0,1,2,3,4,5]; | |
| //Predicados | |
| const P = (x) => x%2 == 0; | |
| const Q = (x) => x > 1; | |
| //creamos otra funcion lambda que verifique que si se cumple | |
| //P(x) entonces también debe de cumplirse Q(x) | |
| const todos_cumplen_implicacion = all( (x) => !P(x) || Q(x), X ); | |
| console.log(`Todos los elementos cumplen P(x) -> Q(x): ${todos_cumplen_implicacion}`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //aun no lo hago jsjs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Ejemplo 4 | |
| //areglo | |
| const X = [[1,2],[3,1],[5,6],[4,4]]; | |
| //predicado a cumplir | |
| const P = (x,y) => x > y; | |
| //si existe al menos un elemento que cumpla la condición, regresa true | |
| const existe_al_menos_un_par = X.some((element) => P(element[0], element[1])); | |
| console.log(`Existe al menos un par que cumple P(x,y): ${existe_al_menos_un_par}`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //EjercicioPropuesto | |
| import { round } from "https://deno.land/x/[email protected]/round.ts"; | |
| import { sqrt } from "https://deno.land/x/[email protected]/sqrt.ts"; | |
| /** | |
| * Funcion que aplica un predicado en cada uno de los elementos | |
| * y tiene que cumplirse en todos para devolver true. | |
| * @param {Function} predicate predicado | |
| * @param {Array<Number>} arr arreglo | |
| * @returns Number | |
| */ | |
| const all = (predicate, arr) => arr.every(predicate); | |
| const distance = (x1,y1,x2,y2) => { | |
| const dx = x2-x1; | |
| const dy = y2-y1; | |
| const distance_obtained = sqrt( dx*dx + dy*dy ); | |
| return round(distance_obtained,2); | |
| }; | |
| const timeForKm = 25; // cada 25 km es 1 h | |
| const tMax = 48; | |
| //pares ordenados de eje x, y | |
| // const clientes = [ | |
| // [0,0], | |
| // [20,40], | |
| // [10,5] | |
| // ]; | |
| const clientes = [ | |
| [-700,460], | |
| [-320,-765], | |
| [-200,-146], | |
| [454,204], | |
| [4.56,-1636], | |
| [604,-483], | |
| [-414,1174], | |
| [1092,855], | |
| ] | |
| // const centrosDistribucion = [ | |
| // [50,100], | |
| // [-400,200], | |
| // [200,400] | |
| // ]; | |
| const centrosDistribucion = [ | |
| [-260,463], | |
| [2891,1448], | |
| [-1258,3183], | |
| [377,-2740], | |
| [197,-870] | |
| ]; | |
| const D = (x,y) => distance(x[0],x[1],y[0],y[1]); | |
| const T = (x,y) => { | |
| console.log("Tiempo calculado: ", D(x,y)/timeForKm); | |
| return D(x,y)/timeForKm | |
| }; | |
| /** | |
| * Verifica si un cliente está dentro del límite de tiempo de algún centro. | |
| * @param {Array} cliente | |
| * @returns Boolean | |
| */ | |
| const predicate = (cliente) => centrosDistribucion.some((centro) => T(cliente, centro) <= tMax); | |
| const todos_los_clientes = all(predicate, clientes); | |
| if (todos_los_clientes) { | |
| console.log("Todos cumplen"); | |
| } else { | |
| console.log("No todos cumplen..."); | |
| const incumplidores = clientes.filter((cliente) => !predicate(cliente)); | |
| console.log("Los que no cumplen:", incumplidores); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Problema PH y nitratos | |
| /** | |
| * Funcion que aplica un predicado en cada uno de los elementos | |
| * y tiene que cumplirse en todos para devolver true. | |
| * @param {Function} predicate predicado | |
| * @param {Array<Number>} arr arreglo | |
| * @returns Number | |
| */ | |
| const all = (predicate, arr) => { | |
| return arr.every(predicate); | |
| }; | |
| const fuentes = [ | |
| [7.2,30], | |
| [8.0,45], | |
| [6.3,40], // no cumple el ph | |
| [7.8,55], // no cumple el nitratos | |
| [6.9,20], | |
| ]; | |
| //predicados | |
| const P = (ph) => (ph >= 6.5) && (ph <= 8.5); | |
| const Q = (nitratos) => nitratos < 50; | |
| const S = (ph,nitratos) => (P(ph) && Q(nitratos)); | |
| //lo adaptamos a lo que tenemos | |
| const cumplimiento = (array) => | |
| S(array[0],array[1]); | |
| const todas_cumplen = all(cumplimiento,fuentes); | |
| //si existe una que no cumpla, regresa verdadero | |
| const existe_al_menos_una_no_cumple = fuentes.some((element) => !cumplimiento(element)); | |
| if(todas_cumplen) | |
| console.log("Todas las fuentes de agua cumplen con los estandares de calidad"); | |
| else{ | |
| console.log("Al menos una fuente de agua no cumple con los estandares de calidad"); | |
| console.log("Fuentes que no cumplen: "); | |
| console.log( fuentes.map((element) => !cumplimiento(element) ? element:'') ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment