Skip to content

Instantly share code, notes, and snippets.

@Goku-kun
Created November 22, 2020 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Goku-kun/1038ae131fb56b64894c24f68e5431d9 to your computer and use it in GitHub Desktop.
Save Goku-kun/1038ae131fb56b64894c24f68e5431d9 to your computer and use it in GitHub Desktop.
Solution to the DNA pAequor problem
// Returns a random DNA base
const returnRandBase = () => {
const dnaBases = ['A', 'T', 'C', 'G']
return dnaBases[Math.floor(Math.random() * 4)]
}
// Returns a random single stand of DNA containing 15 bases
const mockUpStrand = () => {
const newStrand = []
for (let i = 0; i < 15; i++) {
newStrand.push(returnRandBase())
}
return newStrand
}
let pAequorFactory = (num, array) => {
return {
specimenNum: num,
dna: array,
mutate() {
while(true) {
let randomDna = returnRandBase();
let randomMutator = Math.floor(Math.random() * 15);
if(this.dna[randomMutator] != randomDna) {
this.dna[randomMutator] = randomDna;
break;
}
}
},
compareDNA(pae2) {
console.log(`The DNA of ${this.specimenNum} and ${pae2.specimenNum} is being compared.`);
let count = 0;
for ( let i=0; i<pae1.dna.length; i++) {
if(this.dna[i] == pae2.dna[i]) {
count++;
}
}
console.log(this.dna);
console.log(pae2.dna);
console.log((count/this.dna.length) * 100);
},
willLikelySurvive() {
let count = 0;
for(let i=0; i<this.dna.length; i++) {
if(this.dna[i] == 'C' || this.dna[i] == 'G'){
count++;
}
}
if (count/this.dna.length >= 0.6) {
return true;
} else {
return false;
}
}
}
}
let survivingPAequor = [];
let count = 1;
while (true) {
let pae = pAequorFactory(count, mockUpStrand());
if(pae.willLikelySurvive()) {
survivingPAequor.push(pae);
count++;
}
if (count == 31) {
break;
}
}
console.log(survivingPAequor);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment