Skip to content

Instantly share code, notes, and snippets.

@Stephenitis
Stephenitis / gist:4951331
Created February 14, 2013 08:26
Exercise: Reverse words Write a method reverse_words which takes a sentence as a string and reverse each word in it.
def reverse_words(str)
a = str.split.collect { |word| word.reverse }
a.join(" ")
end
@Stephenitis
Stephenitis / gist:4951430
Created February 14, 2013 08:55
Exercise: Calculating the mean of an array of numbers
def mean(array)
array.reduce(:+).to_f / array.length
end
@Stephenitis
Stephenitis / Fibonacci Number Detector
Last active December 14, 2015 02:09
Exercise: Fibonacci number Check, if a number i is part of the Fibonacci sequence.
def is_fibonacci?(i)
fib1 = 0
fib2 = 1
while fib1 <= i
fib1, fib2 = fib2, fib1 + fib2
end
if fib1 == i
return true
else
return false
@Stephenitis
Stephenitis / Fizzbuzz
Created February 23, 2013 00:36
super fizzbuzz exercise
def super_fizzbuzz(array)
new_array = [] #create a new array to store the results
#reminder to make sure that two word variables are seperated by a underscore_k?
array.each do |x|
if x % 15 == 0
new_array << "FizzBuzz" #remember stephen the << is the same as saying array.push("FizzBuzz")
elsif x % 5 == 0
new_array << "Buzz"
elsif x % 3 == 0
new_array << "Fizz"
@Stephenitis
Stephenitis / array#mode dev bootcamp
Last active December 14, 2015 07:48
find the mode in a array?
solution #1
array = [1, 1, 1, 2, 3]
#whats going on here??
freq = array.inject(Hash.new(0)) { |h,v| h[v] += 1; h }
arr.sort_by { |v| freq[v] }.last
solution #2
@Stephenitis
Stephenitis / Array#pad Dev bootcamp
Last active December 14, 2015 07:48
Implement Array#pad and Array#pad!. Each method accepts a minimum size (non-negative integer) and an optional pad value as arguments. If the array's length is less than the minimum size, Array#pad should return a new array padded with the pad value up to the minimum size. For example, ruby [1,2,3].pad(5) should return [1,2,3,nil,nil]
class Array
def pad!(min_size, value = nil)
if self.length >= min_size
return self
else
add_amount = (min_size - self.length)
add_amount.times do self.push(value)
return self
@Stephenitis
Stephenitis / RPN Stephen
Last active December 14, 2015 15:08
RPN Exercise
class RPNCalculator
def evaluate(expression) # expression = '1 2 +'
operators = ["+", "-", "*"]
stack = []
array = expression.split(" ") # ["1", "2", "+"]
array.each do |i|
if operators.include?(i)
second_operand = stack.pop #is defining a variable that takes the
first_operand = stack.pop
stack.push(first_operand.send(i, second_operand))
@Stephenitis
Stephenitis / gist:5185093
Created March 18, 2013 04:25
Recursion from chris pine's learning to program
# These are just to make the map # easier for me to read. "M" is # visually more dense than "o". M= 'land'
o= 'water'
world = [[o,o,o,o,o,o,o,o,o,o,o],
[o,o,o,o,M,M,o,o,o,o,o],
[o,o,o,o,o,o,o,o,M,M,o],
[o,o,o,M,o,o,o,o,o,M,o],
[o,o,o,M,o,M,M,o,o,o,o],
[o,o,o,o,M,M,M,M,o,o,o],
[o,o,o,M,M,M,M,M,M,M,o],
[o,o,o,M,M,o,M,M,M,o,o],
@Stephenitis
Stephenitis / regexsocratesexercisee.rb
Last active December 15, 2015 10:08
regex on socrates exercise
# Determine whether a string contains a Social Security number.
def has_ssn?(string)
if string.scan(/\d\d\d.\d\d.\d\d\d\d/)[0] == nil
false
elsif string.scan(/\d\d\d.\d\d.\d\d\d\d/)[0] != nil
true
end
end
# Return the Social Security number from a string.
@Stephenitis
Stephenitis / Coin Flip Recursion.rb
Created March 28, 2013 17:52
How many different ways are there to flip a fair coin 5 times?
def combination(length=5)
return [[]] if length == 0
combination(length-1).collect {|c| [:h] + c } +
combination(length-1).collect {|c| [:t] + c }
end
combination
print "*There are #{combination.length} ways"
#my output