# The n-th Ternary

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.

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

# 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"


# Stax, 12 bytes

å■Åxb◄34Γφ¥r


Run and debug it

# Zsh, 50 bytes

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


Try it online!