This file contains hidden or 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' | |
| # Sort the array from lowest to highest | |
| def sort(arr) | |
| swap = true | |
| # Merge sort | |
| while swap | |
| swap = false | |
| for i in 0..arr.length - 1 | |
| if i <= arr.length - 2 && arr[i] > arr[i + 1] | |
| greater = arr[i] |
This file contains hidden or 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 | |
| def shakil_the_dog |
This file contains hidden or 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) | |
| # if baller && (furnished || rent < 2100) | |
| # return true | |
| # else | |
| # return false | |
| # end | |
| # end |
This file contains hidden or 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 fuzzbuzz (start_num, end_num) | |
| start_num.upto(end_num) do |i| | |
| if i % 5 == 0 && i % 3 == 0 | |
| puts "FizzBuzz" | |
| elsif i % 5 == 0 | |
| puts "Buzz" | |
| elsif i % 3 == 0 | |
| puts "Fizz" | |
| else | |
| puts i |
This file contains hidden or 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) | |
| result = nil | |
| if !arr.is_a? (Array) | |
| result = "not an array" | |
| else | |
| arr.each { |i| result = i if result == nil || i > result} | |
| return result | |
| end | |
| end |
NewerOlder