Skip to content

Instantly share code, notes, and snippets.

View ScottGo's full-sized avatar

scott golas ScottGo

View GitHub Profile
@ScottGo
ScottGo / 20130407_calc_mean_of_an_array
Last active December 15, 2015 22:29
Calculating the mean of an array of numbers
describe 'mean' do
let(:array_1) { [1, 2, 3, 4, 5, 5, 7] }
let(:array_2) { [4, 4, 5, 5, 6, 6, 6, 7] }
it "is defined as a method" do
defined?(mean).should eq 'method'
end
it "requires a single argument" do
method(:mean).arity.should eq 1
@ScottGo
ScottGo / 20130406_Print out a pretty right triangle.rb
Last active December 15, 2015 21:49
Exercise: Print out a pretty right triangle
# print_triangle(rows) prints out a right triangle of +rows+ rows consisting
# of * characters
#
# +rows+ is an integer
#
# For example, print_triangle(4) should print out the following:
# *
# **
# ***
# ****
@ScottGo
ScottGo / 20130406_reverse_string.rb
Last active December 15, 2015 21:49
Write a method reverse_words which takes a sentence as a string and reverse each word in it.
#from stackoverflow, want to see if/how it works, not taking credit or will use if it does
def reverse_words(string) # Method reverse_string with parameter 'string'.
loop = string.length # int loop is equal to the string's length.
word = '' # This is what we will use to output the reversed word.
while loop > 0 # while loop is greater than 0, subtract loop by 1 and add the string's index of loop to 'word'.
loop -= 1 # Subtract 1 from loop.
word += string[loop] # Add the index with the int loop to word.
end # End while loop.
return word # Return the reversed word.
end # End the method.
@ScottGo
ScottGo / 20130405_times_table.rb
Last active December 15, 2015 21:18
20130405_printing out a times table
def times_table(rows)
first_row = []
int = 5
1.upto(5) do |i|
first_row.push(i)
end
first_row
@ScottGo
ScottGo / 20130305_Fizzbuzzed exercise.rb
Last active December 15, 2015 20:49
20130305_Fizzbuzzed exercise.rb
def super_fizzbuzz(array)
fizzbuzzed = []
array.each do |num|
if num % 3 == 0 && num % 5 == 0 # return "FizzBuzz"
fizzbuzzed.push("FizzBuzz")
elseif num % 3 == 0 # return "Fizz"
fizzbuzzed.push("Fizz")
elseif num % 5 == 0 # return "Buzz"
fizzbuzzed.push("Buzz")
# shortest_string is a method that takes an array of strings as its input
# and returns the shortest string
# +array+ is an array of strings
# shortest_string(array) should return the shortest string in +array+
# If +array+ is empty the method should return nil
# array.sort.first
def shortest_string(array)
new_count = [ ]