Skip to content

Instantly share code, notes, and snippets.

View kanglicheng's full-sized avatar
🦜
LLMs ftw

Kang-Li (Stephen) Cheng kanglicheng

🦜
LLMs ftw
View GitHub Profile
@kanglicheng
kanglicheng / hashtable.py
Created May 19, 2017 00:06 — forked from EntilZha/hashtable.py
Hash Table (Open Address) implementation in Python practicing for interviews
from collections import namedtuple
TableEntry = namedtuple('Element', 'hash key value')
class HashTable(object):
DEFAULT_SIZE = 8
EMPTY_VALUE = TableEntry(None, None, None)
DELETED_VALUE = TableEntry(None, None, None)
LOAD_FACTOR = 2 / 3
MIN_FACTOR = 1 / 3
#my coderbyte solns, from mock skype interviews
def RunLength(string):
count =1
ans = ""
for i in range(len(string)-1):
if string[i] == string[i+1]:
count +=1
elif string[i] != string[i+1]:
ans += str(count)
//Q1
//Write a function 'transformFirstAndLast' that takes in an array, and returns an object with:
//1) the first element of the array as the object's key, and
//2) the last element of the array as that key's value.
function transformFirstAndLast(array) {
//your code here
var ans ={}
ans[array[0]]=array[array.length-1]
return ans
// #52
function getLengthOfThreeWords(word1, word2, word3) {
return word1.length+ word2.length+word3.length
}
//#53
/*
Write a function called "joinArrays".
Given two arrays, "joinArrays" returns an array with the elements of "arr1" in order,
followed by the elementsin "arr2".
// #2
/*
Write an "assertArraysEqual" function from scratch.
Assume that the arrays in question contain only scalar values (i.e., simple values like strings or numbers).
Do not use JSON.stringify(), Array.join(), or any other "convert the array to a string so I can compare two strings" type of technique to implement this.
Examples
SUCCESS CASE
var expected = ['b', 'r', 'o', 'k', 'e', 'n'];
var actual = 'broken'.split('');
assertArraysEqual(actual, expected, 'splits string into array of characters');
def convertFracts(lst):
n= len(lst)
ans =[[0]*2 for _ in range(n)]
for i in range(n):
ans[i][0]=lst[i][0]/gcd(lst[i][0], lst[i][1])
ans[i][1] = lst[i][1]/gcd(lst[i][0], lst[i][1])
return ans
def gcd(a, b):
"""Return greatest common divisor using Euclid's Algorithm."""
@kanglicheng
kanglicheng / gcd_and_lcm.py
Created July 24, 2017 22:39
calculating gcd and lcm of two numbers, using reduce for multiple numbers
#using python 3.5
import functools
# standard euclidean algorithm
def gcd(a, b):
while b:
a, b = b, a%b
return a
# with explicit recursion
def gcd_multiple(a, b):

This lesson has slides and exercises for "Filter", "Reduce", and "Reduce Optional"

Filter

Lesson

Filter Slides

Exercises