Skip to content

Instantly share code, notes, and snippets.

View yangshun's full-sized avatar
😎
Ruining websites since 2013

Yangshun Tay yangshun

😎
Ruining websites since 2013
View GitHub Profile
@yangshun
yangshun / shallow-vs-deep-copy.py
Last active December 28, 2015 08:19
Demonstrating the differences between deep and shallow copy in Python
# no copy
a = {1:'a', 2:'b', 3:{4:'c', 5:'d'}}
b = a
b[1] = 'lol'
print(a[1])
b[3][4] = 'haha'
print(a[3][4])
# shallow copy
c = {1:'a', 2:'b', 3:{4:'c', 5:'d'}}
@yangshun
yangshun / DailyJS Tags Sorter
Created February 24, 2014 06:56
This snippet of code can be used to sort the posts within each tag category on the DailyJS site: http://dailyjs.com/tags.html
(function() {
$('.posts').each(function() {
var $postsInCat = $(this).children();
var $sortedCat = $postsInCat.sort(function(a, b) {
var dateA = new Date($(a).children('div').html());
var dateB = new Date($(b).children('div').html());
return dateA < dateB ? 1 : -1;
});
$(this).html('').html($sortedCat);
});
@yangshun
yangshun / MFCC.py
Last active October 12, 2018 07:44
Computes the MFCC (Mel-frequency cepstrum coefficients) of a sound wave
import random
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as plt
import time
import os
from math import *
from numpy import append, zeros
from scipy.io import wavfile
@yangshun
yangshun / Python Variable Scope Examples
Last active August 29, 2015 13:57
Python Variable Scope Examples
a = 1
def foo():
print(a) # Prints 1. Accesses the variable a outside its own scope.
foo()
print(a) # Prints 1. a not modified.
b = 2
def bar():
@yangshun
yangshun / youtube-vimeo-url-parser.js
Last active June 14, 2023 22:27
YouTube Vimeo URL Parser
function parseVideo (url) {
// - Supported YouTube URL formats:
// - http://www.youtube.com/watch?v=My2FRPA3Gf8
// - http://youtu.be/My2FRPA3Gf8
// - https://youtube.googleapis.com/v/My2FRPA3Gf8
// - Supported Vimeo URL formats:
// - http://vimeo.com/25451551
// - http://player.vimeo.com/video/25451551
// - Also supports relative URLs:
// - //player.vimeo.com/video/25451551
@yangshun
yangshun / nus-timetabledatagenerator.js
Created June 30, 2014 04:44
Creating nus_timetable_data.js
var fs = require('fs');
var data = JSON.parse(fs.readFileSync('modules.json'));
var lessonTypes = ['DESIGN LECTURE', 'LABORATORY', 'LECTURE', 'PACKAGED LECTURE',
'PACKAGED TUTORIAL', 'RECITATION', 'SECTIONAL TEACHING',
'SEMINAR-STYLE MODULE CLASS', 'TUTORIAL', 'TUTORIAL TYPE 2',
'TUTORIAL TYPE 3'];
var weeks = ['EVERY WEEK', 'ODD WEEKS', 'EVEN WEEKS'];
var days = ['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY'];
@yangshun
yangshun / projection-points-set.py
Last active April 28, 2019 19:53
Connect the vertices of the cube in CS4243 Assignment 3 so that it makes it easier to visualize and debug the projected image
# Replace section 1.1 of Assignment 3 with the following lines of code
# Final result looks something like this: http://imgur.com/nVCFDPh
def pts_set_2():
def create_intermediate_points(pt1, pt2, granularity):
new_pts = []
vector = np.array([(x[0] - x[1]) for x in zip(pt1, pt2)])
return [(np.array(pt2) + (vector * (float(i)/granularity))) for i in range(1, granularity)]
@yangshun
yangshun / python-sort-stability.py
Last active July 11, 2023 22:33
Sort then reverse vs Sort(reverse=True)
# We want to sort a list by its second element in descending order.
# The example illustrates the difference in the results of different
# process of sorting in descending order.
# Sort in ascending order, then use list reverse
>>> a = [('A', 1), ('C', 5), ('A', 2), ('B', 3), ('B', 5)]
>>> a.sort(key=lambda x: x[1])
>>> print(a)
[('A', 1), ('A', 2), ('B', 3), ('C', 5), ('B', 5)]
>>> a.reverse()
@yangshun
yangshun / scope-example.py
Created November 26, 2014 03:51
Demonstrate local, nonlocal, and global scope of variables
def scope_test():
def do_local():
spam = "local spam"
def do_nonlocal():
nonlocal spam
spam = "nonlocal spam"
def do_global():
global spam
spam = "global spam"
spam = "test spam"
@yangshun
yangshun / cs1020-grading.sh
Last active August 29, 2015 14:13
CS1020 Codes Autograder
#!/bin/bash
# Assumes a directory structure of:
# dir
# |-- skeleton
# | |-- testFile.java
# | +-- grade.sh (this file)
# |
# +-- testdata
# |-- input