- 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
This file contains hidden or 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
# 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 |
This file contains hidden or 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
# 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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
# 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 |
This file contains hidden or 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
# 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 |
This file contains hidden or 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
# 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| |
This file contains hidden or 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
$(document).ready(function() { | |
/********************* Model **********************/ | |
var Die = function () { | |
this.value = 0; | |
}; | |
Die.prototype.roll = function() { | |
this.value = Math.floor((Math.random()*6)+1); |
This file contains hidden or 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
NUMERAL_HASH = { | |
1 => "I", | |
5 => "V", | |
10 => "X", | |
50 => "L", | |
100 => "C", | |
500 => "D", | |
1000 => "M" | |
} |
This file contains hidden or 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
# 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 |
NewerOlder