Skip to content

Instantly share code, notes, and snippets.

View fireflypulse's full-sized avatar

Michael Ingle fireflypulse

View GitHub Profile
@fireflypulse
fireflypulse / boggle_class_from_methods.rb
Last active January 1, 2016 17:29 — forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
phase 0 unit 2 week 1 boggle class challenge
class BoggleBoard
def initialize(board)
@board = board
end
def create_word(*coords)
coords.map { |coord| @board[coord.first][coord.last]}.join("")
end
#population density is number of people per square mile as of 2012
#this data is updated every year with estimates from a 10 year census
STATE_DATA = { "Alabama" => {population_density: 94.65, population: 4822023, region: 5, regional_spread: 3},
"Alaska" => {population_density: 1.26, population: 731449, region: 10, regional_spread: 9},
"Arizona" => {population_density: 57.05, population: 6553255, region: 8, regional_spread: 8},
"Arkansas" => {population_density: 56.43, population: 2949131, region: 7, regional_spread: 5},
"California" => {population_density: 244.2, population: 38041430, region: 9, regional_spread: 8},
"Colorado" => {population_density: 49.33, population: 5187582, region: 8, regional_spread: 6},
"Connecticut" => {population_density: 741.4, population: 3590347, region: 1, regional_spread: 2},
# When you are finished with this challenge you should be able to:
# Explain what "require_relative" does and why you would use it
# Demonstrate how to iterate through a hash
# Easily recognize and refactor repetitive code
# Objectives
# 1) Run the code. Look at the output. Look at the input (it's in the other file). Explain what the program is doing.
# 2) Write a comment explaining the require_relative statement below
# 3) Comment each method and define it's responsibility
# Input: an integer >= 0
# Output: True/False
1 2 3 4 5 6 7 8
0 1 1 2 3 5 8 13<< 21
9
def fibonacci( n )
return n if ( 0..1 ).include? n
( fibonacci( n - 1 ) + fibonacci( n - 2 ) )
@fireflypulse
fireflypulse / gist:7780062
Last active December 30, 2015 04:59
Array total
def total(array)
sum = 0
#create variable sum
#iterate through the array and add each element to sum
#output sum
array.each do |arg|
sum += arg
def mode(array)
# sort the original array, `array`
# create array of at least 1 in length: return this
frequencies = Hash.new(0)
array.each do |arg|
# add to frequencies
# if key already exists, just add 1 to value
# if not, create key, and set value to 1
end