Skip to content

Instantly share code, notes, and snippets.

@OneCent01
OneCent01 / color object
Last active January 3, 2022 07:28
Every color contained in an object with its corresponding hexa value for O(1) time lookup, find function below object
var colors = {
ALICEBLUE: '#F0F8FF',
ANTIQUEWHITE: '#FAEBD7',
AQUA: '#00FFFF',
AQUAMARINE: '#7FFFD4',
AZURE: '#F0FFFF',
BEIGE: '#F5F5DC',
BISQUE: '#FFE4C4',
BLACK: '#000000',
BLANCHEDALMOND: '#FFEBCD',
@OneCent01
OneCent01 / C: read_input_char
Last active November 15, 2019 08:16
Reads character input from the user; handles arrow keys, converting them to their equivalent WASD character
char read_input_char()
{
char c;
c = getch();
if (c == '\033') { // if the first value is esc
c = getch(); // skip the [
switch(c = getch()) { // the real value
case 'A':
// code for arrow up
return 'w';
@OneCent01
OneCent01 / Obsidian-Express
Last active October 7, 2019 23:42
Secure NodeJS server with add and auth users; salted and hashed passes, token authentication, and apply secure response headers
// This should be an external database in real development. This is just for simplified demonstration.
const memoryDB = {
users: {},
usersCount: 0
}
const express = require('express')
const obsidian = require('obsidian-js')
const bodyParser = require('body-parser')
@OneCent01
OneCent01 / Hashtable insert value
Last active January 21, 2019 08:07
Javascript implementation of an insert value method for hashtables
HashTable.prototype.add = function(key, value) {
var index = key.hashCode();
var tuple = [key, value];
var bucket = this._storage.get(index);
if (bucket) {
for (var i = 0; i < bucket.length; i++) {
if (bucket[i][0] === key) {
bucket[i][1] = value;
} else {
bucket.push(tuple);
var arraySum = function(array) {
var total = 0;
if(array.length === 0) {
return 0;
}
var popped = array.pop();
if(Array.isArray(popped)) {
total += arraySum(popped);
return total += arraySum(array);
}
var delegationObjects = {
Employee: (name) => Object.assign({
name,
hello: () => console.log(`Hi, I'm ${name}`)
}),
Fulltime: (name, role) => Object.assign(delegationObjects.Employee(name), {
role,
weeklyHours: 40
}),
@OneCent01
OneCent01 / JS Factory Method Example
Last active February 10, 2018 17:24
Employee Objects - modified code originally found at: http://www.dofactory.com/javascript/factory-method-design-pattern
var subclasses = {
Employee: function(name, role) {
this.name = name;
this.sayHi = function() {
console.log(this.name + ": I work " + this.weeklyHours + "hrs/week");
};
if(role === "manager" || role === "salesman") return subclasses.Fulltime(name, role);
else if(role === "temporary" || role === "traveling") return subclasses.Parttime(name, role);
},
Fulltime: function(name, role) {
@OneCent01
OneCent01 / JS Employee Class Inheritance
Created February 9, 2018 18:34
Inheritance model implemented in JavaScript, a prototype-based language without classes
function Employee() {
this.name = '';
this.dept = 'general';
}
function Manager() {
Employee.call(this);
this.reports = [];
}
Manager.prototype = Object.create(Employee.prototype);
@OneCent01
OneCent01 / battleship.py
Created January 29, 2018 19:56
final battleship game from the CodeAcademy intro tutorial
from random import randint
board = []
for x in range(5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print " ".join(row)
var findLeft = function(grid, row, col) {
if(col > 0) {
return grid[row][col - 1];
} else {
return undefined;
}
};
var findRight = function(grid, row, col) {
if(col < grid[row].length-1) {