Skip to content

Instantly share code, notes, and snippets.

Avatar
🤨
I'm nitpicky!

Yangshun Tay yangshun

🤨
I'm nitpicky!
View GitHub Profile
@yangshun
yangshun / shallow-vs-deep-copy.py
Last active Dec 28, 2015
Demonstrating the differences between deep and shallow copy in Python
View shallow-vs-deep-copy.py
# 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 Feb 24, 2014
This snippet of code can be used to sort the posts within each tag category on the DailyJS site: http://dailyjs.com/tags.html
View DailyJS Tags Sorter
(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 Oct 12, 2018
Computes the MFCC (Mel-frequency cepstrum coefficients) of a sound wave
View MFCC.py
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 Aug 29, 2015
Python Variable Scope Examples
View 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 Dec 31, 2020
YouTube Vimeo URL Parser
View youtube-vimeo-url-parser.js
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 Jun 30, 2014
Creating nus_timetable_data.js
View nus-timetabledatagenerator.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 Apr 28, 2019
Connect the vertices of the cube in CS4243 Assignment 3 so that it makes it easier to visualize and debug the projected image
View projection-points-set.py
# 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 Feb 11, 2020
Sort then reverse vs Sort(reverse=True)
View python-sort-stability.py
# 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 Nov 26, 2014
Demonstrate local, nonlocal, and global scope of variables
View scope-example.py
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 Aug 29, 2015
CS1020 Codes Autograder
View cs1020-grading.sh
#!/bin/bash
# Assumes a directory structure of:
# dir
# |-- skeleton
# | |-- testFile.java
# | +-- grade.sh (this file)
# |
# +-- testdata
# |-- input
You can’t perform that action at this time.