Pointers, or “Making a Chimera”

If you read the previous blog post about “Sliding Window”, the concept of “Pointers” might be likened to a window of variable size, where whatever remains between the left side of the window and the right side is what will be iterated through to find what you’re looking for. Common uses for pointers might be where you have to iterate through an array (or values that have been converted into an array) to find some value.

Commonly utilized with pointers is the while loop, the reason being is that a while loop’s conditional logic works well with determining which pointer is moved, and when they stop.

The “Making a Chimera” analogy felt “natural” in this context because it was easy to imagine the array as a rack of vials containing sample DNA of different animals which are alphabetized. NOTE: the alphabetization is not random, pointers work will with sorted data, and strings that are alphabetized can be compared using the ‘>', '>=', '<=','<' operators. Visualizing a robot with a left hand and a right, searching the vials of DNA in order to pluck out the correct monster combo sat well with my imagination.

The code below is a demonstration of pointers in this context.

Also, heads up, there is some code (commented as such) that is not integral to understanding pointers, but just there for entertainment value. Some ‘nonsense’ to make the name of the returned creature sound more ridiculous, and also add a “number” that makes it seem more ‘scientific’. Though not relevant to pointers, you may find that code useful or interesting in its own right.

const dnaSamples = ['goat', 'dog', 'pig', 'snake', 'cat', 'mosquito', 'spider', 'frog', 'puggle', 'monkey', 'elephant', 'zebra', 'gorilla', 'donkey', 'rat', 'rabbit', 'alpaca']

function dnaComboForMutantChimera(sampleDNA, mutant) {
  
  const vialsAlphabetizedByCreature = sampleDNA.map(sample => sample.toUpperCase()).sort()
  console.log("sorting DNA samples into alphabetical order... \n", "  ", vialsAlphabetizedByCreature)
  const targetMutant = mutant.split(' ').sort().join('').toUpperCase()
  console.log("parsing input for target mutant: ", mutant, "into", targetMutant, "\n \n")
  
  let left = 0,
    right = vialsAlphabetizedByCreature.length - 1;
  while (left < right) {
    const currentMutant = vialsAlphabetizedByCreature[left] + vialsAlphabetizedByCreature[right];
    console.log("the current mutant we're examining is:", currentMutant)
    if (currentMutant === targetMutant) {
      const revoltingCreatureInDisarray = targetMutant.split('');
      const letterToRemoveIndex = Math.floor(Math.random()*revoltingCreatureInDisarray.length)
  
      //some code for entertainment value, in naming the chimera
      const vowels = ['A','E','I','O','U']
      const addThisVowel = vowels[Math.floor(Math.random()*vowels.length)]
      const removedChar = revoltingCreatureInDisarray.splice(letterToRemoveIndex, 1, addThisVowel)
      const newName = (revoltingCreatureInDisarray.join('')) + " " + ascii_to_hexa(`${removedChar}`)
      
      return "\nCongratulations Doctor, we've managed to grab DNA from '" + [left, right] +"' in order to make the " + newName;
    }

    if (targetMutant > currentMutant) {
      left += 1; 
    } else {
      right -= 1; 
    }
  }
  return console.log("\nsorry, we can't make that chimera");
}
  
 


// this is a helper function just for entertainment 
function ascii_to_hexa(str)
  {
	let arr = [];
	for (let n = 0, l = str.length; n < l; n ++) 
     {
		let hex = Number(str.charCodeAt(n)).toString(16);
		arr.push(hex);
	 }
	return arr.join('');
   }

Now… for the return…

console.log(dnaComboForMutantChimera(dnaSamples, "dog pig")) // returns what's below

sorting DNA samples into alphabetical order... 
  
(17) [
"ALPACA",
"CAT",
"DOG",
"DONKEY",
"ELEPHANT",
"FROG",
"GOAT",
"GORILLA",
"MONKEY",
"MOSQUITO",
"PIG",
"PUGGLE",
"RABBIT",
"RAT",
"SNAKE",
"SPIDER",
"ZEBRA"
]
parsing input for target mutant: dog pigintoDOGPIG
 
the current mutant we're examining is:ALPACAZEBRA
the current mutant we're examining is:CATZEBRA
the current mutant we're examining is:DOGZEBRA
the current mutant we're examining is:DOGSPIDER
the current mutant we're examining is:DOGSNAKE
the current mutant we're examining is:DOGRAT
the current mutant we're examining is:DOGRABBIT
the current mutant we're examining is:DOGPUGGLE
the current mutant we're examining is:DOGPIG

Congratulations Doctor, we've managed to grab DNA from '2,10' in order to make the DOEPIG 47

Discover more from Comedy Tragedy Epic

Subscribe now to keep reading and get access to the full archive.

Continue reading