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
# 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 |
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
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 |
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
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? |
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
# 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. |
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
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) |
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
# 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 │ |
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
# 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 |
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
# 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 |
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
# 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) |