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
# Find the maximum | |
def maximum(arr) | |
max_val = arr[0] | |
arr.each do |num| | |
if num > max_val | |
max_val = num | |
end | |
end | |
max_val | |
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 fizzbuzz(start, finish) | |
start = start.to_i | |
finish = finish.to_i | |
range = (start..finish) | |
for num in range | |
puts "FIZZBUZZ #{num}" if (num % 3==0)&&(num % 5==0) | |
puts "FIZZ #{num}" if num % 3==0 | |
puts "BUZZ #{num} " if num % 5==0 | |
puts num if (num % 3!=0)&&(num % 5!=0) |
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
# must be baller and either furnished or rent cheaper than 2100 | |
def rent?(furnished, rent, baller) | |
baller && (furnished || rent < 2100) | |
end | |
puts rent?(true, 2000, true) | |
puts rent?(false, 2000, true) | |
puts rent?(true, 2500, true) | |
puts rent?(true, 2000, false) | |
puts rent?(true, 2500, false) |
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 "Shakil is barking, what will you do?" | |
answer = gets.chomp | |
while (answer != "go away") do | |
puts "what next?" | |
answer = gets.chomp | |
if answer == "woof" | |
puts "WOOF WOOF WOOF!" | |
elsif answer == "shakil stop" | |
puts "he stopped barking, good job dog whisperer" |
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
# Sort the array from lowest to highest | |
def bubblesort(arr) | |
n = arr.length | |
loop do | |
swapped = false | |
(n-1).times do |i| | |
if arr[i] > arr[i+1] | |
arr[i], arr[i+1] = arr[i+1], arr[i] | |
swapped = true | |
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 merge_sort(arr) | |
def merge(left_side, right_side) | |
result = [] | |
left = 0 | |
right = 0 | |
loop do | |
break if right >= right_side.length && left >= left_side.length | |
if right >= right_side.length || (left < left_side.length && left_side[left] < right_side[right]) |
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
list = {'yvr' => 'Vancouver', | |
'yba' => 'Banff', | |
'yyz' => 'Toronto', | |
'yxx' => 'Abbotsford', | |
'ybw' => 'Calgary'} | |
# Why is it returning nil instead of first element of the list above | |
puts list.first |
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 char_count(list) | |
letters = {} | |
list.each do |word| | |
word.split('').each { |letter| letters.has_key?(letter) ? letters[letter] += 1 : letters[letter] =1} | |
end | |
letters = letters.sort_by {|a,b| b} | |
letters.reverse! | |
print letters | |
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
@states = { | |
OR: 'Oregon', | |
FL: 'Florida', | |
CA: 'California', | |
NY: 'New York', | |
MI: 'Michigan' | |
} | |
@states[:WA]="Washington" | |
@states[:TX]="Texas" |
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 benchmark | |
start_time = Time.now | |
yield | |
end_time = Time.now | |
run_time = end_time - start_time | |
end | |
long_string = "apple"*100000000 | |
stupid_math = 1 + 1 * 1000 - 54 * 345 | |
OlderNewer