194
\$\begingroup\$

In mathematics an exclamation mark ! often means factorial and it comes after the argument.

In programming an exclamation mark ! often means negation and it comes before the argument.

For this challenge we'll only apply these operations to zero and one.

Factorial
0! = 1
1! = 1

Negation
!0 = 1
!1 = 0

Take a string of zero or more !'s, followed by 0 or 1, followed by zero or more !'s (/!*[01]!*/).
For example, the input may be !!!0!!!! or !!!1 or !0!! or 0! or 1.

The !'s before the 0 or 1 are negations and the !'s after are factorials.

Factorial has higher precedence than negation so factorials are always applied first.
For example, !!!0!!!! truly means !!!(0!!!!), or better yet !(!(!((((0!)!)!)!))).

Output the resultant application of all the factorials and negations. The output will always be 0 or 1.

Test Cases

0 -> 0
1 -> 1
0! -> 1
1! -> 1
!0 -> 1
!1 -> 0
!0! -> 0
!1! -> 0
0!! -> 1
1!! -> 1
!!0 -> 0
!!1 -> 1
!0!! -> 0
!!!1 -> 0
!!!0!!!! -> 0
!!!1!!!! -> 0

The shortest code in bytes wins.

\$\endgroup\$
18
  • 26
    \$\begingroup\$ But 0!=1!, so what's the point of handling multiple factorials? \$\endgroup\$
    – boboquack
    Feb 6, 2017 at 9:22
  • 38
    \$\begingroup\$ @boboquack Because that's the challenge. \$\endgroup\$ Feb 6, 2017 at 9:24
  • 14
    \$\begingroup\$ <?='1'; ... correct 75% of the time in php. \$\endgroup\$
    – aslum
    Feb 6, 2017 at 17:17
  • 12
    \$\begingroup\$ I may be wrong here but can't any number with any factorials after it simply be removed and replaced with 1? Like 0!!!! = 1!! = 0!!!!!!!! = 1!!! = 1! = 0! = 1 etc \$\endgroup\$ Feb 6, 2017 at 22:33
  • 3
    \$\begingroup\$ @AlbertRenshaw That is correct. \$\endgroup\$ Feb 6, 2017 at 22:54

62 Answers 62

1 2
3
0
\$\begingroup\$

Julia 0.7, 28 bytes

~s=s[1]<'0'?!~s[2:end]:s>"0"

Try it online!

called with ~"!!0!", returns true or false

ungolfed:

function f(s)
    if s[1] == '!'
        return !f(s[2:end])
    else
        return s>"0"
    end
end

s>"0" is false only for s=="0"

\$\endgroup\$
0
\$\begingroup\$

Lexurgy, 47 bytes

f:
{\0,\1} \!+=>\1
e:
\!\!=>*
g:
\! {\0,\1}=>* {\1,\0}

Explanation

# strip factorials
f:
{\0,\1} \!+=>\1

# !!X = X
e:
\!\!=>*

# apply !
g:
\! {\0,\1}=>* {\1,\0}
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.