Multiply
0
0
2
2
-1
-1
1
1
100000000000000000000000
100000000000000000000000
-100000000000000000000000
-100000000000000000000000
2500
-2500
-2500
Biggest
nil
1
100
Clean
[]
[[], 1, 2, 3, 4, 5, 1..5, "", "a", "b"]
Flatten
[]
[1, 2, [3, 4], 1, []]
RepeatedWords
{"hola"=>0, "tu"=>0, "esta"=>1, "es"=>1, "una"=>0, "prueba"=>1}
Palindrome
false
true
true
Last active
October 18, 2020 19:36
-
-
Save joalbertg/7df081f6e59bc2a7009ea1fec4b12f7d to your computer and use it in GitHub Desktop.
ruby: Technical Interview
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
module TechnicalInterview | |
module Multiply | |
# multiplicar 2 números sin usar en signo de multiplicar para la operación | |
def self.multiply(a, b) | |
return 0 if [a, b].include?(0) | |
a_abs, b_abs = a.abs, b.abs | |
a_positive, b_positive = a.positive?, b.positive? | |
arr = a_abs > b_abs ? [a_abs] * b_abs : [b_abs] * a_abs | |
result = arr.inject(&:+) | |
(a_positive && b_positive) || !(a_positive || b_positive) ? result : -result | |
end | |
end | |
module Biggest | |
# búscar el número más grande de un arreglo pasando solo una vez | |
def self.fetch_biggest(arr = []) | |
return if arr.empty? | |
arr.reduce { |acc, e| acc > e ? acc : e } | |
end | |
end | |
module Clean | |
# eliminar de un arreglo los [nil, false, 0] | |
def self.clean(arr) | |
arr.select { |e| ![nil, 0, false].include?(e) } | |
end | |
end | |
module Flatten | |
# aplanar arreglos en el primer nivel | |
def self.flatten(arr) | |
arr.reduce([]) { |acc, e| acc.concat(e) } | |
end | |
end | |
module RepeatedWords | |
# contar número de palabras repetidas | |
def self.repeated_words(str = '') | |
arr_str = str.downcase.tr("áéíóú", 'aeiou').gsub(/[,.\-?!]/, '').split(' ') | |
arr_str.reduce({}) do |curr, e| | |
curr[e] ? curr[e] += 1 : curr[e] = 0 | |
curr | |
end | |
end | |
end | |
module Palindrome | |
def self.palindrome(str) | |
str = str.gsub(/\s/, '').downcase | |
str == str.reverse | |
end | |
end | |
end | |
puts "Multiply" | |
puts TechnicalInterview::Multiply.multiply(0, 1) | |
puts TechnicalInterview::Multiply.multiply(1, 0) | |
puts TechnicalInterview::Multiply.multiply(1, 2) | |
puts TechnicalInterview::Multiply.multiply(2, 1) | |
puts TechnicalInterview::Multiply.multiply(-1, 1) | |
puts TechnicalInterview::Multiply.multiply(1, -1) | |
puts TechnicalInterview::Multiply.multiply(1, 1) | |
puts TechnicalInterview::Multiply.multiply(-1, -1) | |
puts TechnicalInterview::Multiply.multiply(100000000000000000000000, 1) | |
puts TechnicalInterview::Multiply.multiply(1, 100000000000000000000000) | |
puts TechnicalInterview::Multiply.multiply(-100000000000000000000000, 1) | |
puts TechnicalInterview::Multiply.multiply(1, -100000000000000000000000) | |
puts TechnicalInterview::Multiply.multiply(50, 50) | |
puts TechnicalInterview::Multiply.multiply(-50, 50) | |
puts TechnicalInterview::Multiply.multiply(50, -50) | |
puts "\nBiggest" | |
p TechnicalInterview::Biggest.fetch_biggest([]) | |
p TechnicalInterview::Biggest.fetch_biggest([-1, 0, 1]) | |
p TechnicalInterview::Biggest.fetch_biggest([100, -100, 0]) | |
puts "\nClean" | |
p TechnicalInterview::Clean.clean([]) | |
p TechnicalInterview::Clean.clean([[], 0, 1, 2, 3, 4, 5, (1..5), '', 'a', 'b', nil, false]) | |
puts "\nFlatten" | |
p TechnicalInterview::Flatten.flatten([]) | |
p TechnicalInterview::Flatten.flatten([[1, 2], [[3, 4]], [1, []]]) | |
puts "\nRepeatedWords" | |
p TechnicalInterview::RepeatedWords.repeated_words('Hola tú!, está es una prueba??, esta es... -prueba') | |
puts "\nPalindrome" | |
p TechnicalInterview::Palindrome.palindrome('No es un palindrome') | |
p TechnicalInterview::Palindrome.palindrome('Arepera') | |
p TechnicalInterview::Palindrome.palindrome('Do geese see God') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment