Skip to main content
added 193 characters in body
Source Link
pajonk
  • 18.3k
  • 3
  • 26
  • 74

R, 4742 bytes

function(x,n=0:-1)fory=seq(i in x)show(n[i]<-n[i]+2*2){x[x]=y-1;x[!x]=y;x}

Try it online!Try it online!

-8 bytes thanks to @Dominic

Takes input incremented by 1as booleans: 2TRUE  (for 1) and 1FALSE  (for 0).

Straightforward approach, but takes advantage of truncating the replacement to the length of items being replaced.


More straightforwardDifferent approach:

R, 5047 bytes

function(x){x[x]=1,n=0:sum-1)for(i in x)*2-1;x[!x]=1:sumshow(!xn[i]<-n[i]+2)*2;x}

Try it online!Try it online!

Takes input as booleansincremented by 1: TRUE2  (for 1) and FALSE1  (for 0).

R, 47 bytes

function(x,n=0:-1)for(i in x)show(n[i]<-n[i]+2)

Try it online!

Takes input incremented by 1: 2  (for 1) and 1  (for 0).


More straightforward approach:

R, 50 bytes

function(x){x[x]=1:sum(x)*2-1;x[!x]=1:sum(!x)*2;x}

Try it online!

Takes input as booleans: TRUE(1) and FALSE(0).

R, 42 bytes

function(x,y=seq(x)*2){x[x]=y-1;x[!x]=y;x}

Try it online!

-8 bytes thanks to @Dominic

Takes input as booleans: TRUE(1) and FALSE(0).

Straightforward approach, but takes advantage of truncating the replacement to the length of items being replaced.


Different approach:

R, 47 bytes

function(x,n=0:-1)for(i in x)show(n[i]<-n[i]+2)

Try it online!

Takes input incremented by 1: 2  (for 1) and 1  (for 0).

Source Link
pajonk
  • 18.3k
  • 3
  • 26
  • 74

R, 47 bytes

function(x,n=0:-1)for(i in x)show(n[i]<-n[i]+2)

Try it online!

Takes input incremented by 1: 2 (for 1) and 1 (for 0).


More straightforward approach:

R, 50 bytes

function(x){x[x]=1:sum(x)*2-1;x[!x]=1:sum(!x)*2;x}

Try it online!

Takes input as booleans: TRUE(1) and FALSE(0).