The Non-Zero Digital Product Challenge
Haskell, 27 bytes
foldr((*).max 1.read.pure)1
Try it online!
Ungolfed with UniHaskell and -XUnicodeSyntax
import UniHaskell
f ∷ String → Int
f = product ∘ map (max 1 ∘ read ∘ pure)
Explanation
I'll start with what I initially had:
product.map(max 1.read.pure)
This is a point-free expression that evaluates to a function taking a string (or a list of characters) s ("301"
) as an argument. It maps max 1.read.pure
over s, essentially taking each character i, injecting it into a list (which makes it a string) (["3", "0", "1"]
), then reading it, which evaluates the string ([3, 0, 1]
) and finally taking the greater of i and 1 ([3, 1, 1]
). Then it takes the product
of the resulting list of integers (3
).
I then golfed it by a byte with:
foldr((*).max 1.read.pure)1
This works because product
is equivalent to foldr (*) 1
. Instead of mapping and folding, I combined the two by folding with (*).max 1.read.pure
which takes each non-zero digit and multiplies it with the accumulator.
Python 2, 34 bytes
f=lambda n:n<1or(n%10or 1)*f(n/10)
Try it online!
Jelly, 4 bytes
Do1P
Try it online! or see the test suite
How it works
Do1P - Main link. Argument: n (integer) e.g. 1230456
D - Digits [1, 2, 3, 0, 4, 5, 6]
o1 - Replace 0 with 1 [1, 2, 3, 1, 4, 5, 6]
P - Product 720