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
class Mountain | |
attr_accessor :name, :height, :summit_attempts | |
def initialize(name, height) | |
@name = name | |
@height = height.to_s | |
@summit_attempts = 0 | |
end | |
def climb |
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
# 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) |
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
# 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 |
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
# 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 |
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 song_decoder(lyrics) | |
lyrics.upcase.gsub('WUB', ' ').squeeze(' ').strip.capitalize | |
end | |
song_decoder("WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB") | |
# => WE ARE THE CHAMPIONS MY FRIEND | |
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
# 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 |
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 rot13(string) | |
string.tr("A-Za-z", "N-ZA-Mn-za-m") | |
end | |
# More on tr here: http://apidock.com/ruby/String/tr |
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 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 |
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
# 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) |
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 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 |
OlderNewer