Skip to content

Instantly share code, notes, and snippets.

View jeremiahbiard's full-sized avatar

Jeremiah Biard jeremiahbiard

View GitHub Profile
@jeremiahbiard
jeremiahbiard / gist:7d9e64149c87d876d95c
Created June 11, 2015 03:35
Map/Filter chaining javascript
return newReleases.filter(function(movie) {
return (movie.rating === 5.0);
}).map(function(movie) {
return movie.id;
});
@jeremiahbiard
jeremiahbiard / Symmetric Difference Bonfire
Created June 15, 2015 20:51
Solution to the symmetric difference bonfire. I love functional programming.
function complement(A, B) {
return A.filter(function(elem) { return B.indexOf(elem) == -1; });
}
function unique(arr) {
return arr.filter(function(elem, pos) {
return arr.indexOf(elem) == pos;
});
}
@jeremiahbiard
jeremiahbiard / memoized factorial function
Created June 24, 2015 17:40
memoized factorial function
var f = [];
function factorial (n) {
if (n == 0 || n == 1)
return 1;
if (f[n] > 0)
return f[n];
return f[n] = factorial(n-1) * n;
}
int fast_pow(long long base, long long n,long long M)
{
if(n==0)
return 1;
if(n==1)
return base;
long long halfn=fast_pow(base,n/2,M);
if(n%2==0)
return ( halfn * halfn ) % M;
else
Beginner: Linked List, Stack, Queue, Binary Search Tree.
Intermediate: Heap, Priority Queue, Huffman Tree, Union Find, Tries, Hash Table, Tree Map.
Proficient: Segment Tree, Binary Indexed Tree, Suffix Array, Sparse Table, Lowest Common Ancestor, Range Tree.
Expert: Suffix Automaton, Suffix Tree, Heavy-Light Decomposition, Treap, Aho-Corasick, K-d tree, Link-Cut Tree, Splay Tree, Palindromic Tree, Rope, Dancing Links, Radix Tree, Dynamic Suffix Array.
@jeremiahbiard
jeremiahbiard / polygons.cpp
Created July 17, 2015 23:05
polygons solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <map>
using namespace std;
long long degree(long long a, long long k, long long p) {
@jeremiahbiard
jeremiahbiard / polygons2
Last active August 29, 2015 14:25
More polygons
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <map>
using namespace std;
long long degree(long long a, long long k, long long p) {
@jeremiahbiard
jeremiahbiard / Hint.md
Last active August 29, 2015 14:27 — forked from yuvipanda/Hint.md
Polygon Diagonals Solution (InterviewStreet CodeSprint Fall 2011)

`The required answer is : 1 / (n + k) * n-choose-r(n - 3,k) * n-choose-r(n + k, n - 1).

While I am not aware of a simple way to prove this which I can describe here, here's some practical advice on how such problems can be approached. It helps to write down a simpler solution and searching Google or OEIS (Online Encyclopedia of Integer Sequences) for small inputs like k = 3 and N = 1,2,...10. This usually gives you a formula describing the sequence, and possibly generalizations for the same.

Computing the answer using the above formula requires calculating the power of MOD (1e6 + 3) in the numerator and the demonimator. If the power of MOD is bigger in the numerator, the answer is 0. Otherwise, we can calculate all the factorials with the powers of MOD cast out. For example, if MOD = 3 and N = 11, we would calculate the product 1 * 2 * 3 / 3 * 4 * 5 * (6 / 3) * 7 * 8 * (9 / 9) * 10 * 11. This can be done easily in O(MOD). Once we have that, we can multiply the values in the numerator and mltiply with the mo

start =
expression
validchar
= [0-9a-zA-Z_?!+\-=@#$%^&*/.]
_ =
" "*
list