Skip to content

Instantly share code, notes, and snippets.

@bmcculley
Last active April 4, 2022 18:46
Show Gist options
  • Save bmcculley/b375ff9df255d17596717b0ca1fed6cf to your computer and use it in GitHub Desktop.
Save bmcculley/b375ff9df255d17596717b0ca1fed6cf to your computer and use it in GitHub Desktop.
Master the Coding Interview: Data Structures + Algorithms (udemy, find nemo exercise)
// https://nodejs.org/docs/v12.22.10/api/perf_hooks.html
const {
performance
} = require('perf_hooks');
//#1 -- For loop in Javascript.
const fish = ['dory', 'bruce', 'marlin', 'nemo'];
const nemo = ['nemo'];
const everyone = ['dory', 'bruce', 'marlin', 'nemo', 'gill', 'bloat', 'nigel', 'squirt', 'darla', 'hank'];
const large = new Array(10).fill('nemo');
function findNemo2(fish) {
let t0 = performance.now();
for (let i = 0; i < fish.length; i++) {
if (fish[i] === 'nemo') {
console.log('Found NEMO!');
}
}
let t1 = performance.now();
console.log("Call to find Nemo took " + (t1 - t0) + " milliseconds.");
}
findNemo2(everyone)
#include <iostream>
#include <vector>
#include <string>
#include <chrono>
void findNemo(std::vector<std::string> fish) {
using namespace std::chrono;
high_resolution_clock::time_point t0 = high_resolution_clock::now();
for (unsigned i=0; i<fish.size(); i++) {
if (fish[i] == "nemo") {
std::cout << "Found NEMO!" << std::endl;
}
}
high_resolution_clock::time_point t1 = high_resolution_clock::now();
duration<double> time_span = duration_cast<duration<double>>(t1 - t0);
std::cout << "Call to find Nemo took " << time_span.count() << " milliseconds." << std::endl;
}
int main() {
std::vector<std::string> fish{"dory", "bruce", "marlin", "nemo"};
std::vector<std::string> nemo{"nemo"};
std::vector<std::string> everyone{"dory", "bruce", "marlin", "nemo", "gill", "bloat", "nigel", "squirt", "darla", "hank"};
std::vector<std::string> large;
int large_size = 10;
for (unsigned i=0; i<large_size; i++) {
large.push_back("nemo");
}
std::cout << "Find nemo in nemo vector: " << std::endl;
findNemo(nemo);
std::cout << "Find nemo in everyone vector:" << std::endl;
findNemo(everyone);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment