Other Python solutions.
def convert(number):
result = ""
if (number % 3) == 0:
result += "Pling"
if (number % 5) == 0:
result += "Plang"
if (number % 7) == 0:
result += "Plong"
if result == "":
result = str(number)
return result
Other Clojure solutions.
(ns raindrops)
(defn- substitute-factors-with [n substitutions]
(let [factors (sort (keys substitutions))
converted (map
(fn [factor]
(if (= 0 (mod n factor))
(get substitutions factor)
""))
factors)
s (apply str converted)]
(if (empty? s)
(str n)
s)))
(defn convert [n]
(substitute-factors-with n {3 "Pling"
5 "Plang"
7 "Plong"}))
Other Rust solutions.
pub fn raindrops(n: u32) -> String {
let rain = [(3, "Pling"), (5, "Plang"), (7, "Plong")]
.into_iter()
.filter(|&(number, _)| n % number == 0)
.map(|&(_, sound)| sound)
.collect::<Vec<&str>>()
.join("");
return if rain.is_empty() { n.to_string() } else { rain };
}
Other Elm solutions.
module Raindrops exposing (raindrops)
raindrops : Int -> String
raindrops number =
let
labelledRaindrops =
labelFactors number
[ ( 3, "Pling" )
, ( 5, "Plang" )
, ( 7, "Plong" )
]
in
stringWithDefault (String.fromInt number) labelledRaindrops
labelFactors : Int -> List ( Int, String ) -> String
labelFactors number =
List.filterMap (labelFactor number) >> String.concat
labelFactor : Int -> ( Int, String ) -> Maybe String
labelFactor num ( factor, label ) =
if modBy factor num == 0 then
Just label
else
Nothing
stringWithDefault : String -> String -> String
stringWithDefault default value =
Maybe.withDefault default (nonEmpty value)
nonEmpty : String -> Maybe String
nonEmpty string =
if String.isEmpty string then
Nothing
else
Just string
Other Roc solutions.
module [convert]
convert : U64 -> Str
convert = \number ->
pling = if number % 3 == 0 then "Pling" else ""
plang = if number % 5 == 0 then "Plang" else ""
plong = if number % 7 == 0 then "Plong" else ""
result = Str.joinWith [pling, plang, plong] ""
if result == "" then
Num.toStr number
else
result