Skip to content

Instantly share code, notes, and snippets.

@demarchenac
Last active February 16, 2022 20:17
Show Gist options
  • Save demarchenac/bd64e0badb19ef36137eae47e688ec72 to your computer and use it in GitHub Desktop.
Save demarchenac/bd64e0badb19ef36137eae47e688ec72 to your computer and use it in GitHub Desktop.
Discrete Mathematics, Homework #01, Exercise #04.
// Code developed by:
// - Eduardo David Angulo Madrid.
// - Cristhyan David De Marchena Rueda.
// 2022-02-14.
/**
* Discrete Mathematics, Homework #01, Exercise #04. This methods verifies wether an
* Vertice A (Selected vertice) can be transformed to the vertice B (Target vertice)
* According to the following rules:
* - A letter from the vertice A can be changed and the result is the vertice B.
* - Two letters from thje vertice A can be swaped and the result is the vertice B.
* This method returns wich operation was applied to the vertice A.
* @param {Array<string>} elements Array of strings that conforms the vertices of the graph.
* @param {number} selectedIndex Current element of the vertices that's being iterated.
* @param {number} targetIndex Index of the vertice to match with current selected vertice.
* @param {boolean} printLogs Flags that determines wether or not the logs would be printed.
* @returns 'R' | 'S' | null
*/
const getOperation = (
elements = [],
selectedIndex = 0,
targetIndex = 0,
printLogs = true
) => {
// selected index validations.
if (typeof selectedIndex !== 'number') return null;
if (!Boolean(selectedIndex) && selectedIndex !== 0) return null;
// target index validations.
if (typeof targetIndex !== 'number') return null;
if (!Boolean(targetIndex) && targetIndex !== 0) return null;
// elements list validations.
if (!Boolean(elements)) return null;
if (typeof elements !== 'object') return null;
if (elements.length <= selectedIndex || elements.length <= targetIndex)
return null;
const selected = elements[selectedIndex];
const target = elements[targetIndex];
// One word cannot transform itself into another word if it's already that word.
if (selected === target) return null;
const selectedChars = selected.split('');
const targetChars = target.split('');
const selectedCharsExistance = selectedChars.map(letter =>
targetChars.includes(letter)
);
const presentChars = selectedCharsExistance.reduce((previous, current) => {
if (current) return previous + 1;
else return previous;
}, 0);
// There are no valid word trasnformations with less than two matches within it's letters.
if (presentChars < 2) return null;
let charsInSamePlace = 0;
let matches = [];
for (const charIndex in selectedChars) {
matches.push(selectedChars[charIndex] === targetChars[charIndex]);
if (selectedChars[charIndex] === targetChars[charIndex])
charsInSamePlace++;
}
let operation = null;
if (presentChars === 2 && charsInSamePlace === 2) operation = 'R';
if (presentChars === 3 && charsInSamePlace === 1) operation = 'S';
let log = '';
if (operation === 'R') {
const [selectedChar] = selectedChars.filter(
(element, index) => !matches[index]
);
const charIndex = selectedChars.indexOf(selectedChar);
const newChar = targetChars[charIndex];
log = `Reemplazar la letra \`${selectedChar}' con la letra \`${newChar}'`;
} else if (operation === 'S') {
const [start, swap] = selectedChars.filter(
(element, index) => !matches[index]
);
log = `Intercambiar la letra \`${start}' con la letra \`${swap}'`;
}
if (operation && printLogs) {
log += ` entre los elementos ${selectedIndex} y ${targetIndex} respectivamente.`;
console.log(log);
}
return operation;
};
/**
* This method finds all the adyacent vertice for each vertice in the elements
* of the Graph.
* @param {Array<string>} elements
* @returns An squared matrix with the operations filled in as an adyacency matrix.
*/
const fillAdyacencyMatrix = (elements = []) => {
// elements list validations.
if (!Boolean(elements)) return null;
if (typeof elements !== 'object') return null;
const M = [];
for (const _ in elements) {
const newRow = new Array(elements.length);
M.push(newRow);
}
for (let wordIndex = 0; wordIndex < elements.length; wordIndex++) {
for (
let targetIndex = 0;
targetIndex < elements.length;
targetIndex++
) {
const operation = getOperation(elements, wordIndex, targetIndex);
if (operation) {
M[wordIndex][targetIndex] = operation;
}
}
}
return M;
};
/**
* This method runs the example provided in the Exercise #04 of
* the Homework #01 provided in the Discrete Mathematics Course
*/
const main = () => {
const V = [
'ACT',
'AIM',
'ARC',
'ARM',
'ART',
'CAR',
'CAT',
'OAR',
'OAT',
'RAT',
'TAR',
];
const M = fillAdyacencyMatrix(V);
console.log(`\nVertices:`);
console.log(`V = [${V.join(', ')}]`);
console.log(`\nVertices Renombrados:`);
console.log(`V = [${V.map((v,i) => i).join(', ')}]`);
console.log('\nMatriz de adyacencia:');
console.table(M);
console.log('\nDesarrollado por:');
console.log('\t- Eduardo David Angulo Madrid.');
console.log('\t- Cristhyan David De Marchena Rueda.');
};
main();
@demarchenac
Copy link
Author

The provided code can be executed on repl.it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment