Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View danielpowell4's full-sized avatar

Daniel Powell danielpowell4

View GitHub Profile
class Mountain
attr_accessor :name, :height, :summit_attempts
def initialize(name, height)
@name = name
@height = height.to_s
@summit_attempts = 0
end
def climb
@danielpowell4
danielpowell4 / T9_press_count.rb
Created August 29, 2016 20:36
What is the press count for texting on older T9 phones?
# best answer
def presses(phrase)
groups = ["1", " 0", "ABC2", "DEF3", "GHI4", "JKL5", "MNO6", "TUV8", "PQRS7", "WXYZ9"]
phrase.upcase.chars.map do |c|
1 + groups.find { |grp| grp.include?(c) }.index(c)
end.reduce(:+)
end
# another option
def presses(phrase)
@danielpowell4
danielpowell4 / spinwords.rb
Created August 30, 2016 05:27
Returns a sentence reversing words if they have 5 or more characters
# best answer
def spinWords(string)
string.gsub(/\w{5,}/, &:reverse)
end
# top answer
def spinWords(string)
string.split.map { |s| s.length >= 5 ? s.reverse : s }.join ' '
end
@danielpowell4
danielpowell4 / pig_latin.rb
Created August 30, 2016 06:10
Pig latin translation sans handling for vowels or "Qu"-type exceptions
# gsub solution
def pig_it text
text.gsub(/(\w)(\w+)*/, '\2\1ay')
end
# interpolation check for word
def pig_it text
text.split.map{|word| word =~ /\w/ ? "#{word[1..-1]}#{word[0]}ay" : word}.join(" ")
end
@danielpowell4
danielpowell4 / dub_step_decoder.rb
Created August 30, 2016 06:30
Removes WUB's from popular songs
def song_decoder(lyrics)
lyrics.upcase.gsub('WUB', ' ').squeeze(' ').strip.capitalize
end
song_decoder("WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB")
# => WE ARE THE CHAMPIONS MY FRIEND
@danielpowell4
danielpowell4 / rgb_to_hex.rb
Last active August 31, 2016 06:19
Converts RGB color values into equivalent hex codes in Ruby
# best solution
def rgb(r,g,b)
"%.2X%.2X%.2X" % [r,g,b].map{|c| [[c,255].min,0].max}
end
# needs a more DRY way of handling single digit hex conversion
def rgb(r, g, b)
[r,g,b].map{ |c| [[c,255].min,0].max > 16 ? [[c,255].min,0].max.to_s(16) : "0#{[[c,255].min,0].max.to_s(16)}"}.join.upcase
end
@danielpowell4
danielpowell4 / rot13.rb
Created September 11, 2016 07:01
Replace letters in string with the letter 13 places after it in the alphabet
def rot13(string)
string.tr("A-Za-z", "N-ZA-Mn-za-m")
end
# More on tr here: http://apidock.com/ruby/String/tr
@danielpowell4
danielpowell4 / valid_paren.rb
Created September 11, 2016 07:17
Checks if parens are valid returning true or false
def valid_parentheses(string)
open = 0
string.chars.each do |c|
open += 1 if c == "("
open -= 1 if c == ")"
return false if open < 0
end
open == 0
end
@danielpowell4
danielpowell4 / number_of_labs.rb
Created September 11, 2016 07:31
Given the lengths of two different tracks, output how many laps it will take until two joggers meet again
# lcm => least common multiple
# one-liner
def nbr_of_laps(x,y)
[x.lcm(y)/x, x.lcm(y)/y]
end
# simple
def nbr_of_laps(x, y)
lcm = x.lcm(y)
@danielpowell4
danielpowell4 / choose_best_trip.rb
Created September 11, 2016 18:48
Outputs max distance you can travel where t is destination count, k is distance, and ls is a list of length.
def choose_best_trip(t, k, ls)
ls.combination(k).to_a.map{|a| a.reduce(:+)}.select{ |v| v <= t }.max
end
# example: ts = [50, 55, 56, 57, 58] choose_best_trip(163, 3, ts) -> 163
# Tests
Test.describe("choose_best_sum") do
Test.it("Basic Tests") do