Skip to content

Instantly share code, notes, and snippets.

@jin
jin / check_balanced_brackets.rb
Created October 13, 2014 16:05
Check for matching brackets in a string
def check_string(str)
str.empty? || is_balanced(0, str)
end
def is_balanced(num_left_brackets, str)
if str.head == "["
is_balanced(num_left_brackets + 1, str.tail)
elsif str.head == "]"
return false if num_left_brackets == 0
is_balanced(num_left_brackets - 1, str.tail)
@jin
jin / ruby_reduce.rb
Last active August 29, 2015 14:07
ruby Array#reduce with an initial value
class Array
def reduce(func, init)
if self.length < func.arity
res = nil
elsif self.length == func.arity
res = func.call(*self)
else
res = func.call(self[0], self[1..-1].reduce(func, nil))
end
init.nil? ? res : func.call(init, res)
#!/usr/bin/env ruby
def sum_of_differences(arr)
total = 0
arr.each_cons(2) do |x, y|
total += (x - y).abs
end
total
end
@jin
jin / functional_ruby.rb
Created November 21, 2014 18:45
Functional Ruby: implementing commonly used functions with lambda objects
# Regular Ruby methods, can't pass them around like function objects
def length(xs)
return 0 if xs.empty?
1 + length(xs[1..-1])
end
def map(xs, process = -> (x) { x })
return [] if xs.empty?
[process.call(xs.first)] + map(xs[1..-1], process)
@jin
jin / custom_comparators.rb
Last active August 29, 2015 14:10
Custom sort comparators in Ruby
def sort hash
hash.keys.map(&:to_s).sort(& -> (a, b) { a.length <=> b.length })
end
p sort ({ abc: 'hello', 'another_key' => 123, 4567 => 'third' })
@jin
jin / linked_list.rb
Created December 17, 2014 21:13
Implementing Linked List with structs
Node = Struct.new(:val, :next_node) do
def length
return 1 if next_node.nil?
1 + next_node.length
end
def to_s
self.to_a.join(", ")
end
@jin
jin / socks-proxy-iphone.sh
Last active August 29, 2015 14:14
Share jailbroken iPhone's WiFi connection from USB tether to a Mac.
# A USB tether always make use of the 3G/4G connection of your iPhone.
# You can override this config by buying MyWi 8.0 for 20 bucks or
# do the following. :-)
# Skip to step 4 if you already have ssh set up.
# 1) Install OpenSSH from Cydia
# 2) ssh into your phone. default password is `alpine`.
# I use CCSystemInfo to access my iPhone's cellular IP address easily.
@jin
jin / keybase.md
Last active August 29, 2015 14:14

Keybase proof

I hereby claim:

  • I am jin on github.
  • I am jin (https://keybase.io/jin) on keybase.
  • I have a public key whose fingerprint is 124B CC2E 008C 650E FF3D EC71 47D0 E655 F065 B791

To claim this, I am signing this object:

@jin
jin / vivian_balakrishnan_sudoku.js
Last active February 14, 2016 03:59
@VivianBalakrishnan's translation of Lee Hsien Loong's Sudoku solver.
/****************************************************************
* Sudoku Solver
*
* Translated into Javascript from original code in C++
* by Lee Hsien Loong
* The MIT License (MIT)
* Copyright (c) 2015 Vivian Balakrishnan
*
*
****************************************************************/
require 'nokogiri'
require 'httparty'
require 'concurrent'
require 'pp'
class HWZRequest
def initialize
@host = "http://forums.hardwarezone.com.sg"
end