Skip to content

Instantly share code, notes, and snippets.

@mdciotti
mdciotti / encodeURIComplete.js
Last active August 1, 2022 03:26
Completely encode all characters of a string into a URL escape sequence.
// NOTE: This is overkill. I wrote this and then quickly realized there is a better way to do it. See below.
/**
* https://tc39.es/ecma262/#leading-surrogate
* @param {number} codeUnit
*/
function isLeadingSurrogate(codeUnit) {
return 0xD800 <= codeUnit && codeUnit <=0xDBFF;
}
@mdciotti
mdciotti / FakeAPI.js
Created April 26, 2019 14:45
Mock API for testing
/**
* Simulates asynchronous data loading by wrapping setTimeout in a Promise.
* @param {Number} ms the delay in milliseconds to wait before resolving
* @param {any} data the data to resolve with
* @returns {Promise} a promise that resolves with `data` in `ms` milliseconds
*/
async function asyncData(ms, data) {
await new Promise(r => setTimeout(r, ms));
return data;
}
@mdciotti
mdciotti / fib.cpp
Created September 12, 2016 18:48
Fibonacci Number Generators
// Recursive 1
int rFibNum(int a, int b, int n)
{
if (n == 1)
return a;
else if (n == 2)
return b;
else
return rFibNum(a, b, n - 1) + rFibNum(a, b, n - 2);
}
@mdciotti
mdciotti / lattice-paths.c
Created October 16, 2015 06:39
Calculates the number of paths in a n*n (square) lattice
#import <stdio.h>
#import <stdlib.h>
#import <math.h>
// Retuns the number of paths in a n*n (square) lattice
// Paths can only move rightward or downward
// Paths start at top left and end at bottom right
// Receives an argument `n` which is the length of one side of the square lattice
require 'set'
require 'json'
class QueryHandler
def initialize (indexPath, tagPath)
index_get indexPath
tags_get tagPath
end
def parse_json (path)
/* SEARCH QUERY BISON SOURCE */
%left '|'
%left '&' '!'
%left '(' ')'
%start file
%%
@mdciotti
mdciotti / hexconverter.js
Created October 16, 2015 06:22
Converts hexadecimal-formatted strings to numbers and back
Number.prototype.toHex = function (padSize) {
if (padSize == undefined) padsize = 1;
var hex = this.toString(16).toUpperCase();
while (padSize > hex.length) {
hex = "0" + hex;
}
return hex;
};
String.prototype.toHex = function () {
function hash (s) {
var h = 7;
var letters = "acdegilmnoprstuw";
for (var i = 0; i < s.length; i++) {
h = (h * 37 + letters.indexOf(s[i]));
}
return h;
}
#include <stdio.h>
#include <stdlib.h>
unsigned long long int reduce (unsigned long long int n)
{
unsigned long long int result, d;
result = 1;
while (n > 0) {

Keybase proof

I hereby claim:

  • I am mdciotti on github.
  • I am mdciotti (https://keybase.io/mdciotti) on keybase.
  • I have a public key whose fingerprint is 5B7A 9BBE C0E3 9EE3 322F 011C 8AC8 D94E 8A1C 957D

To claim this, I am signing this object: