Last active
November 8, 2016 01:12
-
-
Save matt297/26588d65f8cd09211d078d90294f1f21 to your computer and use it in GitHub Desktop.
Lighthouse Labs - Intro to Web Dev - W3D1
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
# Courtesy of https://ide.c9.io/brendandeere/yellowpager | |
# Write a method called "yellowpager" that accepts a 10-character string of | |
# letters and outputs a corresponding phone number string. If the input letter | |
# string isn't 10 characters, you should return false. Not valid. | |
# 2 -> A B C | |
# 3 -> D E F | |
# 4 -> G H I | |
# 5 -> J K L | |
# 6 -> M N O | |
# 7 -> P Q R S | |
# 8 -> T U V | |
# 9 -> W X Y Z | |
########################## | |
# Solution 1 # | |
########################## | |
# This is probably the simplest implementation. Note that it only works for | |
# capital letters and will error if the input string contains a lowercase | |
# letter. Not very robust... | |
# Use an instance variable so that this is available to us inside our method. | |
@keypad = { | |
'A' => '2', 'B' => '2', 'C' => '2', | |
'D' => '3', 'E' => '3', 'F' => '3', | |
'G' => '4', 'H' => '4', 'I' => '4', | |
'J' => '5', 'K' => '5', 'L' => '5', | |
'M' => '6', 'N' => '6', 'O' => '6', | |
'P' => '7', 'Q' => '7', 'R' => '7', 'S' => '7', | |
'T' => '8', 'U' => '8', 'V' => '8', | |
'W' => '9', 'X' => '9', 'Y' => '9', 'Z' => '9' | |
} | |
def yellowpager(string) | |
# If the string is 10 characters long do stuff | |
# if not return false | |
if string.length == 10 | |
phone_number = '' | |
string.each_char do |character| | |
# Add each number to our buffer variable. this says | |
# the new value of phone_number, is the old value, with an extra | |
# character appended to it. The 'addition' happens before the | |
# value is assigned to the variable | |
phone_number = phone_number + @keypad[character] | |
end | |
# Remember, the last evaluated line of a ruby method is what the method | |
# will return. In this case we want that to be the value of our phone_number | |
phone_number | |
else | |
false | |
end | |
end | |
# Actually Run our method a few times | |
puts yellowpager('abc') | |
puts yellowpager('AHGYFMXEPK') | |
puts yellowpager("DJZOCGRAUG") | |
# The above solution is acceptable but not very elegant. Below are some | |
# (arguably) better solutions to the yellowpager problem | |
########################## | |
# Solution 2 # | |
########################## | |
# This solution uses a `case` statement. It accepts a argument and then | |
# conditionally returns the first value which matches a series of conditions. | |
# its kind of a short hand for: | |
# | |
# if thing | |
# ... | |
# elsif other_thing | |
# ... | |
# elsif another_thing | |
# ... | |
# elsif so_many_things | |
# ... | |
# end | |
# This solution also takes advantage of the fact that ranges like ('A'..'Z') know | |
# how to tell if a value is inside the range it represents. Case statements use this | |
# to compare values to ranges in a very clean way. | |
# You can try it yourself: type `irb` in the console and then type `(1..10) === 9`. | |
# The triple equals sign tells the range to check if the value belongs to that range | |
def better_yellowpager(string) | |
return false unless string.length == 10 | |
# upcase the whole string for case insensitive mathces | |
string.upcase.each_char.map do |character| # map returns an array of things | |
case character | |
when ('A'..'C') then 2 | |
when ('D'..'F') then 3 | |
when ('G'..'I') then 4 | |
when ('J'..'L') then 5 | |
when ('M'..'O') then 6 | |
when ('P'..'S') then 7 | |
when ('T'..'V') then 8 | |
when ('W'..'Z') then 9 | |
end | |
end. | |
join # join squishes all the array values together into a string | |
end | |
puts better_yellowpager('abc') | |
puts better_yellowpager('AHGYFMXEPK') | |
puts better_yellowpager("DJZOCGRAUG") | |
########################## | |
# Solution 3 # | |
########################## | |
# Sometimes developers will challenge themselves to produce the shortest code | |
# possible. We call this playing golf (the lower your character count, the better) | |
# While this doesnt always yield "good" code it can be kind of fun. | |
# Here is what people at work came up with when I asked them to find the shortest | |
# solution | |
def short_yellowpager(s) | |
/\A[a-z]{10}\z/i === s && s.upcase.tr('A-Z', '22233344455566677778889999') | |
end | |
# This looks crazy and complicated, but if you want to understand a bit more | |
# Read about ruby Regexes here: | |
# http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm | |
# And the String#tr method here: | |
# http://ruby-doc.org/core-2.2.3/String.html#method-i-tr | |
puts short_yellowpager('abc') | |
puts short_yellowpager('AHGYFMXEPK') | |
puts short_yellowpager("DJZOCGRAUG") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment