Skip to content

Instantly share code, notes, and snippets.

@jah2488
Created May 17, 2019 16:23
Show Gist options
  • Save jah2488/66a8dbef84ef77eede32e80c3d480d6b to your computer and use it in GitHub Desktop.
Save jah2488/66a8dbef84ef77eede32e80c3d480d6b to your computer and use it in GitHub Desktop.
The many phases of Raindrops from Exercism.
require 'prime'
module Raindrops
extend self
def convert(n)
primes = n.prime_division.flatten
if primes.any? { |x| [3,5,7].include?(x) }
primes.map do |x|
case x
when 3 then 'Pling'
when 5 then 'Plang'
when 7 then 'Plong'
else x
end
end.select { |x| x.is_a?(String) }.join
else
n.to_s
end
end
end
require 'prime'
module Raindrops
extend self
def convert(n)
primes = n.prime_division.flatten
if (primes & [3,5,7]).length > 0
primes.map do |x|
case x
when 3 then 'Pling'
when 5 then 'Plang'
when 7 then 'Plong'
end
end.join
else
n.to_s
end
end
end
require 'prime'
module Raindrops
extend self
def convert(n)
primes = n.prime_division.flatten
return n.to_s unless (primes & [3,5,7]).length > 0
primes.map do |x|
case x
when 3 then 'Pling'
when 5 then 'Plang'
when 7 then 'Plong'
end
end.join
end
end
require 'prime'
module Raindrops
extend self
def convert(n)
primes = n.prime_division.flatten
return n.to_s unless (primes & [3,5,7]).length > 0
primes.map { |x| { 3 => 'Pling',
5 => 'Plang',
7 => 'Plong', }[x] }.join
end
end
require 'prime'
module Raindrops
extend self
def convert(n)
primes = n.prime_division.flatten
return n.to_s unless (primes & [3,5,7]).length > 0
primes.select { |x| [3,5,7].include?(x) }.map { |x| "Pl#{{3=>'i',5=>'a',7=>'o'}[x]}ng"}.join
end
end
require 'prime'
module Raindrops
extend self
def convert(n)
c = { 3 => 'i' , 5 => 'a' , 7 => 'o'}
primes = n.prime_division.flatten
return n.to_s unless (primes & c.keys).length > 0
primes.select { |x| c.keys.include?(x) }
.map { |x| "Pl#{c[x]}ng" }
.join
end
end
require 'prime'
module Raindrops
extend self
def convert(n)
primes = n.prime_division.flatten
return n.to_s unless no_simple_primes?(primes)
primes.select { |x| small_prime?(x) }
.map { |x| "Pl#{conversions[x]}ng" }
.join
end
private
def no_simple_primes?(primes)
(primes & conversions.keys).length > 0
end
def small_prime?(n)
conversions.keys.include?(n)
end
def conversions
{ 3 => 'i' , 5 => 'a' , 7 => 'o'}
end
end
require 'prime'
module Raindrops
extend self
def convert(n)
return n.to_s unless no_simple_primes?(primes(n))
primes(n).select(&method(:small_prime?)).map(&method(:pl_ng)).join
end
private
def primes(n)
n.prime_division.flatten
end
def no_simple_primes?(primes)
(primes & conversions.keys).length > 0
end
def small_prime?(n)
conversions.keys.include?(n)
end
def pl_ng(n)
"Pl#{conversions[n]}ng"
end
def conversions
{ 3 => 'i' , 5 => 'a' , 7 => 'o'}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment