Skip to content

Instantly share code, notes, and snippets.

View clarketm's full-sized avatar
🐍
Programming

Travis Clarke clarketm

🐍
Programming
View GitHub Profile
@clarketm
clarketm / clockAngleProblem.js
Last active July 18, 2020 10:17
Clock Angle Problem (JavaScript)
function getClockAngle(hours, minutes, isPortionWithTwelve) { // using 12-hour OR 24-hour clock notation
hours = hours % 12;
var hourMinPart = 0.5 * minutes, // 30 degrees per 60 minutes => 1/2 degree per 1 minute => 0.5 * minute
hourHourPart = 30 * hours, // 30 degrees per 1 hour => 30 * hour
minAngle = 6 * minutes, // 360 degrees per 60 minutes => 6 degrees per 1 minute => 6 * minute
totalAngle = Math.abs(hourMinPart + hourHourPart - minAngle); // absolute difference
return isPortionWithTwelve ? 360 - totalAngle : totalAngle; // subtract the total angle from 360 to get the portion w/ 12
}
@clarketm
clarketm / findMinArray.js
Created May 23, 2016 23:40
Find Min Array (JavaScript)
function findMin(array) {
var min = array[0],
count = 0;
for (var i = 1; i < array.length; i++, count++) {
if (array[i] < min) {
min = array[i];
}
}
console.log(count + " iterations");
@clarketm
clarketm / ransomNoteProblem.js
Last active August 12, 2016 04:26
Ransom Note Problem (JavaScript)
function isRansomNotePossible(newsArticle, ransomNote) {
var availableChars = {};
for (var r = 0; r < newsArticle.length; r++) {
var asciiCode = newsArticle.charCodeAt(r);
availableChars[asciiCode] = (availableChars[asciiCode] || 0) + 1
}
for (var r = 0; r < ransomNote.length; r++) {
var asciiCode = ransomNote.charCodeAt(r);
@clarketm
clarketm / ransomNoteProblemES6.js
Last active May 24, 2016 01:29
Ransom Note Problem (ES6)
function isRansomNotePossible(newsArticle, ransomNote) {
let availableChars = new Map();
for (let r of newsArticle) {
let asciiCode = newsArticle.charCodeAt(r);
availableChars.set(asciiCode, (availableChars.get(asciiCode) || 0) + 1);
}
for (let r of ransomNote) {
let asciiCode = ransomNote.charCodeAt(r);
@clarketm
clarketm / FindMinBinarySearch.js
Created May 24, 2016 01:31
Find Min Binary Search (JavaSript)
function findMinBinarySearch(array) {
var left = 0, // index of first element
right = array.length - 1, // index of last element.
mid, // midpoint
count = 0;
while (array[left] > array[right]) {
count++;
mid = ~~((left + right) / 2); // math.floor of the midpoint
if (array[mid] > array[right]) {
@clarketm
clarketm / binarySearch.js
Last active April 8, 2017 06:01
Binary Search (JavaScript)
function binarySearch(array, target) {
var startIndex = 0,
stopIndex = array.length - 1,
middle,
count = 0;
while (startIndex < stopIndex) {
count++;
middle = ~~((stopIndex + startIndex) / 2);
@clarketm
clarketm / The Technical Interview Cheat Sheet.md
Created May 24, 2016 16:40 — forked from tsiege/The Technical Interview Cheat Sheet.md
This is my technical interview cheat sheet. Feel free to fork it or do whatever you want with it. PLEASE let me know if there are any errors or if anything crucial is missing. I will add more links soon.

Studying for a Tech Interview Sucks, so Here's a Cheat Sheet to Help

This list is meant to be a both a quick guide and reference for further research into these topics. It's basically a summary of that comp sci course you never took or forgot about, so there's no way it can cover everything in depth. It also will be available as a gist on Github for everyone to edit and add to.

Data Structure Basics

###Array ####Definition:

  • Stores data elements based on an sequential, most commonly 0 based, index.
  • Based on tuples from set theory.
@clarketm
clarketm / largestBinaryGap.js
Last active October 12, 2022 12:13
Largest Binary Gap (JavaScript)
function largestBinaryGap(num) {
var bin = Math.abs(num).toString(2),
finalMax = 0,
currentMax;
for (var i = 0; i < bin.length; i++) {
currentMax = 0;
while (bin[i] === "0") {
++currentMax && ++i;
}
@clarketm
clarketm / cyclicRotation.js
Last active June 16, 2021 15:38
Cyclic Rotation (JavaScript)
function cyclicRotation(array, times) {
var rotatedArray = array;
while (times > 0) {
var currentArray = rotatedArray.slice();
for (var i = 0; i < currentArray.length; i++) {
rotatedArray[(i+1)%currentArray.length] = currentArray[i];
}
times--;
@clarketm
clarketm / ch_04_picture_grid.py
Last active September 26, 2016 03:23 — forked from anonymuse/ch_04_picture_grid.py
Automate the Boring Stuff with Python -- Chapter 04 -- Character Picture Grid
grid = [['.', '.', '.', '.', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['O', 'O', 'O', 'O', 'O', '.'],
['.', 'O', 'O', 'O', 'O', 'O'],
['O', 'O', 'O', 'O', 'O', '.'],
['O', 'O', 'O', 'O', '.', '.'],
['.', 'O', 'O', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]