This file contains 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
# Currently works with seven letter words only | |
phone_number = "18002435377" | |
last_seven_digits = phone_number.to_s[-7..-1].split(//).map(&:to_i) | |
def return_letters(digit) | |
hash_map = {2 => ["a", "b", "c"], | |
3 => ["d", "e", "f"], | |
4 => ["g", "h", "i"], |
This file contains 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 binary_search(num, array, offset = 0) | |
mid = (array.length - 1)/2 | |
if array[mid] == num | |
return mid + offset | |
end | |
if array[mid] < num | |
offset += mid + 1 | |
binary_search(num, array[(mid + 1).. -1], offset) |
This file contains 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 primes(n) | |
prime_factors = [] | |
until n == 1 | |
2.upto(n) do |factor| | |
if n % factor == 0 | |
prime_factors << factor | |
n /= factor | |
end | |
end |
This file contains 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 fac(n) | |
if n < 2 | |
1 | |
else | |
n * fac(n-1) | |
end | |
end | |
def factorial(n) | |
sum = 1 |
This file contains 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
require 'benchmark' | |
def fib_it(n) | |
fib_nums = [0, 1] | |
iterate = n-1 | |
iterate.times do | |
num = fib_nums[-2] + fib_nums[-1] | |
fib_nums << num | |
end | |
p fib_nums[-1] |
This file contains 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
# Returns an array of all matching indeces | |
list_of_items = [8,1,1,1,2,3,4,5,6,7,8,8,8,8] | |
def linear_search(target_item, list) | |
items_index = [] | |
counter = 0 | |
list.each do |t| | |
if t == target_item | |
items_index << counter | |
end |
This file contains 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
# Only returns the index of the first match | |
list_of_items = [8,1,1,1,2,3,4,5,6,7,8,8,8,8] | |
def linear_search(target_item, list) | |
counter = 0 | |
list.each do |t| | |
if t == target_item | |
puts counter | |
break | |
end |
This file contains 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
# Destructive method | |
list_of_items = %w(a b c d e f g h i j k l m n o p q r s t u v w x y z) | |
def shuffle(list_of_items) | |
randomly_ordered_list = [] | |
until list_of_items.length == 0 | |
random_number = rand(list_of_items.length) | |
removed_item = list_of_items.slice!(random_number) |
This file contains 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
puts "Enter a word: " | |
word = gets.chomp | |
def pig_latinify(word) | |
if word.start_with?('a', 'e', 'i', 'o', 'u') | |
word << 'way' | |
else | |
sorted_letters = word.chars.to_a | |
until sorted_letters[0] == 'a' || sorted_letters[0] == 'e' || sorted_letters[0] == 'i' || sorted_letters[0] == 'o' || sorted_letters[0] == 'u' |
This file contains 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 to_roman(num) | |
roman_numerals = { | |
1000 => "M", | |
900 => "CM", | |
500 => "D", | |
400 => "CD", | |
100 => "C", | |
90 => "XC", | |
50 => "L", | |
40 => "XL", |