Skip to content

Instantly share code, notes, and snippets.

Avatar
:electron:

Muhammad Khan MBM1607

:electron:
View GitHub Profile
@MBM1607
MBM1607 / bubble_sort.rb
Created Feb 25, 2021
bubble sort with ruby
View bubble_sort.rb
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 gist:a5e10b9d03983d8474f6870045597d8b
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 }
@MBM1607
MBM1607 / substrings.rb
Created Feb 25, 2021
Ruby substrings exercise
View substrings.rb
# 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
@MBM1607
MBM1607 / ceaser_cipher.rb
Created Feb 25, 2021
Odin Project Ruby Ceaser Cipher
View ceaser_cipher.rb
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