-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5874 from RicJDev/main
#34 y 35 - JavaScript
- Loading branch information
Showing
4 changed files
with
310 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
170 changes: 170 additions & 0 deletions
170
Roadmap/34 - ÁRBOL GENEALÓGICO LA CASA DEL DRAGÓN/javascript/RicJDev.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
EJERCICIO | ||
@RicJDev | ||
*/ | ||
|
||
class Person { | ||
constructor(id, name) { | ||
this.name = name | ||
this.id = id | ||
|
||
this.parents = [] | ||
this.children = [] | ||
|
||
this.partner = null | ||
} | ||
|
||
addChild(child) { | ||
if (!this.children.includes(child)) { | ||
if (child.hasParents()) { | ||
console.log( | ||
`${child.name} ya tiene padres: ${child.parents[0].name}, ${child.parents[1].name}.` | ||
) | ||
} else { | ||
child.parents.push(this) | ||
this.children.push(child) | ||
|
||
console.log(`${this.name} ha tenido un hijo: ${child.name}.`) | ||
} | ||
} else { | ||
console.log(`${child.name} ya es hijo de ${this.name}.`) | ||
} | ||
} | ||
|
||
setPartner(partner) { | ||
if (partner.hasPartner() || this.hasPartner()) { | ||
this.hasPartner() | ||
? console.log(`${this.name} ya tiene pareja: ${this.partner.name}.`) | ||
: console.log(`${partner.name} ya tiene pareja: ${partner.partner.name}.`) | ||
} else { | ||
this.partner = partner | ||
partner.partner = this | ||
|
||
console.log(`${this.name} ahora es pareja de ${partner.name}.`) | ||
} | ||
} | ||
|
||
hasPartner() { | ||
return !(this.partner === null) | ||
} | ||
|
||
hasParents() { | ||
return this.parents.length === 2 | ||
} | ||
} | ||
|
||
class FamilyTree { | ||
constructor() { | ||
this.people = {} | ||
} | ||
|
||
addPerson(id, name) { | ||
if (this.people[id]) { | ||
console.log(`El ID ${id} ya ha sido registrado.`) | ||
} else { | ||
this.people[id] = new Person(id, name) | ||
|
||
console.log(`Se ha registrado a ${name} [ID: ${id}].`) | ||
} | ||
} | ||
|
||
deletePerson(id) { | ||
this.people[id] | ||
? delete this.people[id] | ||
: console.log(`No se ha encontrado a ninguna persona con la ID: ${id || 0}.`) | ||
} | ||
|
||
setPartner(id1, id2) { | ||
const person1 = this.people[id1] | ||
const person2 = this.people[id2] | ||
|
||
person1 && person2 | ||
? person1.setPartner(person2) | ||
: console.log(`Una de las ID's no coincide con ningún registro: ${id1 || 0}, ${id2 || 0}`) | ||
} | ||
|
||
addChild(id, childId) { | ||
const parent = this.people[id] | ||
const child = this.people[childId] | ||
|
||
if (parent && child) { | ||
parent.addChild(child) | ||
|
||
if (parent.hasPartner()) { | ||
parent.partner.addChild(child) | ||
} | ||
} else { | ||
console.log(`Una de las ID's no coincide con ningún registro: ${id || 0}, ${childId || 0}`) | ||
} | ||
} | ||
|
||
displayTree() { | ||
const visited = new Set() | ||
|
||
function printPerson(person, level) { | ||
if (visited.has(person.id)) return | ||
visited.add(person.id) | ||
|
||
let ident = ' '.repeat(level * 4) | ||
|
||
console.log(`${ident} - ${person.name} [ID: ${person.id}]`) | ||
|
||
if (person.partner) { | ||
visited.add(person.partner.id) | ||
|
||
console.log(`${ident} Pareja: ${person.partner.name} [ID: ${person.id}]`) | ||
} | ||
|
||
if (person.children.length > 0) { | ||
console.log(`${ident} Hijos:`) | ||
person.children.forEach((child) => { | ||
printPerson(child, level + 1) | ||
}) | ||
} | ||
} | ||
|
||
const rootPersons = Object.values(this.people).filter((person) => { | ||
return person.parents.length === 0 | ||
}) | ||
|
||
rootPersons.forEach((root) => printPerson(root, 0)) | ||
} | ||
} | ||
|
||
const tree = new FamilyTree() | ||
|
||
tree.addPerson(1, 'Aegon I Targaryen') | ||
tree.addPerson(2, 'Rhaenys Targaryen') | ||
tree.addPerson(3, 'Visenya Targaryen') | ||
|
||
tree.addPerson(4, 'Aenys I Targaryen') | ||
tree.addPerson(5, 'Maegor I Targaryen') | ||
|
||
tree.addPerson(6, 'Jaehaerys I Targaryen') | ||
tree.addPerson(7, 'Alysanne Targaryen') | ||
|
||
tree.addPerson(8, 'Aegon II Targaryen') | ||
tree.addPerson(9, 'Helaena Targaryen') | ||
|
||
tree.addPerson(10, 'Viserys II Targaryen') | ||
|
||
tree.addPerson(11, 'Rhaenyra Targaryen') | ||
tree.addPerson(12, 'Daemon Targaryen') | ||
|
||
tree.setPartner(1, 2) | ||
tree.setPartner(1, 3) | ||
tree.setPartner(4, 7) | ||
tree.setPartner(8, 9) | ||
tree.setPartner(11, 12) | ||
|
||
tree.addChild(1, 4) | ||
tree.addChild(1, 5) | ||
|
||
tree.addChild(4, 6) | ||
tree.addChild(4, 11) | ||
|
||
tree.addChild(6, 8) | ||
tree.addChild(6, 10) | ||
|
||
console.log(' ') | ||
tree.displayTree() |
100 changes: 100 additions & 0 deletions
100
Roadmap/35 - REPARTIENDO LOS ANILLOS DE PODER/javascript/RicJDev.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
EJERCICIO: | ||
- Se ejecuta en Node.js, importando el módulo 'Readline/promises' (https://nodejs.org/api/readline.html) | ||
@RicJDev | ||
*/ | ||
|
||
function isPrime(num) { | ||
if (num <= 1) return false | ||
if (num <= 3) return true | ||
if (num % 2 === 0 || num % 3 === 0) return false | ||
|
||
for (let i = 5; i * i <= num; i += 6) { | ||
if (num % i === 0 || num % (i + 2) === 0) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
const isEven = (num) => num % 2 === 0 | ||
|
||
function distributeRings(totalRings) { | ||
let rings = null | ||
|
||
if (totalRings < 6) { | ||
console.warn('No hay suficientes anillos para repartir') | ||
return rings | ||
} | ||
|
||
totalRings -= 1 | ||
|
||
let bestDiference = Number.MAX_SAFE_INTEGER | ||
|
||
for (let elvesRings = 1; elvesRings <= totalRings; elvesRings += 2) { | ||
for (let dwarvesRings = 2; dwarvesRings <= totalRings; dwarvesRings++) { | ||
if (isPrime(dwarvesRings)) { | ||
let humansRings = totalRings - elvesRings - dwarvesRings | ||
|
||
if (isEven(humansRings) && humansRings > 0) { | ||
const diference = | ||
Math.max(elvesRings, dwarvesRings, humansRings) - | ||
Math.min(elvesRings, dwarvesRings, humansRings) | ||
|
||
if (diference < bestDiference) { | ||
bestDiference = diference | ||
|
||
rings = { | ||
elves: elvesRings, | ||
dwarves: dwarvesRings, | ||
humans: humansRings, | ||
sauron: 1, | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (rings === null) { | ||
console.warn('No se ha encontrado una combinación para repartir los anillos') | ||
} | ||
|
||
return rings | ||
} | ||
|
||
//Implementación en terminal | ||
|
||
import * as readline from 'readline/promises' | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}) | ||
|
||
console.clear() | ||
|
||
while (true) { | ||
let answer = await rl.question('Indique la cantidad de anillos a repartir. ') | ||
|
||
let totalRings = parseInt(answer.match(/\d+/g)) | ||
|
||
if (isNaN(totalRings)) { | ||
console.log('Por favor igresar numeros enteros') | ||
} else { | ||
const rings = distributeRings(totalRings) | ||
|
||
if (rings !== null) { | ||
console.log(`\nSe han distribuido los ${totalRings} anillos de la siguiente manera: `) | ||
console.log('Elfos:', rings.elves) | ||
console.log('Enanos:', rings.dwarves) | ||
console.log('Hombres:', rings.humans) | ||
console.log('Sauron:', rings.sauron) | ||
|
||
rl.close() | ||
break | ||
} | ||
} | ||
} |