Created
September 12, 2017 17:20
-
-
Save KevinSia/5e9477ae54a983a553c69cc47fd62d6f to your computer and use it in GitHub Desktop.
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
# TODO: Refactor for elegance | |
def shout_backwards(string) | |
# before: | |
# all_caps = string.upcase | |
# backwards = all_caps.reverse | |
# result = backwards + "!!!!" | |
# return result | |
string.upcase.reverse + "!!!!" | |
end | |
# FIXME: This is convoluted. Refactor for clarity. | |
def factors_of(num) | |
return [num] if num <= 1 | |
(2..num - 1).select { |i| x % i == 0 } | |
end | |
def squared_primes(array) | |
# before: array.find_all{|x| (2..x-1).select(){|i| x % i == 0 }.count == 0 }.map{|p| p*p} | |
primes = array.select do |num| | |
factors_of(num).count == 0 | |
end | |
primes.map { |num| num ** 2 } | |
end | |
# Driver code... don't edit. This should print a bunch of trues. | |
puts shout_backwards("hello, boot") == "TOOB ,OLLEH!!!" | |
puts squared_primes([1, 3, 4, 7, 42]) == [1, 9, 49] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment