Skip to content

Instantly share code, notes, and snippets.

View Ajwah's full-sized avatar

Kevin Johnson Ajwah

  • Toronto, Canada
View GitHub Profile
@Ajwah
Ajwah / states_cities.rb
Created September 23, 2015 15:11
States and Cities - W1D3 exercise
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'
@Ajwah
Ajwah / letter_count.rb
Created September 23, 2015 12:10
Count letters of a string and also return indexes of each letter, skipping space.
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.
@Ajwah
Ajwah / debug_solved.rb
Created September 23, 2015 11:49
Mixed all 4 solutions in one gist.
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)
@Ajwah
Ajwah / mergesort.rb
Last active September 23, 2015 00:19
Implement mergesort
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)
@Ajwah
Ajwah / shop_sign.rb
Created September 22, 2015 18:20
Method to calculate total cost given dimension and amount colours.
def signCost(dim, col)
squareFoot = dim[0] * dim[1]
costColours = (col > 2) ? 15 : 10
net = squareFoot * 15 + col * costColours
net * 1.15
end
@Ajwah
Ajwah / sort.rb
Created September 22, 2015 17:16
Implement Quicksort
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)
@Ajwah
Ajwah / dog.rb
Created September 22, 2015 15:58
Interaction with Shakil the Dog.
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]
@Ajwah
Ajwah / renter.rb
Created September 22, 2015 15:31
Write test driver for faulty implementation of apartment query and refactor.
# 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.
@Ajwah
Ajwah / fizzbuzz.rb
Created September 22, 2015 14:59
Refactor FizzBuzz
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 : ""
@Ajwah
Ajwah / max.rb
Created September 22, 2015 13:01
Day2 - Maximum
# 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