Skip to content

Instantly share code, notes, and snippets.

View cyyeh's full-sized avatar
👋
Focusing

Chih-Yu Yeh cyyeh

👋
Focusing
View GitHub Profile
@cyyeh
cyyeh / swift-basic-operators.swift
Last active February 27, 2019 11:40
Swift Basic Operators
/*
Referenced from Stanford CS193p
#: important
##: very important
Basic Operators
- Terminology
- Assignment Operator
- Arithmetic Operators
@cyyeh
cyyeh / swift-basics.swift
Last active February 27, 2019 11:33
Swift Basics
/*
Referenced from Stanford CS193p
#: important
##: very important
The Basics
- Constants and Variables
- Comments
- Semicolons
@cyyeh
cyyeh / two_linear_time_algorithms.py
Last active December 20, 2018 10:23
comparing_time_complexity
# Uses python3
import sys
def get_majority_element_linear_A(a, left, right):
elements_dict = {}
majority_threshold = len(a) // 2
result = -1 # -1: no majority, 0: majority
# record all elements into dictionary and test if it's the majority element
start = time.time()
@cyyeh
cyyeh / max_binary_heap.py
Created September 9, 2018 12:56
max_binary_heap.py
class MaxBinaryHeap:
def __init__(self, values=[]):
self.values = values
self.build_max_heap()
def max(self):
"""return first element in the array"""
if len(self.values) > 0:
return self.value[0]
else:
@cyyeh
cyyeh / max_binary_heap.py
Created September 6, 2018 04:56
max_binary_heap.py
class MaxBinaryHeap:
def __init__(self, values=[]):
self.values = values
self.build_max_heap()
def max(self):
"""return first element in the array"""
if len(self.values) > 0:
return self.value[0]
else:
04f30a2483859caa3e70e3c1f7cfa6bd7ab4da39a5d0af26774e9d509ab90a642841b4693464967e225e1713ce0142f46f02bfa844c8888ce0ec0e570dc3e85bd0
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.20.1/vis.min.css" />
<link rel="stylesheet" href="index.css">
<style id="jsbin-css">
html, body {
#reference: https://en.wikipedia.org/wiki/Zebra_Puzzle
# keypoint in python here:
#1. generator expression
#2. *args to function calls
#3. itertools module
#4. is (if two variables point to the same object)
#5. rearrage for and if clause to stop program earlier (if not this program may run estimately an hour)
#5. time module
import itertools
class HashTable:
def __init__(self):
self.size = 11
self.slots= [None] * self.size
self.data = [None] * self.size
def put(self, key, data):
hashvalue = self.hashfunction(key, len(self.slots))
if self.slots[hashvalue] == None:
# The Three Laws of Recursion
# 1. A recursive algorithm must have a base case.
# 2. A recursive algorithm must change its state and move toward the base case.
# 3. A recursive algorithm must call itself, recursively.
import turtle
myTurtle = turtle.Turtle()
myWin = turtle.Screen()