Skip to content

Instantly share code, notes, and snippets.

View prmichaelsen's full-sized avatar
🌚

Patrick Michaelsen prmichaelsen

🌚
  • 12:26 (UTC -06:00)
View GitHub Profile
let prev = null;
let reverse = null;
let it = head;
while (it !== null) {
prev = it;
it = it.next;
let temp = {
val: prev.val,
next: reverse,
};
// creating an array of ints without generics
int[] myArray = new int[] {};
// creating lists for ints and strings, respectively
IntList myIntList = new IntList();
StringList myStringList = new StringList();
// creating a list for each using generics
List<int> myGenericIntList = new ArrayList<int>();
List<string> myGenericStringList = new ArrayList<string>();
@prmichaelsen
prmichaelsen / deep-complete.ts
Created May 16, 2019 23:37
recursively mark all properties of a typescript type as defined
/** private type, not exported */
declare type NonObject = undefined | null | boolean | string | number | Function;
/**
* This type allows you to mark an object with
* optional properties as required.
*/
export declare type Complete<T> = {
[K in keyof T]-?: T[K];
}
/**
@prmichaelsen
prmichaelsen / stuff-you-should-know.md
Last active August 13, 2019 18:12
stuff you should _probably_ know before you walk into an interview for a big name tech company

Stuff you should probably know before going for a code interview

This is a quick and dirty list of all the stuff you should know before you walk into an interview for a big name tech company. This will not cover any software topics, only computer science topics. This will help you in the coding portion of interviews, not in design portions.

ℹ - Use the language you are most familiar with for any of this content. Don't use a language you don't use everyday, or it will show. "...how do you truncate an array again?" Remember that a lot of interviewers expect you to code on a whiteboard. There will be no autocomplete or google to help you if you forget the name of a function.

At the very least, you should have written code to do these things at least once. Ideally, you could do each without a reference.

you should know:

@prmichaelsen
prmichaelsen / PrivateInvocationTestUtil.java
Created May 2, 2019 18:29
a snippet I wrote to test a private method before I made the philosophical decision to just not test private methods
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class TestClass {
class PrivateInvocationException extends Exception {
public PrivateInvocationException(String message) { super(message); }
}
@SuppressWarnings("unchecked") // method with throw if type-cast is wrong, anyway.
private <TClass, TOut> TOut invokePrivateMethod(
Class testClass,
@prmichaelsen
prmichaelsen / mappedGenericObject.ts
Created February 11, 2019 19:35
neat trick for typing an object transform function
/**
this lets you type a function that transforms a map
from a map with KV such that typeof V is some type A<T>
to a a map with KV such that typeof V is some type B<T>
In this case, we go from DataRequest<T> to ISubscription<T>
*/
class Definition<T> {}
interface DataRequest<T> {
def: Definition<T>;
#############################################################
# lotto.py
#############################################################
# Description: Compare avg return of 2018-10-23
# mega million lottery compared to number of tickets bought.
# Note: this script doesn't appear to implement the correct
# behavior for doing so. I think my math is off. Oh well, it
# was fun trying.
#############################################################
/**
* the stupidest way I could imagine to paginate
* an input array by pages with pageSize
*/
const paginate = (data, pageSize) => [
...Array(~~((data || []).length/pageSize)).keys()
].map(i => data.slice(i*pageSize , (i+1)*pageSize ));
// ~~ // floors a number
// [...Array(n).keys] => [0, 1, 2, ..., n - 2, n - 1]
var scripts = [
'https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js'
];
scripts.forEach(source => {
var script = document.createElement('script');
script.src = source;
document.getElementsByTagName('head')[0].appendChild(script);
});
@prmichaelsen
prmichaelsen / countSubstr.c
Created September 15, 2017 18:04
with only stdio, count number of occurrences of a substring within a string
#include <stdio.h>
int countSubstr(char string[], char substring[])
{
int count = 0;
size_t i = -1;
while(string[++i])
{
int match = 1;
size_t j = 0;