View ceaser_cipher.rb
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 caesar_cipher(message, shift) | |
split_message = message.split("").map do |letter| | |
# If letter is not an alphabet do not change it | |
if letter.downcase.ord < 97 or letter.downcase.ord > 128 | |
letter | |
else | |
code = letter.ord + shift | |
if (letter == letter.upcase && code > 90) || (letter == letter.downcase && code > 122) | |
code -= 26 | |
end |
View substrings.rb
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
# Return a hash listing each substring present in the dictionary found in the string | |
def substrings(string, dictionary) | |
count = Hash.new(0) | |
dictionary.each do |word| | |
count[word] = string.scan(/#{word}/).length if string.scan(/#{word}/).length != 0 | |
end | |
puts count | |
end | |
View gist:a5e10b9d03983d8474f6870045597d8b
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 stock_picker(stocks) | |
profits = {} | |
stocks.each_with_index do |price_i, i| | |
stocks.each_with_index do |price_j, j| | |
profits[[i, j]] = price_j - price_i if j > i | |
end | |
end | |
index, _ = profits.max_by { |k, v| v } |
View bubble_sort.rb
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 bubble_sort(array) | |
sorted = false | |
until sorted do | |
sorted = true | |
array.each_with_index do |_, i| | |
if i + 1 != array.length && array[i] > array[i + 1] | |
array[i], array[i + 1] = array[i + 1], array[i] | |
sorted = false | |
end | |
end |
View code_to_dial_code.json
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
{ | |
"AF": "+93", | |
"AX": "+358", | |
"AL": "+355", | |
"DZ": "+213", | |
"AS": "+1684", | |
"AD": "+376", | |
"AO": "+244", | |
"AI": "+1264", | |
"AQ": "+672", |