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
| states = { | |
| OR: 'Oregon', | |
| FL: 'Florida', | |
| CA: 'California', | |
| NY: 'New York', | |
| MI: 'Michigan' | |
| } | |
| states[:AL] = 'Alabama' | |
| states[:AK] = 'Alaska' | |
| states.default = 'a city currently unknown to the database' |
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 count_letters(sentence) | |
| letters = Hash.new(0) | |
| sentence.split(' ').join.split('').each { |letter| letters[letter] += 1 } | |
| letters | |
| end | |
| p count_letters("lighthouse in the house...") | |
| def count_letter_positions(sentence) | |
| # letters = Hash.new([]) does not work. |
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
| list = {'yvr' => 'Vancouver', 'yba' => 'Banff', 'yyz' => 'Toronto', 'yxx' => 'Abbotsford', 'ybw' => 'Calgary'} | |
| # Why is it returning nil instead of first element of the list above | |
| p list[0] | |
| #Because list is a Hash and doesn't have anything associated with the key 0 | |
| def average(numbers) | |
| return 0 if numbers.empty? | |
| excess = numbers.count(nil) |
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' | |
| # Conceptually, a merge sort works as follows: | |
| # Divide the unsorted list into n sublists, each containing 1 element | |
| # (a list of 1 element is considered sorted). | |
| # Repeatedly merge sublists to produce new sorted sublists until there is only 1 | |
| # sublist remaining. This will be the sorted list. | |
| # | |
| def couple_up_elements(arr) |
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 signCost(dim, col) | |
| squareFoot = dim[0] * dim[1] | |
| costColours = (col > 2) ? 15 : 10 | |
| net = squareFoot * 15 + col * costColours | |
| net * 1.15 | |
| 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 partition(arr) | |
| p = arr[0] | |
| smaller = arr.select {|e| e < p} | |
| bigger = arr.select {|e| e > p} | |
| equal = arr.select {|e| e == p} | |
| [smaller, equal, bigger] | |
| end | |
| def quicksort(arr) |
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 shakil_the_dog(interaction) | |
| responses = Hash.new('Shakil has no clue what you say.') | |
| responses["woof"] = "WOOF WOOF WOOF" | |
| responses["shakil stop"] = "Shakil stops barking." | |
| responses["Shakil STOP!"] = "Shakil stops barking." | |
| responses["treat"] = "Wagging tail, ancticipating treat." | |
| responses["go away"] = "Shakil the dog leaves the room with its tail in between its legs." | |
| interaction = (/treat/.match(interaction) == nil) ? interaction : 'treat' | |
| responses[interaction] |
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) | |
| baller && (furnished || rent < 2100) | |
| 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. | |
| # Without the test code, it will be hard for you to know if this method is working as it should or not. |
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
| FIZZ, BUZZ = "Fizz","Buzz" | |
| D1, D2 = 3,5 #The two Divisors | |
| def fizzbuzz(s=1,e=100) | |
| s,e = (s < e) ? [s,e] : [e,s] #Swap values when starting value `s` | |
| #is bigger than ending value `e` to avoid | |
| #infinite loop | |
| def helper(i,m,r=FIZZ) | |
| (i % m == 0) ? r : "" |
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) | |
| arr.empty? ? nil : arr.reduce(0) {|acc, e| e > acc ? e : acc} | |
| end | |
| # expect it to return 42 below | |
| result = maximum([2, 42, 22, 02]) | |
| puts "max of 2, 42, 22, 02 is: #{result}" | |
| # expect it to return nil when empty array is passed in |