Skip to content

Instantly share code, notes, and snippets.

View ToeKneeLeg's full-sized avatar

Tony Ka Cheong Lei ToeKneeLeg

View GitHub Profile
@ToeKneeLeg
ToeKneeLeg / searchtempest2
Last active February 2, 2016 23:47
optimizing search problem - searchtempest
// for loop compare with the use of obj to check for duplicate => 400-500ms at 1mil elm
function maxNum(x)
{
var m = -Infinity, i = 0, n = x.length;
for (; i != n; ++i) {
if (x[i] > m) {
m = x[i];
}
@ToeKneeLeg
ToeKneeLeg / sorting.js
Created February 1, 2016 04:17
To test multiple sorting methods with large array
//aaray.sort(with integer compare)
//this will generate an array that contains t number of elements
var inputArr = [];
for (var i=0, t=1000000; i<t; i++) {
inputArr.push(Math.round(Math.random()*t))
}
console.log(inputArr);
var sortFunc = function(x){
@ToeKneeLeg
ToeKneeLeg / array_of_light
Created September 28, 2015 15:59
Week 6 Day 1 Array of Light
var array = [];
var light = function arrayOfLight(number) {
for (i = 0; i < number + 1; i++){
array.push(i);
}
}
light(13);
console.log(array);
#Exercise 1
SELECT e.isbn
FROM editions AS e
INNER JOIN publishers AS p
ON e.publisher_id = p.id
WHERE name = 'Random House'
;
#Exercise 2
@ToeKneeLeg
ToeKneeLeg / contact.rb
Created September 8, 2015 18:08
Refactored version of contact.rb and contact_database.rb
require_relative 'contact_database.rb'
class Contact
attr_accessor :name, :email
def initialize(name, email)
@name = name
@email = email
end
require_relative 'contact_database.rb'
class Contact
attr_accessor :name, :email
def initialize(name, email)
@name = name
@email = email
end
@ToeKneeLeg
ToeKneeLeg / candidates.rb
Created August 28, 2015 02:16
Candidates
require 'active_support/all'
@candidates = [
{
id: 5,
years_of_experience: 4,
github_points: 293,
languages: ['C', 'Ruby', 'Python', 'Clojure'],
date_applied: 5.days.ago.to_date,
age: 26
# Determine whether a string contains a SIN (Social Insurance Number).
# A SIN is 9 digits and we are assuming that they must have dashes in them
def has_sin?(string)
if /\d{3}-\d{3}-\d{3}/.match(string)
return true
end
end
# puts "has_sin? returns true if it has what looks like a SIN"
# puts has_sin?("please don't share this: 234-604-142") == true
@ToeKneeLeg
ToeKneeLeg / benchmark_with_block.rb
Created August 27, 2015 15:48
Benchmark with block
def benchmark
start_time = Time.now
yield
end_time = Time.now
run_time = end_time - start_time
# Your benchmarking code goes here.
end
# Be careful, pasting this into IRB will take a long time to print.
# It's a loooong string. :)
@ToeKneeLeg
ToeKneeLeg / countindex.rb
Created August 27, 2015 01:54
counting with index
def count_index(input)
letters = {}
words = input.split(" ")
conc_word = nil
words.each do |word|
if conc_word == nil
conc_word = word
else
conc_word += word
end