Skip to content

Instantly share code, notes, and snippets.

View jergagon1's full-sized avatar

Jeremy Gagon jergagon1

View GitHub Profile
@jergagon1
jergagon1 / .README.md
Last active August 29, 2015 14:24
Sieve of Eratosthenes

Sieve of Eratosthenes

A Ruby method to find all prime numbers within a range using the Sieve of Eratosthenes.

Input: An integer defining upper boundary of range

Output: An array of the prime numbers within the range

Steps:

  • Create an array of values 2..n where n is upper boundary of range
  • Outer Loop:
  • starting from the first prime number, 2, loop through the values of the array
  • for any nil values, skip to the next value
@jergagon1
jergagon1 / linear_search.rb
Created July 10, 2015 00:36
Ruby study of available linear search approaches
# Ruby linear search method
# Input: an array and a desired value
# the array is not necessarily sorted
# Output: the index of the desired value or nil if the desired value does not occur
def linear_search input_array, desired_value
current_index = 0
while current_index < input_array.length
if input_array[current_index] == desired_value
return current_index
@jergagon1
jergagon1 / binary_search.rb
Created July 9, 2015 23:58
Ruby binary search method
# Ruby binary search method
# Input: a sorted array and the desired value
# Output: the index of the desired value or nil if it does not occur
# Edge Cases:
# Value is within upper and lower range of array but does not occur
# Value is outside upper range of array
# Value is outside lower range of array
# Input array is empty
# input array is not sorted -> to test would have to sort array and check equality
# for efficiency, assuming input sorted
@jergagon1
jergagon1 / bubble_sort.rb
Created July 9, 2015 23:54
Ruby bubble sort method
def bubble_sort num_array
array_sorted = false
current_index = 0
index_to_compare = 1
until array_sorted
change_this_loop = false
(num_array.length-1).times do |counter|
first_value = num_array[current_index + counter]
second_value = num_array[index_to_compare + counter]
if first_value > second_value
@jergagon1
jergagon1 / palindrome.rb
Created July 9, 2015 23:53
Ruby method to check if a string is a palindrome
# Ruby method to detect if input is palindrome
# Input: string
# Output: boolean
# Edge cases
# Spaces and punctuation -> scrub input with helper method
# Number input -> convert to string in scrub string
def scrub_string str
str = str.to_s if !str.is_a?String
str = str.gsub(/\W|\_/, "").downcase
@jergagon1
jergagon1 / fizzbuzz.rb
Created July 6, 2015 20:31
Ruby FizzBuzz Solution
# Ruby Method FizzBuzz
# Input: An optional array of numbers, if not provided use array of numbers in range 1 to 100
# Output: An array of numbers with the following exceptions
# Any number evenly divisible by 3 and 5 is replaced with string "FizzBuzz"
# Any number evenly divisble by 3 and not evenly divisible by 5 is replaced with string "Fizz"
# Any number evenly divisble by 5 and not evenly divisible by 3 is replaced with string "Buzz"
def fizzbuzz (num_array = (1..100).to_a)
results = num_array.map do |num|
if num % 3 == 0 && num % 5 == 0
@jergagon1
jergagon1 / map.rb
Created July 2, 2015 23:02
Monkey Patch an Array Map Method in Ruby
# Monkey patch a map and map! method
class Array
def my_map
mapped_array = []
self.each { |element| mapped_array.push yield element }
mapped_array
end
def my_map!
self.length.times do |i|
@jergagon1
jergagon1 / application.js
Created June 29, 2015 18:40
Simple OOJS Dice Roller
$(document).ready(function() {
/********************* Model **********************/
var Die = function () {
this.value = 0;
};
Die.prototype.roll = function() {
this.value = Math.floor((Math.random()*6)+1);
@jergagon1
jergagon1 / roman_numerals.rb
Created June 22, 2015 15:41
Ruby Method to convert integers to Roman numerals
NUMERAL_HASH = {
1 => "I",
5 => "V",
10 => "X",
50 => "L",
100 => "C",
500 => "D",
1000 => "M"
}
@jergagon1
jergagon1 / selection_sort.rb
Created June 16, 2015 00:03
Ruby manual selection sort method
# Manual selection sort method in ruby
# Big O time = O(n**2)
# Big O space = O(1)
##############################################
# 1st pass: nested while loops
##############################################
def selection_sort num_array
i = 0