Skip to content

Instantly share code, notes, and snippets.

View scottchiang's full-sized avatar

Scott Chiang scottchiang

View GitHub Profile

Keybase proof

I hereby claim:

  • I am scottchiang on github.
  • I am schiang (https://keybase.io/schiang) on keybase.
  • I have a public key ASB1fx_e0aOB7dm4LZjIMaRIFGi20HVSEjmHm0zIt8uucAo

To claim this, I am signing this object:

@scottchiang
scottchiang / cube_permutations.scala
Last active August 29, 2015 14:00
Find smallest cube for which exactly five permutations of its digits are cube
import scala.collection.mutable
import scala.collection.mutable.MutableList
import scala.collection.immutable.IntMap
import scala.collection.JavaConversions.mapAsScalaMap
import scala.util.control.Breaks._
// The run time for my code is roughly 120ms. I'm pretty sure I'm not understanding something about scala because when
// I implement this in ruby, it runs in around 20ms. I think I can improve performance by changing how I'm
// storing and updating values using HashMap.
@scottchiang
scottchiang / gumroad_design
Created July 10, 2013 06:32
Gumroad balance design question
data model:
seller:
has_many :products
attributes: balance
product:
has_many :purchases
has_many :buyers, :through => :purchase
@scottchiang
scottchiang / pivot_index.rb
Last active December 19, 2015 13:39
Write a method that returns the "pivot" index of a list. We define the pivot index as the index where the sum of the numbers on the left is equal to the sum of the numbers on the right. For instance given [1, 4, 6, 3, 2], the method should return 2, since the sum of the numbers to the left of index 2 is equal to the sum of numbers to the right o…
def pivot_index(ary)
idx = -1
left_sum = 0
right_sum = ary[1..-1].inject(:+)
#can't find pivot if array's length is less than 3
if ary.length > 2
#ignore the first and last element because they only have 1 neighbor
1.upto(ary.length - 2) do |x|
left_sum += ary[x-1]
@scottchiang
scottchiang / fizzbuzz.go
Created April 19, 2013 08:26
FizzBuzz in golang using switch statement
package main
import "fmt"
func main() {
for i := 1; i < 101; i++ {
switch {
case (i % 15) == 0: fmt.Println("FizzBuzz")
case (i % 3) == 0: fmt.Println("Fizz")
case (i % 5) == 0: fmt.Println("Buzz")
@scottchiang
scottchiang / flashcardinator.rb
Created October 25, 2012 17:48 — forked from dbc-challenges/card.rb
FlashCardinator
class Flashcard
attr_accessor :word, :definition
def initialize(word, definition)
@word = word
@definition = definition
end
end
class CardStack
@scottchiang
scottchiang / object_privacy.rb
Created October 11, 2012 04:34
Object privacy
class BankAccount
def initialize(customer_name, type, acct_number)
@customer_name = customer_name
@type = type
@acct_number = acct_number
end
def name
@customer_name
end
@scottchiang
scottchiang / number_in_words.rb
Created October 11, 2012 00:32
Numbers in Words
def in_words(n)
@ones = {1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five", 6 => "six",
7 => "seven", 8 => "eight", 9 => "nine", 10 => "ten", 11 => "eleven",
12 => "twelve", 13 => "thirteen", 14 => "fourteen", 15 => "fifteen",
16 => "sixteen", 17 => "seventeen", 18 => "eighteen", 19 => "nineteen"
}
@twos = {2 => "twenty", 3 => "thirty", 4 => "forty", 5 => "fifty", 6 => "sixty",
7 => "seventy", 8 => "eighty", 9 => "ninety"
}
@scale = {1 => "thousand", 2 => "million", 3 => "billion", 4 => "trillion", 5 => "quadrillion",
@scottchiang
scottchiang / binary_search.rb
Created October 10, 2012 02:55
Solution for Binary Search
def binary_search(obj, array, lower = 0, upper = array.length)
mid_point = ((upper - lower) / 2) + lower
if obj != array[mid_point] && (mid_point == array.length - 1 || mid_point == 0)
return -1
elsif obj == array[mid_point]
return mid_point
elsif obj < array[mid_point]
binary_search(obj, array, lower, mid_point)
elsif obj > array[mid_point]
binary_search(obj, array, mid_point, upper)
@scottchiang
scottchiang / linear_search.rb
Created October 10, 2012 00:44
Solution for Linear Search
#linear search
def linear_search(object, array)
index = 0
while array[index] != nil
if object != array[index]
mismatch += 1
end
if object == array[index]