Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 86 characters in body
Source Link
digEmAll
  • 5.1k
  • 15
  • 22

R, 95(削除) 95 (削除ここまで), 94 bytes

Assuming combn always returns sorted indexes

At the moment the assumption is valid in all R versions, but is not documented; the alternative (longer) solution is reported at the bottom of the answer

  • -1 byte thanks to @Dominc van Essen
function(v,s,r=v,`+`=length){for(x in combn(+v,+s,,F))if(all(v[x]==s)&+r>+&+v[T]>+(a=v[xy=x:x[+x]]x[+x]))r=a;rT=y;v[T]}

Try it online! Try it online!

Unrolled code with explanation:

function(v,s){ # take full vector v and the vector to search s
 r=v # init shortest slice r to full vector v
 for(x in combn(length(v), # for each combination x of length(s) elements taken 
 length(s),,F){ # from the vector of indexes 1:length(v)
 # (we assume x is sorted ascending)
 a=v[x:x[+x]] # get the slice between first and last index in x 
 if(all(v[x]==s) & # if the values of the combinations == s and
 length(r) > length(a)) # the slice is shorter than r
 r=a # store the new slice
 }
 r # return the shortest slice
}

Version without assumption:

R, 107 bytes

function(v,s,r=v,`+`=length){for(x in combn(+v,+s,,F)){y=sort(x);if(all(v[x]==s)&+r>+(a=v[x:x[+x]]))r=a};r}

Try it online!

R, 95 bytes

Assuming combn always returns sorted indexes

At the moment the assumption is valid in all R versions, but is not documented; the alternative (longer) solution is reported at the bottom of the answer

function(v,s,r=v,`+`=length){for(x in combn(+v,+s,,F))if(all(v[x]==s)&+r>+(a=v[x:x[+x]]))r=a;r}

Try it online!

Unrolled code with explanation:

function(v,s){ # take full vector v and the vector to search s
 r=v # init shortest slice r to full vector v
 for(x in combn(length(v), # for each combination x of length(s) elements taken 
 length(s),,F){ # from the vector of indexes 1:length(v)
 # (we assume x is sorted ascending)
 a=v[x:x[+x]] # get the slice between first and last index in x 
 if(all(v[x]==s) & # if the values of the combinations == s and
 length(r) > length(a)) # the slice is shorter than r
 r=a # store the new slice
 }
 r # return the shortest slice
}

R, 107 bytes

function(v,s,r=v,`+`=length){for(x in combn(+v,+s,,F)){y=sort(x);if(all(v[x]==s)&+r>+(a=v[x:x[+x]]))r=a};r}

Try it online!

R, (削除) 95 (削除ここまで), 94 bytes

Assuming combn always returns sorted indexes

At the moment the assumption is valid in all R versions, but is not documented; the alternative (longer) solution is reported at the bottom of the answer

  • -1 byte thanks to @Dominc van Essen
function(v,s,`+`=length){for(x in combn(+v,+s,,F))if(all(v[x]==s)&+v[T]>+(y=x:x[+x]))T=y;v[T]}

Try it online!

Unrolled code with explanation:

function(v,s){ # take full vector v and the vector to search s
 r=v # init shortest slice r to full vector v
 for(x in combn(length(v), # for each combination x of length(s) elements taken 
 length(s),,F){ # from the vector of indexes 1:length(v)
 # (we assume x is sorted ascending)
 a=v[x:x[+x]] # get the slice between first and last index in x 
 if(all(v[x]==s) & # if the values of the combinations == s and
 length(r) > length(a)) # the slice is shorter than r
 r=a # store the new slice
 }
 r # return the shortest slice
}

Version without assumption:

R, 107 bytes

function(v,s,r=v,`+`=length){for(x in combn(+v,+s,,F)){y=sort(x);if(all(v[x]==s)&+r>+(a=v[x:x[+x]]))r=a};r}

Try it online!

added 699 characters in body
Source Link
digEmAll
  • 5.1k
  • 15
  • 22

R, 110 bytes

function(v,s,r=v,`+`=length){combn(+v,+s,function(y,x=sort(y),a=v[x:x[+x]]){if(all(v[x]==s)&+r>+a)r<<-a;0});r}

Try it online!

R , 10095 bytes

At the moment the assumption is valid in all R versions, but is not documented, so...documented; the alternative (longer) solution is reported at the bottom of the answer

function(v,s,r=v,`+`=length){for(x in combn(+v,+s,function(x,a=v[x:x[+x]]F){)if(all(v[x]==s)&+r>+a&+r>+(a=v[x:x[+x]])r<<-a;0});rr=a;r}

Try it online! Try it online!

function(v,s){ # take full vector v and the vector to search s
 r=v # init shortest slice r to full vector v
 for(x in combn(length(v), # for each combination x of length(s) elements taken 
 length(s),  length(s),,F){ # from the vector of indexes 1:length(v)
 function(y){ # execute this function :
 x=sort(y)  # sort the(we indexesassume x (might notis besorted necessary...ascending)
 a=v[x:x[+x]] # get the slice between first and last index in x if(all(v[x]==s) & # if a==sthe and
values of the combinations == s and
 length(r) > length(a)) # the slice is shorter than r
 r=a r<<-a  # store the new slice
 0  # combn requires a return value
 }
 )
 r # return the shortest slice
}

R , 107 bytes

function(v,s,r=v,`+`=length){for(x in combn(+v,+s,,F)){y=sort(x);if(all(v[x]==s)&+r>+(a=v[x:x[+x]]))r=a};r}

Try it online!

R, 110 bytes

function(v,s,r=v,`+`=length){combn(+v,+s,function(y,x=sort(y),a=v[x:x[+x]]){if(all(v[x]==s)&+r>+a)r<<-a;0});r}

Try it online!

R , 100 bytes

At the moment the assumption is valid, but is not documented, so...

function(v,s,r=v,`+`=length){combn(+v,+s,function(x,a=v[x:x[+x]]){if(all(v[x]==s)&+r>+a)r<<-a;0});r}

Try it online!

function(v,s){ # take full vector v and the vector to search s
 r=v # init shortest slice r to full vector v
 combn(length(v), # for each combination of length(s) elements taken 
 length(s),  # from the vector of indexes 1:length(v)
 function(y){ # execute this function :
 x=sort(y)  # sort the indexes x (might not be necessary...)
 a=v[x:x[+x]] # get the slice between first and last index if(all(v[x]==s) & # if a==s and
 length(r) > length(a)) # the slice is shorter than r
 r<<-a  # store the new slice
 0  # combn requires a return value
 }
 )
 r # return the shortest slice
}

R, 95 bytes

At the moment the assumption is valid in all R versions, but is not documented; the alternative (longer) solution is reported at the bottom of the answer

function(v,s,r=v,`+`=length){for(x in combn(+v,+s,,F))if(all(v[x]==s)&+r>+(a=v[x:x[+x]]))r=a;r}

Try it online!

function(v,s){ # take full vector v and the vector to search s
 r=v # init shortest slice r to full vector v
 for(x in combn(length(v), # for each combination x of length(s) elements taken 
 length(s),,F){ # from the vector of indexes 1:length(v)
 # (we assume x is sorted ascending)
 a=v[x:x[+x]] # get the slice between first and last index in x if(all(v[x]==s) & # if the values of the combinations == s and
 length(r) > length(a)) # the slice is shorter than r
 r=a # store the new slice
 }
 r # return the shortest slice
}

R , 107 bytes

function(v,s,r=v,`+`=length){for(x in combn(+v,+s,,F)){y=sort(x);if(all(v[x]==s)&+r>+(a=v[x:x[+x]]))r=a};r}

Try it online!

added 699 characters in body
Source Link
digEmAll
  • 5.1k
  • 15
  • 22

R, 110 bytes

function(v,s,r=v,`+`=length){combn(+v,+s,function(y,x=sort(y),a=v[x:x[+x]]){if(all(v[x]==s)&+r>+a)r<<-a;0});r}

Try it online!

R, 100 bytes

Assuming combn always returns sorted indexes

At the moment the assumption is valid, but is not documented, so...

function(v,s,r=v,`+`=length){combn(+v,+s,function(x,a=v[x:x[+x]]){if(all(v[x]==s)&+r>+a)r<<-a;0});r}

Try it online!

Explanation in progress...

Unrolled code with explanation:

function(v,s){ # take full vector v and the vector to search s
 r=v # init shortest slice r to full vector v
 combn(length(v), # for each combination of length(s) elements taken 
 length(s), # from the vector of indexes 1:length(v)
 function(y){ # execute this function :
 x=sort(y) # sort the indexes x (might not be necessary...)
 a=v[x:x[+x]] # get the slice between first and last index
 if(all(v[x]==s) & # if a==s and
 length(r) > length(a)) # the slice is shorter than r
 r<<-a # store the new slice
 0 # combn requires a return value
 }
 )
 r # return the shortest slice
}

R, 110 bytes

function(v,s,r=v,`+`=length){combn(+v,+s,function(y,x=sort(y),a=v[x:x[+x]]){if(all(v[x]==s)&+r>+a)r<<-a;0});r}

Try it online!

R, 100 bytes

Assuming combn always returns sorted indexes

At the moment the assumption is valid, but is not documented, so...

function(v,s,r=v,`+`=length){combn(+v,+s,function(x,a=v[x:x[+x]]){if(all(v[x]==s)&+r>+a)r<<-a;0});r}

Try it online!

Explanation in progress...

R, 110 bytes

function(v,s,r=v,`+`=length){combn(+v,+s,function(y,x=sort(y),a=v[x:x[+x]]){if(all(v[x]==s)&+r>+a)r<<-a;0});r}

Try it online!

R, 100 bytes

Assuming combn always returns sorted indexes

At the moment the assumption is valid, but is not documented, so...

function(v,s,r=v,`+`=length){combn(+v,+s,function(x,a=v[x:x[+x]]){if(all(v[x]==s)&+r>+a)r<<-a;0});r}

Try it online!

Unrolled code with explanation:

function(v,s){ # take full vector v and the vector to search s
 r=v # init shortest slice r to full vector v
 combn(length(v), # for each combination of length(s) elements taken 
 length(s), # from the vector of indexes 1:length(v)
 function(y){ # execute this function :
 x=sort(y) # sort the indexes x (might not be necessary...)
 a=v[x:x[+x]] # get the slice between first and last index
 if(all(v[x]==s) & # if a==s and
 length(r) > length(a)) # the slice is shorter than r
 r<<-a # store the new slice
 0 # combn requires a return value
 }
 )
 r # return the shortest slice
}
added 699 characters in body
Source Link
digEmAll
  • 5.1k
  • 15
  • 22
Loading
Source Link
digEmAll
  • 5.1k
  • 15
  • 22
Loading

AltStyle によって変換されたページ (->オリジナル) /