Skip to main content
deleted 1 character in body
Source Link
Maël
  • 419
  • 2
  • 9

R, 5648 bytes

-2 bytes thanks to Giuseppe. -1 byte thanks to Dominic van Essen.

function\(v,x) sequence(d<-diff(c(0,which(v==x))),d-1,-1)

output

f(int, 0)
# [1] 2 1 0 2 1 0 0
f(int, 1)
# [1] 0 0
f(int, 2)
# [1] 4 3 2 1 0 2 1 0
f(int, 3)
# integer(0)

f(char, "A")
# [1] 0 2 1 0 0
f(char, "B")
# [1] 1 0
f(char, "C")
# [1] 2 1 0
f(char, "D")
# integer(0)

Works for R >= 4.01.0.

R, 56 bytes

-2 bytes thanks to Giuseppe.

function(v,x) sequence(d<-diff(c(0,which(v==x))),d-1,-1)

output

f(int, 0)
# [1] 2 1 0 2 1 0 0
f(int, 1)
# [1] 0 0
f(int, 2)
# [1] 4 3 2 1 0 2 1 0
f(int, 3)
# integer(0)

f(char, "A")
# [1] 0 2 1 0 0
f(char, "B")
# [1] 1 0
f(char, "C")
# [1] 2 1 0
f(char, "D")
# integer(0)

Works for R >= 4.0.0.

R, 48 bytes

-2 bytes thanks to Giuseppe. -1 byte thanks to Dominic van Essen.

\(v,x)sequence(d<-diff(c(0,which(v==x))),d-1,-1)

output

f(int, 0)
# [1] 2 1 0 2 1 0 0
f(int, 1)
# [1] 0 0
f(int, 2)
# [1] 4 3 2 1 0 2 1 0
f(int, 3)
# integer(0)

f(char, "A")
# [1] 0 2 1 0 0
f(char, "B")
# [1] 1 0
f(char, "C")
# [1] 2 1 0
f(char, "D")
# integer(0)

Works for R >= 4.1.0.

added 27 characters in body
Source Link
Maël
  • 419
  • 2
  • 9

R, 5856 bytes

-2 bytes thanks to Giuseppe.

function(v,x){d=diff sequence(d<-diff(c(0,which(v==x)))
sequence(d,d-1,-1)}

output

f(int, 0)
# [1] 2 1 0 2 1 0 0
f(int, 1)
# [1] 0 0
f(int, 2)
# [1] 4 3 2 1 0 2 1 0
f(int, 3)
# integer(0)

f(char, "A")
# [1] 0 2 1 0 0
f(char, "B")
# [1] 1 0
f(char, "C")
# [1] 2 1 0
f(char, "D")
# integer(0)

Works for R >= 4.0.0.

R, 58 bytes

function(v,x){d=diff(c(0,which(v==x)))
sequence(d,d-1,-1)}

output

f(int, 0)
# [1] 2 1 0 2 1 0 0
f(int, 1)
# [1] 0 0
f(int, 2)
# [1] 4 3 2 1 0 2 1 0
f(int, 3)
# integer(0)

f(char, "A")
# [1] 0 2 1 0 0
f(char, "B")
# [1] 1 0
f(char, "C")
# [1] 2 1 0
f(char, "D")
# integer(0)

Works for R >= 4.0.0.

R, 56 bytes

-2 bytes thanks to Giuseppe.

function(v,x) sequence(d<-diff(c(0,which(v==x))),d-1,-1)

output

f(int, 0)
# [1] 2 1 0 2 1 0 0
f(int, 1)
# [1] 0 0
f(int, 2)
# [1] 4 3 2 1 0 2 1 0
f(int, 3)
# integer(0)

f(char, "A")
# [1] 0 2 1 0 0
f(char, "B")
# [1] 1 0
f(char, "C")
# [1] 2 1 0
f(char, "D")
# integer(0)

Works for R >= 4.0.0.

Source Link
Maël
  • 419
  • 2
  • 9

R, 58 bytes

function(v,x){d=diff(c(0,which(v==x)))
sequence(d,d-1,-1)}

output

f(int, 0)
# [1] 2 1 0 2 1 0 0
f(int, 1)
# [1] 0 0
f(int, 2)
# [1] 4 3 2 1 0 2 1 0
f(int, 3)
# integer(0)

f(char, "A")
# [1] 0 2 1 0 0
f(char, "B")
# [1] 1 0
f(char, "C")
# [1] 2 1 0
f(char, "D")
# integer(0)

Works for R >= 4.0.0.