Skip to content

Instantly share code, notes, and snippets.

@lrechert
lrechert / make_acronym.rb
Created April 30, 2016 21:22
Ruby Weekly Challenge - Make Acronym
# Implement a function called make_acronym that returns the first letters
# of each word in a passed in string. Make sure the letters returned are
# uppercase.
# If the value passed in is not a string return 'Not a string'
# If the value passed in is a string which contains only characters
# other than spaces and alphabet letters, return 'Not letters'
def make_acronym(input_string)
return nil unless input_string
@lrechert
lrechert / rgb.spec
Created April 24, 2016 05:37
Ruby Weekly Challenge - RGB
def rgb(r, g, b)
"#{to_padded_hex(r)}#{to_padded_hex(g)}#{to_padded_hex(b)}".upcase
end
def to_padded_hex(n)
n = 0 if n < 0
n = 255 if n > 255
n.to_s(16).rjust(2, "0")
end
@lrechert
lrechert / valid_braces.rb
Last active April 24, 2016 05:28
Ruby Weekly Challenge - Valid Braces
def valid_braces(braces)
BracesValidator.new(braces).valid_braces?
rescue
return false
end
# most succinct
def validBraces(braces)
while braces.gsub!(/\(\)|\{\}|\[\]/, '') do ; end
braces.empty?
@lrechert
lrechert / two_joggers.rb
Created April 16, 2016 21:52
Ruby Challenge - Two Joggers
# Description
# Bob and Charles are meeting for their weekly jogging tour. They both start
# at the same spot called "Start" and they each run a different lap, which
# may (or may not) vary in length. Since they know each other for a long time
# already, they both run at the exact same speed.
# Task
# Your job is to complete the function nbrOfLaps(x, y) that, given the length
# of the laps for Bob and Charles, finds the number of laps that each jogger
# has to complete before they meet each other again, at the same time, at the start.
@lrechert
lrechert / dna_sequence.rb
Created April 16, 2016 21:02
Ruby Weekly Challenge - DNA Sequences
def check_dna(sequence1, sequence2)
complementary?(normalize(sequence1), normalize(sequence2.reverse))
end
def complementary?(seq1, seq2)
return bonded?(seq1, seq2) || bonded?(seq2, seq1)
end
def bonded?(seq1, seq2)
seq2.include?(seq1)
@lrechert
lrechert / pin_generator.rb
Last active April 18, 2016 18:41
Ruby Weekly Challenge - pin generator
# Description:
# Alright, detective, one of our colleagues successfully observed our target person, Robby the robber.
# We followed him to a secret warehouse, where we assume to find all the stolen stuff.
# The door to this warehouse is secured by an electronic combination lock. Unfortunately our spy isn't
# sure about the PIN he saw, when Robby entered it.
# The keypad has the following layout:
# ┌───┬───┬───┐
# │ 1 │ 2 │ 3 │
# ├───┼───┼───┤
# │ 4 │ 5 │ 6 │
@lrechert
lrechert / extract_hashtags.rb
Last active April 18, 2016 18:41
Ruby Weekly Challenge - hashtags
# Description:
#
# You start working for a fancy new startup hoping to revolutionize social networking! GASP! They had this great idea that users should be able to specify relevant keywords to their posts using an ingenious idea by prefixing those keywords with the pound sign (#). Your job is to extract those keywords so that they can be used later on for whatever purposes.
#
# Note:
# Pound signs alone do not count, for example: the string "#" would return an empty array.
# If a word is preceded by more than one hashtag, only the last hashtag counts (e.g. "##alot" would return ["alot"])
# Hashtags cannot be within the middle of a word (e.g. "in#line hashtag" returns an empty array)
# Hashtags must precede alphabetical characters (e.g. "#120398" or "#?" are invalid)
#
@lrechert
lrechert / pad_left.rb
Last active April 18, 2016 18:41
Ruby Weekly Challenge - leftpad
# Description:
# From Programming Praxis ... https://programmingpraxis.com/2016/03/25/leftpad/
# Large portions of the internet failed a few days ago when a program called
# leftpad, which pads a string to a given length by adding spaces or other
# characters at the left of the string, was suddenly removed from its repository.
# The whole episode is sad, and brings nothing but shame on everyone involved
# (though everyone involved seems to think they acted properly throughout), and
# all of the web sites that broke were created by fools (you don’t rely on
# unknown third parties to maintain code critical to your application at some
# unknown place on the internet). You can read more about what happened at these
@lrechert
lrechert / pattern_matcher.rb
Last active April 18, 2016 18:41
Ruby Weekly Challenge - Pattern Matcher
# Description:
# word_pattern(pattern, string)
# that given a pattern and a string str, find if str follows the same sequence as pattern.
# For example:
# word_pattern('abab', 'truck car truck car') == true
# word_pattern('aaaa', 'dog dog dog dog') == true
# word_pattern('abab', 'apple banana banana apple') == false
@lrechert
lrechert / longest_common_prefix.rb
Last active April 18, 2016 18:42
Ruby Weekly Challenge - String Prefixes
# Two strings have a common prefix that consists of the longest prefix of the strings that is the same.
# For instance, the strings “I love cats” and “I love dogs” have the common prefix “I love ”
# (including a trailing space at the end of love, which doesn’t appear properly in some browsers).
# Your task is to write a program that finds the common prefix of a list of strings (possibly more than two strings).
# Algo:
# 1. find length of shortest sting in arr since this is the max longest-prefix length
# 2. iterate over each string in arr, comparing against the substring
# 3. if we reach the end of arr, return the calculated prefix as the longest.
def shortest_string(arr)