Skip to content

Instantly share code, notes, and snippets.

View akueisara's full-sized avatar
🎹

Kuei-Jung Hu akueisara

🎹
View GitHub Profile
@akueisara
akueisara / deckofcard.py
Created July 29, 2016 19:46
deckofcard
#Create a deck of cards
class Card(object):
suits = ["spade", "heart", "club", "diamond"]
ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
def _init_(self, suit, rank):
self.suit = suit
self.rank = rank
@akueisara
akueisara / count_words.py
Last active October 9, 2016 04:42
Count words in Python
"""Count words."""
def count_words(s, n):
"""Return the n most frequently occuring words in s."""
# TODO: Count the number of occurences of each word in s
words = s.split()
word_occurences = [words.count(i) for i in words]
word_map = dict(zip(words, word_occurences))
# TODO: Sort the occurences in descending order (alphabetically in case of ties)
@akueisara
akueisara / BinaryTree.java
Last active November 25, 2016 09:49
Coursera - Data structures: Measuring and Optimizing Performance - Week 4
import java.util.LinkedList;
import java.util.Queue;
public class BinaryTree<E extends Comparable<E>> {
TreeNode<E> root;
public BinaryTree() {
root = null;
}
def isLeapYear(year):
# From Wikipedia http://en.wikipedia.org/wiki/Leap_year
"""
if (year is not exactly divisible by 4) then (it is a common year)
else
if (year is not exactly divisible by 100) then (it is a leap year)
else
if (year is not exactly divisible by 400) then (it is a common year)
else (it is a leap year)
"""
@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.

@akueisara
akueisara / Directed Graphs.md
Last active June 23, 2017 02:38
Week 2 of Coursera's Algorithms on Graphs

Directed Graphs

1. Directed Acyclic Graphs

(1) Directed Graphs

Definition

A directed graph is a graph where each edge has a start vertex and an end vertex.

(2) Directed DFS

  • Only follow directed edges.
  • explore(v) finds all vertices reachable from v.
  • Can still compute pre- and post- orderings.
@akueisara
akueisara / BFS-Algorithm.md
Created January 2, 2017 04:57
Week 3 of Coursera's Advanced Data Structures in Java

BFS: Algorithm

BFS(S, G):
  Initialize: queue, visited HashSet and parent HashMap
  Enqueue S onto the queue and add to visited
  while stack is not empty:
    dequeue node curr from front of queue
    if curr == G return parent map
    for each of curr's neighbors, n, not in visited set:
      add n to visited set
@akueisara
akueisara / Most Direct Route.md
Last active August 10, 2017 11:03
Week 3 of Coursera's Algorithms on Graphs

Most Direct Route

1. Most Direct Route

(1) Paths and lengths

Length of the path L(P) is the number of edges in the path.

(2) Distances

The distance between two vertices is the length of the shortest path between them.

2. Breadth-First Search

@akueisara
akueisara / A* algorithm.md
Last active August 10, 2017 11:03
Week 4 of Coursera's Advanced Data Structures in Java

Dijkstra's Algorithm

PQ ordering is based on: g(n): the distance (cost) from start vertex to vertex n.

A* algorithm

PQ ordering is based on: g(n): the distance (cost) from start vertex to vertex n AND h(n): the heuristic estimated cost from vertex n to goal vertex.
f(n) = g(n) + h(n)

@akueisara
akueisara / 1. Fastest Route.md
Last active April 5, 2022 21:42
Week 4 of Coursera's Algorithms on Graphs

1. Fastest Route

1-1. Fastest Route

What is the fastest route to get home from work?

1-2. Naive Algorithm

Observation

Any subpath of an optimal path is also optimal.

Edge Relaxation

  • dist[v] will be an upper bound on the actual distance from S to v.