This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_fibonacci?(i) | |
ary=[] | |
(1..100).inject([0]) {|ary, x| x>1? ary<< ary[-1]+ary[-2] : ary << 1} | |
ary.include?(i) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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:" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#linear search | |
def linear_search(object, array) | |
index = 0 | |
while array[index] != nil | |
if object != array[index] | |
mismatch += 1 | |
end | |
if object == array[index] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer