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
[1] pry(main)> def say_bye(name) | |
[1] pry(main)* puts "Bye bye, #{name}! See you later!" | |
[1] pry(main)* end | |
=> nil | |
[2] pry(main)> say_bye("Rux") | |
Bye bye, Rux! See you later! | |
=> nil | |
[3] pry(main)> my_array = ["one", "two", "blabla"] | |
=> ["one", "two", "blabla"] | |
[4] pry(main)> my_array.sort |
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) | |
# arr.max | |
# end | |
def maximum(arr) | |
return nil if arr.empty? | |
max = - 1.0/0 #returns negative infinity |
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
# Save this file to your computer so you can run it | |
# via the command line (Terminal) like so: | |
# $ ruby shakil_the_dog.rb | |
# | |
# Your method should wait for user input, which corresponds | |
# to you saying something to your dog (named Shakil). | |
# You'll probably want to write other methods, but this | |
# encapsulates the core dog logic |
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
# Implemented bubble sort & radix sort | |
# Included benchmarking module & benchmarked both methods | |
# Included manual benchmarking for radix_sort | |
# Sort the array from lowest to highest | |
def bubble_sort(arr) | |
return arr if arr.size <= 1 # already sorted | |
swapped = true | |
while swapped do | |
swapped = 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
# must be baller and either furnished or rent cheaper than 2100 | |
def rent?(furnished, rent, baller) | |
result = baller && (furnished || rent < 2100) | |
puts "Furnished: #{furnished}. Rent: #{rent}. Baller: #{baller}. Rent? #{result}" | |
end | |
### | |
# Add your "test" ("driver") code below in order to "test drive" (run) your method above... | |
# The test code will call the method with different permutations of options and output the result each time. | |
# This way, you will be able to run the renter.rb file from the CLI and look at the output of your "tests" to validate if the method works. |
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_output(number) | |
result = "" | |
result << "Fizz" if number % 3 == 0 | |
result << "Buzz" if number % 5 == 0 | |
result = number if result.empty? | |
result | |
end | |
def fizzbuzz(a,b) | |
a.upto(b) do |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
def area_price(length,width) | |
length * width * 15 | |
end | |
def colour_price(colour_count) | |
colour_count <= 2 ? 10 * colour_count : 15 * colour_count | |
end | |
def add_tax(price_without_tax) | |
(price_without_tax * 1.15).round(2) |
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) | |
# if array is empty or has just one element, no need to sort | |
return arr if arr.size <= 1 | |
# split array in 2 halves (divide) | |
mid = arr.size/2 | |
left = arr[0, mid] # arr[start,length] | |
right = arr[mid, arr.size-mid] | |
return merge(merge_sort(left),merge_sort(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
def fb(s, f) | |
s.upto(f) { |x| | |
puts e(x) | |
} | |
end | |
def e(y) | |
if div_3?(y) && div_5?(y) | |
"FizzBuzz" | |
elsif div_5?(y) |
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 fizz_buzz_puts(start, finish) | |
# loops through each number in the range | |
start.upto(finish) do |number| | |
puts fizz_buzz(number) | |
end | |
end | |
def fizz_buzz(number) | |
# checks if number is divisible by both 3 and 5 | |
if div_3?(number) && div_5?(number) |
OlderNewer