| Bytes | Lang | Time | Link |
|---|---|---|---|
| 024 | Juby rset | 250515T143507Z | Jordan |
| 9483 | Rust | 231123T001204Z | 138 Aspe |
| 013 | TIBASIC | 231119T151428Z | Youserna |
| 033 | Noulith | 231117T150035Z | bigyihsu |
| 112 | Go 1.21+ | 231030T135824Z | bigyihsu |
| 004 | Uiua | 231030T123228Z | chunes |
| 003 | Thunno 2 | 230725T125050Z | The Thon |
| 005 | Pyt | 230220T023335Z | Kip the |
| 033 | PowerShell | 230220T000935Z | Julian |
| 029 | Arturo | 230219T224730Z | chunes |
| 038 | Desmos | 220629T005521Z | Aiden Ch |
| 095 | HBL | 220628T225718Z | DLosc |
| 030 | Haskell | 210514T223309Z | Delfad0r |
| 006 | Vyxal | 210514T212754Z | Adam |
| 058 | Nim | 201222T083034Z | Adam |
| 044 | Zsh | 201222T075354Z | pxeger |
| 027 | jq | 210101T055405Z | user9915 |
| 049 | C# Visual C# Interactive Compiler | 201222T073127Z | LiefdeWe |
| 016 | x86_64 zero indexed | 201225T041652Z | EasyasPi |
| 183 | FALSE | 201227T204216Z | ReedsSho |
| 003 | 05AB1E | 201222T032927Z | lyxal |
| 044 | JavaScript Node.js | 201225T041357Z | GOTO 0 |
| 004 | MATL | 201224T162012Z | Luis Men |
| 007 | Gaia | 201223T170351Z | Giuseppe |
| 049 | Haskell | 201223T134001Z | AZTECCO |
| 023 | R | 201222T172817Z | Dominic |
| 087 | C gcc with m32 | 201223T124626Z | ErikF |
| 006 | Japt | 201222T223428Z | Shaggy |
| 024 | R | 201222T122846Z | pajonk |
| 035 | Factor | 201223T023646Z | Bubbler |
| 025 | Zsh | 201222T231804Z | GammaFun |
| 009 | Add++ | 201222T023700Z | caird co |
| 021 | Julia 1.0 | 201222T170540Z | MarcMush |
| 045 | PowerShell | 201222T165402Z | Zaelin G |
| 017 | Octave | 201222T161631Z | Luis Men |
| 012 | J | 201222T082046Z | Galen Iv |
| 082 | Java JDK | 201222T152049Z | Olivier |
| 019 | Scala | 201222T141056Z | user |
| 009 | Charcoal | 201222T110832Z | Neil |
| 050 | JavaScript ES6 | 201222T114737Z | Arnauld |
| 051 | Retina 0.8.2 | 201222T110250Z | Neil |
| 041 | Python 3 | 201222T071426Z | pxeger |
| 005 | Neim | 201222T064832Z | LiefdeWe |
| 020 | Wolfram Language Mathematica | 201222T035816Z | att |
| 011 | Brachylog | 201222T035249Z | Unrelate |
| 033 | Ruby 2.7 | 201222T025704Z | vrintle |
| 003 | APL Dyalog Extended | 201222T025004Z | Bubbler |
| 007 | Husk | 201222T031259Z | Razetime |
| 003 | Jelly | 201222T030555Z | Unrelate |
| 033 | Perl 5 | 201222T030328Z | Xcali |
J-uby -rset, 24 bytes
Takes a Set as input, returns an array of true and false.
:*%[:& &:===,:max|:!~&0]
Explanation
:* % [:& & :===, :max | :!~ & 0]
:max | :!~ & 0 # Range 0..(input.max)
:* % [ , ] # Map with...
:& & :=== # Membership in input
Rust, 94 83 bytes
Saved 11 bytes thanks to @att
Golfed version. Attempt This Online!
|a:Vec<_>|->Vec<_>{(1..=*a.iter().max().unwrap()).map(|i|a.contains(&i)).collect()}
Ungolfed version. Attempt This Online!
fn f(a: Vec<usize>) -> Vec<bool> {
let max_val = *a.iter().max().unwrap();
(0..max_val).map(|i| a.contains(&(i+1))).collect()
}
TI-BASIC, 13 bytes
seq(max(I=Ans),I,1,max(Ans
Takes input in Ans.
Go 1.21+, 112 bytes
import."slices"
func f(I[]int)(o[]int){
for i:=1;i<=Max(I);i++{e:=0
if Contains(I,i){e=1}
o=append(o,e)}
return}
Uses the new slices.Max function from the standard library, which doesn't exist pre-1.21.
Uiua, 4 bytes
>0⍘⊚
0-indexed
>0⍘⊚
⍘⊚ # inverse where (occurrences)
>0 # where is it greater than zero?
Thunno 2, 3 bytes
GRƇ
Explanation
GRƇ # Implicit input
G # Maximum
R # Range
Ƈ # Contains
# Implicit output
Pyt, 5 bytes
Đ↑ř⇹∈
Đ implicit input; Đuplicate
↑ get maximum
ř řangify
⇹∈ is each element in [1,2,...,max] in the input?; implicit print
PowerShell, 33 bytes
1..($a=$args|sort)[-1]|%{$_-in$a}
Takes in an array of positive integer, returns an array of booleans.
I stole the test cases from Zaelin's answer, thank you!
Explanation
1..($a=$args|sort)[-1] # Generate a range from 1 to the maximum number in the input
|%{$_-in$a} # If the number is in the input list, returns $true, otherwise $false
HBL, 9.5 bytes
'?(*(*-,.
*().(0(/.
1-indexed. Try it here!
Explanation
The helper function on the first line takes two arguments: a list of indices and a specific index. It returns 1 if the index appears in the list, 0 if it does not.
'?(*(*-,.
(* ) For each element of
. the list,
- subtract it from
, the index
(* ) Take the product of the resulting list
'? Is the product zero?
The main function takes a list of indices and returns the "untruthed" list of 0's and 1's:
*().(0(/.
(0 ) Range from 1 to
(/ ) the maximum of
. the list
* For each element,
() call the helper function
. with the index list as first argument
and the element of the range as second argument
Haskell, 30 bytes
f a=map(`elem`a)[1..maximum a]
The relevant function is f, which takes as input a list of integers a (1-indexed) and returns a list of Bools.
Zsh, 44 bytes
for i ({1..${${(nO)@}[1]}})<<<$[$@[(I)$i]>0]
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
jq, 30 29 27 bytes
range(max)as$a|[$a+1]-.==[]
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?
C# (Visual C# Interactive Compiler), 49 bytes
a=>Enumerable.Range(1,a.Max()).Select(a.Contains)
Saved 3 bytes thanks to caird
Saved 1 byte thanks to didymus
Saved 2 bytes thanks to NonlinearFruit
x86_64 (zero indexed, length given), 19 16 bytes
Raw machine code:
31 c0 57 f3 aa 5f 89 d1 8d c6 04 07 01 e2 f9 c3
Uncommented assembly:
.intel_syntax noprefix
.globl untruth
untruth:
xor eax, eax
push rdi
rep stosb byte ptr [rdi]
pop rdi
mov ecx, edx
.Lloop:
lodsd dword ptr [rsi]
mov byte ptr [rdi + rax], 1
loop .Lloop
.Lend:
ret
Explanation
I'm not too good at x86, so I am pretty sure there is a better way to do this.
C signature:
// System V ABI (rdi, rsi, rdx, rcx)
void untruth(bool *out, const uint32_t *indices, uint32_t indices_len, uint32_t out_len);
It's my function, I can order the parameters however I please. 😏
First: memset(out, 0, out_len) using rep stosb. Since we need to save the pointer and stosb clobbers it, we push and pop it.
The standard calling conventions say that the direction flag is always cleared when calling, so we know this will be a forwards operation.
untruth:
xor eax, eax
push rdi
rep stosb byte ptr [rdi]
pop rdi
Using the fancy loop instruction, loop through each index in the array, storing 1 to out[index]
mov ecx, edx
.Lloop:
lodsd dword ptr [rsi]
mov byte ptr [rdi + rax], 1
loop .Lloop
At the end of the loop, return.
.Lend:
ret
Note: It also happens to be x86-compatible on the binary level.
Thanks to Neil for the -3 bytes (using lodsd)!
x86_64 (zero indexed, calculates max), 28 bytes
Raw machine code:
31 c0 57 51 af 0f 42 47 fc e2 f9 91 56 56 5f f3
aa 5f 59 5e ad c6 04 07 01 e2 f9 c3
Assembly:
.intel_syntax noprefix
.globl untruth
# void untruth(const uint32_t *indices{rdi}, char *out{rsi}, uint32_t indices_len{rcx})
untruth:
xor eax, eax
push rdi
push rcx
.Lfind_max:
scasd eax, dword ptr [rdi]
cmovb eax, dword ptr [rdi - 4]
loop .Lfind_max
.Lfind_max.end:
xchg ecx, eax
push rsi
push rsi
pop rdi
rep stos byte ptr [rdi], al
pop rdi
pop rcx
pop rsi
.Lloop:
lodsd eax, dword ptr [rsi]
mov byte ptr [rdi + rax], 1
loop .Lloop
.Lloop_end:
ret
This version will check for the maximum itself, but the output buffer provided must be large enough.
Probably many things here can be optimized.
Explanation
Note that the parameters are different than the first: indices is in rdi, out is in rsi, rdx is unused, and indices_len is in rcx.
I don't know why scasd uses rdi but whatever.
This is a simple max loop. It compares each dword in indices, and sets eax to the maximum.
This seems to be smaller than doing something with lodsd, although that 4 byte cmovb is pretty yucky.
untruth:
xor eax, eax
push rdi
push rcx
.Lfind_max:
scasd eax, dword ptr [rdi]
cmovb eax, dword ptr [rdi - 4]
loop .Lfind_max
Since we know ecx will be zero due to the loop condition, we can set ecx to the maximum and set eax to zero in one byte.
.Lfind_max.end:
xchg ecx, eax
Unfortunately, our output array is in rsi, not rdi. We push and pop twice to mov without the REX tax and save a copy, then do a memset with rep stosb.
push rsi
push rsi
pop rdi
rep stos byte ptr [rdi], al
pop rdi
Now we need to get indices and indices_len from the stack. Note that this time, we put indices into rsi.
pop rcx
pop rsi
For each dword in indices, out[indices[i]] to 1 using lodsd and loop
.Lloop:
lodsd eax, dword ptr [rsi]
mov byte ptr [rdi + rax], 1
loop .Lloop
Return.
.Lloop_end:
ret
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:]#
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.
05AB1E, 4 3 bytes
ZLå
Yes it is just a port of every other answer.
Explained
ZLå
ZL # Push the range: [1, max(input)]
å # Vectorise: is item in input? over that range
JavaScript (Node.js), 45 44 bytes
a=>a.map(b=>c[b]=1,c=[])&&[...c].map(d=>d|0)
This implementation uses zero indexing and returns an array of 0s and 1s.
I think the code isn't very difficult to understand. Here's a summary explanation:
c=[]: create the output array.a.map(b=>c[b]=1 ): for each value of the input array, set 1 at the respective index in the output array.[...c].map(d=>d|0): convert the output array into a non-sparse array, then map each element to a 32-bit integer. This will map1to1andundefinedto0.
Thanks to Neil for -1 byte.
MATL, 4 bytes
v1i(
Try it online! Or verify all test cases.
Explanation
v % Concatenate stack contents vertically. Gives an empty array
1 % Push 1
i % Take input
( % Assignment indexing: write 1 into the array at the input positions.
% This automatically extends the array and fills with 0
% Implicit display
Gaia, 7 bytes
e:⌉┅¤Ė¦
Identical to, e.g., pajonk's R answer.
e: # eval implicit input as a list and duplicate
⌉┅ # take the max of the list and compute [1...M]
¤ # swap so [1..M] is on the bottom
˦ # For each element of [1..M], is it in the input list?
R, 24 23 bytes
Edit: -1 byte thanks to pajonk
F[scan()]=1;F&!is.na(F)
A different approach to pajonk's R answer.
Shorter at time of posting, but this is a precarious situation, as it would be longer in a fair 'apples-with-apples' comparison (using scan() for both approaches).
C (gcc) with -m32, 87 bytes
Zero-indexed, using -1 as a sentinel value for arrays.
To get the size of the output array, I recursively scan the input array and allocate the output array with the maximum size found (plus one for the sentinel.) After that, each input index is marked and the resulting array is returned.
*j;*g(i,m)int*i;{~*i?g(i+1,m<*i?*i:m)[*i]=1:(j=calloc(++m+1,4))[m]--;i=j;}f(i){g(i,0);}
R, 32 25 24 bytes
function(x)1:max(x)%in%x
Taking advantage of %in% operator and abusing weird precedence.
-1 thanks to Dominic van Essen
21 bytes using scan
1:max(x<-scan())%in%x
Factor, 35 bytes
[ 0 [ 2^ bitor ] reduce make-bits ]
Takes a sequence of 0-based indices and returns a virtual boolean sequence of t (true) and f (false).
How it works
[
0 [ 2^ bitor ] reduce ! Reduce all 2^n bitmasks by bitwise OR
! for each n in the sequence
make-bits ! Create a virtual sequence of bits from the integer
]
Zsh, 25 bytes
for x;a[x]=1
<<<${a/#%/0}
The # and % in globs act like the regex ^ and $ anchors, but for full words instead of lines.
Altenatively, <<<${a:///0} works.
Add++, 9 bytes
L,dbMR$€e
Given that no-one besides me really uses Add++, I figured I wouldn't be sniping anyone
How it works
L,dbMR$€e - Anonymous lambda function
L, - Define the lambda function. Takes l as input
d - Duplicate; STACK = [l l]
bM - Maximum; STACK = [l max(l)]
R - Range; STACK = [l [1 2 ... max(l)]]
$ - Swap; STACK = [[1 2 ... max(l)] l]
€ - Over each:
e - In l?
PowerShell, 45 bytes
1-indexed, which felt weird to do in a 0-indexed language. Accepts only non-negative integers; negative indexing in powershell is 1-based, so this answer breaks down for negative cases.
$a=,0*($args|sort)[-1];$args|%{$a[$_-1]=1};$a
Octave, 17 bytes
@(x){y(x)=1,y}{2}
Anonymous function that takes a row (or column) vector as input and produces a row vector as output.
Uses the last trick on this list to effectively include several statements in an anonymous function.
J, 12 bytes
e.~1 i.@+>./
0-indexed; similar to Bubbler's APL solution
K (oK), 14 12 bytes
-2 bytes thanks to coltim
{~^x?!1+|/x}
0-indexed
Java (JDK), 82 bytes
a->{int m=0,r[];for(int i:a)m=i>m?i:m;r=new int[m];for(int i:a)r[i-1]=1;return r;}
Scala, 19 bytes
i=>1 to i.max map i
Returns a Seq of Booleans. Takes a Set[Int] as input, since sets are also predicates in Scala. If a list is taken as input, i.contains would have to be used instead.
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.
JavaScript (ES6), 50 bytes
a=>a.map(g=(x,i)=>x&&g(--x,g,o[x]|=++i/i),o=[])&&o
Commented
a => // a[] = input array
a.map( // for each ...
g = (x, i) => // ... value x at position i in a[]:
x && // if x is not equal to 0:
g( // do a recursive call:
--x, // decrement x
g, // force i to a non-numeric value
o[x] |= // update o[x]:
++i / i // set it to 1 if i is numeric (≥ 0)
// or just coerce it to a number otherwise
// (i.e. undefined values are turned into 0's)
), // end of recursive call
o = [] // start with o[] = empty array
) && o // end of map(); return o[]
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.
Neim, 5 bytes
𝐠𝐈Γ₁𝕚
Explanation:
𝐠 # Get Greatest element
𝐈 # Inclusive range: (0 .. n]
Γ # For each element in range do:
₁ # 1st input line
𝕚 # Check that the int is in the list
Brachylog, 11 bytes
{~l,1}ᵐz₁⌉ᵐ
It's certainly interesting to consider what approach is the best in a language without a concept of a Boolean. 0-indexed. For some reason the unbound variables that are pretty much 0 are actually displaying as variables, so tack a ≜ onto the end if that's a problem.
{ }ᵐ For each element of the input:
,1 append 1 to
~l something that long.
z₁ Ragged zip. (i.e. non-cycling, doesn't stop until all lists are exhausted)
⌉ᵐ Take the maximum of each column.
Ruby 2.7, 33 bytes
->a,n=[0]*a.max{a.map{n[_1]=1};n}
Uses the 0-indexing. TIO uses an older version of Ruby, whereas in Ruby 2.7, we've numbered parameters, which saves two bytes.
APL (Dyalog Extended), 3 bytes
ׯ⍸
A tacit function. Banning only the exact built-in actually gives APL a massive advantage!
How it works
ׯ⍸
⍸ ⍝ Takes a vector v and gives another vector containing v[i] copies of i
⍝ for each index i
¯ ⍝ Inverse of the above, which counts occurrences of i which becomes v[i]
× ⍝ Signum of each number, converting any positive count to 1
APL (Dyalog Unicode), 7 bytes
⊢∊⍨∘⍳⌈/
(⍳⌈/)∊⊢
Non-Extended solution. Works exactly like the 3-byte Jelly solution.
APL (Dyalog Unicode), 7 bytes
∨⌿-↑⍤0×
A fun way to do the job. For each number n, create a length-n vector that has a 1 at the end and 0 for the rest. Then promote the entire array to a matrix (padding as necessary) and take the logical OR of the rows.
Husk, 7 bytes
mo±€¹ḣ▲
I feel like I'm missing a way to do this with η.
Explanation
mo±€¹ḣ▲
ḣ▲ range 1..max(input)
mo map each to
€¹ whether it's present in the input(index if present, 0 if not)
± and take the sign of that
Jelly, 3 bytes
Ṁe€
This does seem somewhat difficult to outdo.
€ Map over (the range from 1 to)
Ṁ the largest element of the input:
e is it in the input?
Silly, 0-indexed bonus:
Jelly, 6 bytes
2*BUo/
2* For each element of the input, raise 2 to that power.
B Convert to binary
U and reverse each,
/ then reduce by
o vectorizing logical OR.
Perl 5, -pa 33 bytes
@r[@F]=(1)x@F;map$_+=0,@r;$_="@r"
0 indexed. Input and output are space separated.