Skip to content

Instantly share code, notes, and snippets.

View danielhao5's full-sized avatar
💭
Learn new things everyday.

Daniel Hao danielhao5

💭
Learn new things everyday.
View GitHub Profile
@mattdeboard
mattdeboard / kmp.py
Created March 17, 2012 00:04
Knuth-Morris-Pratt algorithm
def kmp_match(seq, subseq):
"""
Implementation of the Knuth-Morris-Pratt (KMP) algorithm in Python.
This algorithm finds valid shifts to locate subsequence `subseq` in
sequence `seq`.
"""
n = len(seq)
m = len(subseq)
@kachayev
kachayev / uf.py
Created July 9, 2013 10:36
Union-Find data structure implementation with examples (Kruskala MST, Tarjan LCA etc)
# Union-Find (Disjoint Set Union)
# more information on wiki:
# http://en.wikipedia.org/wiki/Disjoint-set_data_structure
class UF(object):
def __init__(self, size):
self.p = [None]*size
self.rank = [1]*size
@kylebgorman
kylebgorman / triangle.py
Created December 21, 2013 01:39
Triangular (square) matrix class for Python, using only half as much memory. Supports decent portions of what you'd expect for a numpy object
#!/usr/bin/env python -O
#
# Copyright (c) 2013 Kyle Gorman
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
anonymous
anonymous / gist:8733039
Created January 31, 2014 14:29
from collections import defaultdict
from itertools import chain
from banyan import SortedDict, OverlappingIntervalsUpdator
class Rectangle(object):
def __init__(self, left, bottom, right, top):
self.left = left
self.top = top
class Day(object):
def __init__(self, visits, contacts):
self.visits = visits
self.contacts = contacts
def __add__(self, other):
total_visits = self.visits + other.visits
total_contacts = self.contacts + other.contacts
return Day(total_visits, total_contacts)
@akueisara
akueisara / 1. Graph Basics.md
Last active June 2, 2020 15:07
Week 1 of Coursera's Algorithms on Graphs

Graph Basics

Examples

  • Internet: Webpages connected by links.
  • Maps: Intersections connected by roads.
  • Social Networks: People connected by friendships.
  • Configuration Spaces: Possible configurations connected by motions.

Definition

Graph

An (undirected) Graph is a collection V of vertices, and a collection E of edges each of which connects a pair of vertices.

@folksilva
folksilva / problem1.py
Last active February 28, 2022 13:39
Daily Coding Problem: Problem #1
"""
This problem was asked by Google.
Given a stack of N elements, interleave the first half of the stack with the second half reversed using only one other queue. This should be done in-place.
Recall that you can only push or pop from a stack, and enqueue or dequeue from a queue.
For example, if the stack is [1, 2, 3, 4, 5], it should become [1, 5, 2, 4, 3]. If the stack is [1, 2, 3, 4], it should become [1, 4, 2, 3].
Hint: Try working backwords from the end state.
@rajatdiptabiswas
rajatdiptabiswas / Binary Indexed Tree.py
Last active June 22, 2023 05:20
Implementation of Binary Indexed Tree/Fenwick Tree in Python
#!/usr/bin/env python3
"""
Binary Indexed Tree / Fenwick Tree
https://www.hackerearth.com/practice/notes/binary-indexed-tree-made-easy-2/
https://www.topcoder.com/community/data-science/data-science-tutorials/binary-indexed-trees/
https://www.youtube.com/watch?v=v_wj_mOAlig
https://www.youtube.com/watch?v=kPaJfAUwViY
"""
@georgeth
georgeth / recover_source_code.md
Created August 8, 2018 14:44 — forked from simonw/recover_source_code.md
How to recover lost Python source code if it's still resident in-memory

How to recover lost Python source code if it's still resident in-memory

I screwed up using git ("git checkout --" on the wrong file) and managed to delete the code I had just written... but it was still running in a process in a docker container. Here's how I got it back, using https://pypi.python.org/pypi/pyrasite/ and https://pypi.python.org/pypi/uncompyle6

Attach a shell to the docker container

Install GDB (needed by pyrasite)

apt-get update && apt-get install gdb
@simecek
simecek / reprlibdemo.ipynb
Created November 4, 2018 22:13
ReprlibDemo.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.