Skip to content

Instantly share code, notes, and snippets.

View emberlzhang's full-sized avatar

Ember Zhang emberlzhang

View GitHub Profile
@emberlzhang
emberlzhang / fizzbuzz
Created July 5, 2013 20:24
FizzBuzz in Ruby: Write a program that prints out the numbers 1 to 100 (inclusive). If the number is divisible by 3, print Fizz instead of the number. If it's divisible by 5, print Buzz. If it's divisible by both 3 and 5, print FizzBuzz.
numbers = Array(1..100)
numbers.each do |num|
if num % 15 == 0
puts "FizzBuzz"
elsif num % 5 == 0
puts "Buzz"
elsif num % 3 == 0
puts "Fizz"
else
puts num
@emberlzhang
emberlzhang / easy_lev.rb
Created March 20, 2013 20:56
Need to install gem 'levenshtein-ffi'
require 'levenshtein'
def sorted_lev(word, werd_collection)
lev_result = fancy_lev(word, werd_collection)
lev_result.sort! do |a,b|
a[1] <=> b[1]
end
end
def fancy_lev(word, werd_collection)
@emberlzhang
emberlzhang / todos_3.rb
Created October 22, 2012 19:19
Solution for Ruby Todos
#!/usr/bin/env ruby
require "yaml"
require 'term/ansicolor'
class String
include Term::ANSIColor
end
class List
def initialize
@emberlzhang
emberlzhang / todo_1.rb
Created October 18, 2012 02:34
Solution to Ruby Todos 1.0: Core Features
require "./todo_item.rb"
class List
attr_reader :filename
attr_accessor :list
def initialize(filename)
@filename = filename
@list = []
end
end
@emberlzhang
emberlzhang / gist:3886693
Created October 14, 2012 00:09
Solution to Simple Matter of Inheritance
class Animal
attr_accessor :awake_at
def initialize
@awake_at = "day"
end
def winged?
false
end
@emberlzhang
emberlzhang / gist:3862557
Created October 10, 2012 01:15
Solution for Linear Search
def linear_search(obj, array) #3, [4,5,1,2,3] => 4
index = 0
for elem in array
if elem == obj
break
else
index += 1
end
end
index
@emberlzhang
emberlzhang / linear_search.rb
Created October 10, 2012 00:45 — forked from scottchiang/linear_search.rb
Solution for Linear Search with Scott
#linear search
def linear_search(object, array)
index = 0
mismatch = 0
while index < array.length
if object != array[index]
mismatch += 1
end
# Put your answers here!
# Put your answers here!
class Array
#changes the array
def pad!(min_size, value = nil)
if min_size <= self.length
return self.clone
elsif min_size > self.length
(min_size - self.length).times do
self << value
end
return self.clone