29
\$\begingroup\$

Jelly has an "untruth" atom: . This takes a non-empty array of positive integers and returns a Boolean array with 1s at the indexes in the input. For example:

[1,3,5,6]Ṭ ⁼ [1,0,1,0,1,1]
[5]Ṭ       ⁼ [0,0,0,0,1]
[2,1,1,2]Ṭ ⁼ [1,1]
[5,4,3]Ṭ   ⁼ [0,0,1,1,1]
[1]Ṭ       ⁼ [1]

Try it online!

Note that Jelly uses 1-indexing, and that duplicate values in the array have no effect.

Your job is to take a non-empty array of positive integers and output the result of applying on the array. If your language has a builtin with this exact behaviour, you may not use it. You may also choose to use zero indexing if you wish (so the above examples become [0,2,4,5], [4], [1,0,0,1] etc) and take non-negative integers in the input array.

You may use your language’s true and false values instead of 1 and 0, so long as they are the Boolean values rather than general truthy and falsey values (e.g. 0 and non-zero integers). You may also take input as a set if your language has that type. The input is not guaranteed to be sorted and may be in any order. The output may not have trailing zeros (or any other "trailing" values).

This is so the shortest code in bytes wins.

\$\endgroup\$
22
  • 1
    \$\begingroup\$ Sandbox. Imaginary brownies for beating my 9 byte Add++ answer (below), and extra imaginary brownies for beating (or tying) my 3 byte Jelly answer \$\endgroup\$ – caird coinheringaahing Dec 22 '20 at 2:38
  • 1
    \$\begingroup\$ Is the input guaranteed to be non-empty? \$\endgroup\$ – xnor Dec 22 '20 at 2:40
  • 3
    \$\begingroup\$ @cairdcoinheringaahing I was hoping for a "no", but thought to ask because one approach that occurred to me would result in infinite trailing zeroes \$\endgroup\$ – Unrelated String Dec 22 '20 at 2:56
  • 3
    \$\begingroup\$ @LiefdeWen Banning builtins is generally discouraged because it’s usually too vague to be objective. Stuff like “you may not use prime related builtins” for example. Here, I’ve banned this exact builtin to avoid trivialising the challenge (e.g. Jelly, 1 byte being an answer). Take a look at Bubbler’s answer to see how a similar builtin is used because only this exact builtin is banned \$\endgroup\$ – caird coinheringaahing Dec 22 '20 at 11:24
  • 2
    \$\begingroup\$ @a25bedc5-3d09-41b8-82fb-ea6c353d75ae I’m going to say no on that one, as a set is inherently unique by definition, whereas a list can contain repeated values \$\endgroup\$ – caird coinheringaahing Dec 22 '20 at 15:10

38 Answers 38

1
2
2
\$\begingroup\$

jq, 30 29 27 bytes

range(max)as$a|[$a+1]-.==[]

Try it online!

Explanation

range(                      # the 0-range up to
  max)                      # the largest item of the input - 1
as$a                        # And assign it to $a

|                           # And then, for every item in $a:
[$a+1]-.==[]                #     Is the item + 1 contained in the original input?
\$\endgroup\$
2
\$\begingroup\$

Zsh, 44 bytes

for i ({1..${${(nO)@}[1]}})<<<$[$@[(I)$i]>0]

Try it online!

Explanation:

             ${(nO)@}                          # sort the arguments numerically, in reverse
           ${        [1]}                      # take the first (i.e. maximum value)
       {1..              }                     # range from 1 to that
for i (                   )                    # for each i
                                @[(I)$i]       # find the index of `i` in `argv`, or 0 if not present
                              $[        >0]    # is that positive? (1 or 0)
                           <<<                 # print
\$\endgroup\$
1
2
\$\begingroup\$

Nim, 60 58 bytes

func t[S](n:S):S=
 for i in 1..n.max:result.add int i in n

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Vyxal, 6 bytes

Gɾƛ?$c

Try it Online!

Gɾƛ?$c
G      Maximum
 ɾ     Range
  ƛ    Map:
   ?     Push the input
    $    Swap
     c   Contains?
\$\endgroup\$
3
  • \$\begingroup\$ Try it Online! for 4 bytes \$\endgroup\$ – lyxal May 15 at 13:16
  • \$\begingroup\$ @lyxal Is there a documentation on command line flags somewhere? I was actually wondering why something like that doesn't work (without the flag)… \$\endgroup\$ – xigoi May 15 at 14:05
  • 1
    \$\begingroup\$ Try it Online! for a list of flags \$\endgroup\$ – lyxal May 15 at 22:29
1
\$\begingroup\$

Retina 0.8.2, 51 bytes

.+
$*01
+`^((.)*).?(.*)¶(?<-2>.)*(?(2)$)(.+)
$1$4$3

Try it online! Takes input as a newline-separated list but link includes test suite that converts from comma-separated for ease of use. Outputs a binary string. 0-indexed. Explanation:

.+
$*01

Convert each entry into a string of 0s followed by a 1.

+`

For each additional entry, its 1 is merged into the first line in turn:

^((.)*)

Match the prefix on the first line.

.?(.*)¶

Skip the digit above the 1, if any, but keep the suffix of the first line.

(?<-2>.)*(?(2)$)

Match a prefix of the same length on the second line.

(.+)

Match the any 0s needed for padding and the 1.

$1$4$3

If the first line was shorter than the second line, then append the suffix of the second line to the first line, otherwise insert the 1 from the second line in between the prefix and suffix of the first line.

Example 1: When merging 10001 with 001, the prefix is two characters, 10 (captured as $1) on the first line corresponding to 00 on the second line. The 1 (captured as $4) from the second line is then inserted, and then the suffix 01 on the first line (captured as $3) is appended, resulting in 10101.

Example 2: When merging 101 with 00001, the prefix is three characters 101 (captured as $1) on the first line corresponding to 000 on the second line. The 01 (captured as $4) from the second line is then appended ($3 is empty as the first line is shorter than the second), again resulting in 10101.

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 9 bytes

⭆⊕⌈θ∧№θι¹

Try it online! Link is to verbose version of code. Outputs a binary string. 0-indexed. Explanation:

   θ        Input array
  ⌈         Maximum
 ⊕          Incremented
⭆           Map over implicit range and join.
     №      Count of
       ι    Current index
      θ     In input array
    ∧       Logical Or
        ¹   Literal `1`
            Implicitly print

1-indexed version is also 9 bytes:

⭆⌈θ∧№θ⊕ι¹

Try it online! Link is to verbose version of code. Explanation: Same as the first version, except that the increment is in a different place.

\$\endgroup\$
1
\$\begingroup\$

Haskell, 52 49 bytes

t=foldl(#)[]
l#i=take i(l++[0,0..])++1:drop(i+1)l

Try it online!

  • 0 indexed
\$\endgroup\$
1
\$\begingroup\$

FALSE, 183 bytes

0$$b:c:d:[^$1_=~][$32=$[%%0a:[a;b;=~][a;1\[$0=~][\10*\1-]#%*a;0=~[+]?a;1+$b;=~[@\]?a:]#0b:c;1+c:$d;>[$d:]?1_]?~[b;1+b:48-]?]#%[b;d;>~][0$e:[$1=~e;c;=~&][e;1+$e:øb;=[%1]?]#.32,b;1+b:]#

Try it online!

NOTE: Since FALSE does not actually have arrays, input is space-separated numbers with one space at the end, and output is the same.

Most of this answer is just input string to int.

0 indexed.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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