Last active
April 11, 2020 22:17
-
-
Save thewarpaint/fd4d68d509a09a48b0383e94d4ca80f7 to your computer and use it in GitHub Desktop.
Ruby crash course
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 anagrams?(a, b) | |
frequencies_a = get_frequencies(a) | |
frequencies_b = get_frequencies(b) | |
if frequencies_a.length != frequencies_b.length | |
return false | |
end | |
frequencies_a.each do |key, value| | |
if value != frequencies_b[key] | |
return false | |
end | |
end | |
true | |
end | |
def get_frequencies(string) | |
frequency_map = {} | |
string.split("").each do |c| | |
unless frequency_map.key?(c) | |
frequency_map[c] = 0 | |
end | |
frequency_map[c] += 1 | |
end | |
frequency_map | |
end | |
puts anagrams?("binary", "brainy") | |
puts anagrams?("iamlordvoldemort", "tommarvoloriddle") | |
puts anagrams?("aaabbc", "aaabbd") |
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 balanced?(string) | |
opening_braces = ["{", "[", "("] | |
closing_braces = ["}", "]", ")"] | |
stack = [] | |
string.split("").each do |c| | |
opening_pos = opening_braces.index(c) | |
if opening_pos != nil | |
stack.push(c) | |
puts "Char #{c} pushed to stack" | |
next | |
else | |
if stack.length == 0 | |
puts "Char #{c} found but stack is empty" | |
return false | |
end | |
stack_top = stack.pop | |
puts "Char #{c} popped char #{stack_top} from stack" | |
closing_pos = closing_braces.index(c) | |
opening_pos = opening_braces.index(stack_top) | |
if closing_pos != opening_pos | |
puts "Char #{c} doesn't match char #{stack_top} on top of the stack" | |
return false | |
end | |
end | |
end | |
return stack.length == 0 | |
end | |
puts balanced?("{}()[]") | |
puts balanced?("{[()]}") | |
puts balanced?("{{[[(())]]}}") | |
puts balanced?("()]") | |
puts balanced?("{[)}") | |
puts balanced?("{[(])}") |
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 mask_email_address(email_address = "") | |
parts = email_address.split("@") | |
if parts.length == 0 | |
return "***@**" | |
end | |
masked_email_address = parts[0][0..1] + "***" | |
if parts[1] != nil | |
masked_email_address += "@" | |
masked_domain_parts = parts[1].split(".").map do |domain_part| | |
domain_part[0] + "**" | |
end | |
masked_email_address += masked_domain_parts.join(".") | |
end | |
masked_email_address | |
end | |
puts mask_email_address | |
puts mask_email_address("") | |
puts mask_email_address("eduardo") | |
puts mask_email_address("eduardo@localhost") | |
puts mask_email_address("eduardo@garcia.mx") | |
puts mask_email_address("eduardo@garcia.espinosa.mx") |
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 palindrome?(string) | |
steps = (string.length / 2.0).floor | |
for i in 0...steps do | |
reverse_i = string.length - i - 1 | |
puts "Comparing #{string[i]} at index #{i} vs #{string[reverse_i]} at index #{reverse_i}" | |
if string[i] != string[reverse_i] | |
return false | |
end | |
end | |
true | |
end | |
puts palindrome?("racecar") | |
puts palindrome?("anitalavalatina") | |
puts palindrome?("vetustamorla") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment