22
\$\begingroup\$

I define the n-th ternary as a ternary that returns n and has the form:

1 ? 2 ? 3 ? n - 1 ? n : 0 : 0 : 0  # n - 1 zeroes

Write a function or complete program that given an input n will output or return the n-th ternary. Code-Golf.

Testcases

0 #=> undefined behaviour
1 #=> 1
2 #=> 1 ? 2 : 0
3 #=> 1 ? 2 ? 3 : 0 : 0
10 #=> 1 ? 2 ? 3 ? 4 ? 5 ? 6 ? 7 ? 8 ? 9 ? 10 : 0 : 0 : 0 : 0 : 0 : 0 : 0 : 0 : 0

The 1000-th ternary I think there is some kind of zen harmony to it.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Is trailing whitespace allowed? \$\endgroup\$ Jul 9, 2015 at 19:15
  • \$\begingroup\$ @rink no, no training space \$\endgroup\$
    – Caridorc
    Jul 9, 2015 at 19:39
  • 1
    \$\begingroup\$ Since "ternary" means 3, shouldn't you name it the "n-ary", which is what it's referred to in mathematics? \$\endgroup\$
    – mbomb007
    Jul 9, 2015 at 21:10
  • 4
    \$\begingroup\$ The way to "edit" a comment is: delete it, and add a new one. \$\endgroup\$ Jul 9, 2015 at 21:15
  • 2
    \$\begingroup\$ @RetoKoradi You can edit a comment if it's within five minutes of posting it. \$\endgroup\$
    – mbomb007
    Jul 9, 2015 at 21:48

33 Answers 33

1
2
0
\$\begingroup\$

Jelly, 12 bytes

Rj“:?0”s2ZFK

Try it online!

How it works

Rj“:?0”s2ZFK - Main link. Takes n on the left
R            - Yield the range [1, 2, ..., n]
 j“:?0”      - Join by “:?0”; [1, ":", "?", "0", 2, ..., ":", "?", "0", n]
       s2    - Split into pairs; [[1, ":"], ["?", "0"], ..., ["?", "0"], [n]]
         Z   - Transpose; [[1, "?", ..., "?", n], [":", "0", ..., ":", "0"]]
          F  - Flatten; [1, "?", 2, ..., "?", n, ":", "0", ..., ":", "0"]
           K - Join by spaces; "1 ? 2 ... ? n : 0 ... : 0"
\$\endgroup\$
0
\$\begingroup\$

Stax, 12 bytes

å■Åxb◄34Γφ¥r

Run and debug it

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

Zsh, 50 bytes

s=${(j: ? :)$(seq $1)}
repeat $1-1 s+=' : 0'
<<<$s

Try it online!

\$\endgroup\$
1
2

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.