Skip to content

Instantly share code, notes, and snippets.

View scottchiang's full-sized avatar

Scott Chiang scottchiang

View GitHub Profile
@scottchiang
scottchiang / ternary_operator.rb
Created September 16, 2012 04:12
Ternary Operators
=begin
I've seen ternary operators before but for some reason, I didn't appreciate it (or try to understand it) until now. Ternary operators can make if statements look a lot nicer (and shorter).
Here's an example:
=end
if x % 15 == 0
return "FizzBuzz"
else
return x
@scottchiang
scottchiang / fibonacci.rb
Created September 27, 2012 03:45
A method to determine if a given number is a part of the Fibonacci Sequence
def is_fibonacci?(i)
ary=[]
(1..100).inject([0]) {|ary, x| x>1? ary<< ary[-1]+ary[-2] : ary << 1}
ary.include?(i)
end
rvm rubies
ruby-1.8.7-p370 [ i686 ]
ruby-1.9.2-p320 [ x86_64 ]
=* ruby-1.9.3-p194 [ x86_64 ]
# => - current
# =* - current && default
# * - default
@scottchiang
scottchiang / 99bottles.rb
Created October 2, 2012 04:57
99 bottles
def ninety_nine_bottles(num)
while num>=0
if num == 0
puts "No more bottles of beer on the wall :/"
break
else
puts "#{num} bottles of beer on the wall"
puts "#{num} bottles of beer"
puts "Take one down, pass it around"
puts "#{num-1} bottles of beer on the wall"
@scottchiang
scottchiang / pseudocode
Created October 2, 2012 21:50
pseudocode
Script: CONVERT EVERY OTHER LETTER OF A SENTENCE TO CAPITAL LETTERS
GET a sentence from user input
IF the sentence starts with a capital letter, don't change it
ELSE convert every other letter to capital letters
ENDIF
PRINT the formatted sentence
@scottchiang
scottchiang / richard_pseudocode.rb
Created October 3, 2012 02:25
Richard's pseudocode
# Script: Covert sentences
#
# GET a sentence from user input.
# IF the sentence length is greater than 20, randomize the sentence
# ELSE take the 2nd letter of each word and change it to the letter P
# ENDIF
# PRINT the new sentence
def pseudocode
puts "Type in a sentence:"
@scottchiang
scottchiang / convert_to_hash.rb
Created October 5, 2012 20:58
convert nested array to hash
def convert_to_hash
data_table = []
#data_table[0] = ["Number", "Name", "Position", "Points per Game"]
data_table[0] = ["12", "Joe Schmo", "Center", "[14, 32, 7, 0, 23]"]
data_table[1] = ["9", "Ms. Buckets", "Point Guard", "[19, 0, 11, 22, 0]"]
data_table[2] = ["31", "Harvey Kay", "Shooting Guard", "[0, 30, 16, 0, 25]"]
data_table[3] = ["18", "Sally Talls", "Power Forward", "[18, 29, 26, 31, 19]"]
data_table[4] = ["22", "MK BiBoux", "Small Forward", "[11, 0, 23, 17, 0]"]
hash_data_table = []
@scottchiang
scottchiang / translate_pesudocode.rb
Created October 5, 2012 22:22
Translate Pseudocode
# SCRIPT: PICK UP GOLD
#
# Iteration One: PICK UP THE GOLD
#
# READ grid containing position of gold and rock
# SET starting position to 0,0
# SET an empty sachel
# WHILE y position is less than height of grid
# IF current position has gold
# THEN add gold to sachel and set content of current position to rock
@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]
@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)