# Disappearing Elements

Given a string S and a list of indices X, modify S by removing the element at each index of S while using that result as the new value of S.

For example, given S = 'codegolf' and X = [1, 4, 4, 0, 2],

0 1 2 3 4 5 6 7  |
c o d e g o l f  |  Remove 1
c d e g o l f    |  Remove 4
c d e g l f      |  Remove 4
c d e g f        |  Remove 0
d e g f          |  Remove 2
d e f


Your task is to perform this process, collect the values of S after each operation, and display each on a newline in order. The final answer would be

S = 'codegolf'
X = [1, 4, 4, 0, 2]

codegolf
cdegolf
cdeglf
cdegf
degf
def

• This is so make your code as short as possible.
• You may assume that the values in X are always valid indices for S, and you may use either 0-based or 1-based indexing.
• The string will only contain [A-Za-z0-9]
• Either S or x may by empty. If S is empty, it follows that x must also be empty.
• You may also take S as a list of characters instead of a string.
• You may either print the output or return a list of strings. Leading and trailing whitespace is acceptable. Any form of output is fine as long as it is easily readable.

## Test Cases

S = 'abc', x = [0]
'abc'
'bc'

S = 'abc', x = []
'abc'

S = 'abc', x = [2, 0, 0]
'abc'
'ab'
'b'
''

S = '', x = []
''

S = 'codegolfing', x = [10, 9, 8, 3, 2, 1, 0]
'codegolfing'
'codegolfin'
'codegolfi'
'codegolf'
'codgolf'
'cogolf'
'cgolf'
'golf'

• May we take S as a list of characters? Commented Aug 11, 2017 at 16:04
• @Mr.Xcoder Sure, I'll add that in the spec. Commented Aug 11, 2017 at 16:06
• May we print as a list of characters? Commented Aug 11, 2017 at 16:19
• Can we skip the first item (the original string) in the output? Commented Aug 11, 2017 at 16:28
• @ETHproductions No, the output should be the original string first, and then each string that results from deleting a character. So the output should contain len(x)+1 strings. Commented Aug 11, 2017 at 16:31

s#i=take i s++drop(i+1)s
scanl(#)


Straight forward: repeatedly take the elements before and after index i, rejoin them and collect the results.

Try it online!

Edit: @Lynn saved 5 bytes. Thanks!

• s#i=take i s++drop(i+1)s is actually shorter, saving 5 bytes.
– lynn
Commented Aug 11, 2017 at 23:59
• Nope, you’re at 33 now — don’t forget that the TIO snippet has q= in there ^^;
– lynn
Commented Aug 12, 2017 at 10:30
• Yep, you're right.
– nimi
Commented Aug 12, 2017 at 22:29

# JavaScript (ES6), 57504845 42 bytes

Takes the string as an array of individual characters, outputs an array containing a comma separated string of the original followed by a subarray of comma separated strings for each step.

s=>a=>[s+"",a.map(x=>s.splice(x,1)&&s+"")]

• 3 bytes saved thanks to Arnauld suggesting I abuse the loose output spec more than I already was, which led me to abusing it even more for another 3 byte saving.

## Test it

o.innerText=JSON.stringify((f=

s=>a=>[s+"",a.map(x=>s.splice(x,1)&&s+"")]

)([...i.value="codegolf"])(j.value=[1,4,4,0,2]));oninput=_=>o.innerText=JSON.stringify(f([...i.value])(j.value.split,))
label,input{font-family:sans-serif;font-size:14px;height:20px;line-height:20px;vertical-align:middle}input{margin:0 5px 0 0;width:100px;}
<label for=i>String: </label><input id=i><label for=j>Indices: </label><input id=j><pre id=o>

## Explanation

We take the two inputs via parameters s (the string array) and a (the integer array) in currying syntax, meaning we call the function with f(s)(a).

We build a new array and start it off with the original s. However, as the splice method we'll be using later on modifies an array, we need to make a copy of it, which we can do by converting it to a string (simply append an empty string).

To generate the subarray, we map over the integer array a (where x is the current integer) and, for each element, we splice 1 element from s, beginning at index x. We return the modified s, again making a copy of it by converting it to a string.

• Because any form of output is fine as long as it is easily readable, I suppose that one should also be acceptable: s=>a=>[s+'',...a.map(x=>s.splice(x,1)&&s+'')] Commented Aug 11, 2017 at 16:53
• Nice one, @Arnuald - I wouldn't have thought to push it that far, even given that spec. Commented Aug 11, 2017 at 16:55

# Japt, 6 bytes

åjV uV


Test it online!

### Explanation

UåjV uV   Implicit: U = array of integers, V = string
Uå        Cumulatively reduce U by
j         removing the item at that index in the previous value,
V        with an initial value of V.
uV   Push V to the beginning of the result.


Alternatively:

uQ åjV

UuQ       Push a quotation mark to the beginning of U.
å     Cumulatively reduce by
j      removing the item at that index in the previous value,
V     with an initial value of V.


This works because removing the item at index " does nothing and so returns the original string.

## Husk, 7 bytes

G§+oh↑↓


Takes first the string, then (1-based) indices. Try it online!

## Explanation

G§+oh↑↓
G        Scan from left by function:
Arguments are string, say s = "abcde", and index, say i = 3.
↓    Drop i elements: "de"
↑     Take i elements
oh      and drop the last one: "ab"
§+        Concatenate: "abde"
Implicitly print list of strings on separate lines.

• How do I use an empty list of indices x? Commented Aug 11, 2017 at 17:07
• @miles You have to specify the type, like this. Commented Aug 11, 2017 at 17:09
• I see, thanks. I'm not very familiar with either Haskell or Husk. Commented Aug 11, 2017 at 17:10

# Python 2, 43 bytes

s,i=input()
for i in i+[0]:print s;s.pop(i)


Try it online!

Any form of output is fine as long as it is easily readable.

So this prints as lists of chars.

• Nice, but why did you choose the notation abuse for i in i+[0]? Commented Aug 11, 2017 at 16:12
• @Mr.Xcoder Because that way the last line is shown. (and the reason I posted separately in first place) Commented Aug 11, 2017 at 16:13
• No, not +[0], I am talking about for i in i. for k in i is equivalent. Commented Aug 11, 2017 at 16:14
• @Mr.Xcoder Because I like it that way... Commented Aug 11, 2017 at 16:14
• Ok, just curious... Maybe it was a trick I didn't know of :) Commented Aug 11, 2017 at 16:15

# Python 2, 47 bytes

This could be shortened to 43 bytes, as @LuisMendo pointed out, but that's already @ErktheOutgolfer's solution.

a,b=input();print a
for i in b:a.pop(i);print a


Try it online!

• a[2::5] instead ''.join(a)
– Rod
Commented Aug 11, 2017 at 16:18
• @Rod How does than work? Commented Aug 11, 2017 at 16:19
• repr and string spliting, works good to turn a list of characters into a string, a[1::3] can also be used with a list of digits
– Rod
Commented Aug 11, 2017 at 16:21
• @Rod I know what they are, I don't understand how ::5 works here :P Commented Aug 11, 2017 at 16:22
• @Mr.Xcoder well then study string slicing ;) basically it takes every 5th char starting from the 2nd Commented Aug 11, 2017 at 16:22

# Java 8, 78 bytes

This is a curried lambda, from int[] to a consumer of StringBuilder or StringBuffer. Output is printed to standard out.

l->s->{System.out.println(s);for(int i:l)System.out.println(s.delete(i,i+1));}


Try It Online

• Your second answer is a valid entry. Nothing forbids you to carefully select your input type as long as it makes sense. I've several times already taken even Streams as input and got very nice answers. Actually, nearly all golfing languages use streams equivalent internally. So by selecting your input, you just level a tad. +1 nonetheless Commented Aug 12, 2017 at 14:15
• Maybe you're right. I think I tend to be more conservative than most about these things. I'll switch to the second solution. Commented Aug 12, 2017 at 16:31

# 05AB1E, 11 bytes

v=ā0m0yǝÏ},


Try it online!

v           # For each index:
=          #   Print without popping
ā         #   Push range(1, len(a) + 1)
0m       #   Raise each to the power of 0.
#   This gives a list of equal length containing all 1s
0yǝ    #   Put a 0 at the location that we want to remove
Ï   #   Keep only the characters that correspond to a 1 in the new list
}, # Print the last step

• The first line isn't there. Oh, and not sure if you can print like that. Commented Aug 11, 2017 at 16:23
• @EriktheOutgolfer Any form of output is fine as long as it is easily readable Commented Aug 11, 2017 at 16:28
• I can save some bytes then... Commented Aug 11, 2017 at 16:31

# Mathematica, 70 bytes

(s=#;For[t=1,t<=Length@#2,Print@s;s=StringDrop[s,{#2[[t]]+1}];t++];s)&


Try it online!

# R, 46 32 bytes

function(S,X)Reduce([,-X,S,,T)


Try it online!

Takes input as a list of characters and X is 1-based. Reduce is the R equivalent of fold, the function in this case is [ which is subset. Iterates over -X because negative indexing in R removes the element, and init is set to S, with accum=TRUE so we accumulate the intermediate results.

# R, 80 bytes

function(S,X,g=substring)Reduce(function(s,i)paste0(g(s,0,i-1),g(s,i+1)),X,S,,T)


2-argument function, takes X 1-indexed. Takes S as a string.

Try it online!

• Very smart to use Reduce here. Well done! Commented Aug 12, 2017 at 18:25

scanl(\s i->take i s++drop(i+1)s)


Try it online!

# q/kdb+, 27 10 bytes

Solution:

{x _\0N,y}


Examples:

q){x _\0N,y}["codegolf";1 4 4 0 2]
"codegolf"
"cdegolf"
"cdeglf"
"cdegf"
"degf"
"def"
q){x _\0N,y}["abc";0]
"abc"
"bc"
q){x _\0N,y}["abc";()]
"abc"
q){x _\0N,y}["abc";2 0 0]
"abc"
"ab"
,"b"
""
q){x _\0N,y}["";()]
""


Explanation:

Takes advantage of the converge functionality \ as well as drop _.

{x _\0N,y}
{        } / lambda function, x and y are implicit variables
0N,y  / join null to the front of list (y), drop null does nothing
_\      / drop over (until result converges) printing each stage
x         / the string (need the space as x_ could be a variable name)


Notes:

If we didn't need to print the original result, this would be 2 bytes in q:

q)_\["codegolfing";10 9 8 3 2 1 0]
"codegolfin"
"codegolfi"
"codegolf"
"codgolf"
"cogolf"
"cgolf"
"golf"


# PowerShell, 94 84 bytes

param($s,$x)$a=[Collections.Generic.list[char]]$s;$x|%{-join$a;,$a|% r*t$_};-join$a  Try it online! Takes input $s as a string and $x as an explicit array. We then create $a based on $s as a list. Arrays in PowerShell are fixed size (for our purposes here), so we need to use the lengthy [System.Collections.Generic.list] type in order to get access to the .removeAt() function, which does exactly what it says on the tin. I sacrificed 10 bytes to include two -join statements to make the output pretty. OP has stated that outputting a list of chars is fine, so I could output just $a rather than -join$a, but that's really ugly in my opinion. Saved 10 bytes thanks to briantist. • You can leave off System and just use [Collections.Generic.list[char]]. To keep it pretty without sacrificing bytes, you can put the last -join$a in the footer in TIO. Commented Aug 11, 2017 at 22:25
• I think you can also save 3 bytes by changing $a.removeat($_) to ,$a|% r*t$_. Commented Aug 11, 2017 at 22:52
• @briantist Thanks for those - I always forget to remove System from the class name. Sadly the last -join$a is necessary for the code, so I can't move it to the footer. Commented Aug 14, 2017 at 14:10 # Python 2, 50 bytes • Takes in string and list of indices s,i=input() for j in i+[0]:print s;s=s[:j]+s[j+1:]  Try it online! # C (gcc), 99 bytes j;f(s,a,i)char*s;int*a;{puts(s);for(j=0;j<i;j++){memmove(s+a[j],s+a[j]+1,strlen(s)-a[j]);puts(s);}}  Try it online! Takes the string, array, and the length of the array. • 91 bytes Commented Dec 7, 2018 at 21:46 # 05AB1E, 9 7 bytes =svõyǝ=  Try it online! =s # Print original string, swap with indices. v # Loop through indices... õyǝ # Replace current index with empty string.  -2 thanks to idea from @ETHProductions. • This doesn't print anything if x is empty. Commented Aug 11, 2017 at 16:30 • @Riley fixed. (#ETHProductions fixed.) Commented Aug 11, 2017 at 16:32 • Couldn't you just do =sv""yǝ= or something similar instead of replacing with a newline and then removing the newline? Commented Aug 11, 2017 at 17:19 • @ETHproductions õ also works :) Commented Aug 11, 2017 at 17:35 • Well I don't know 05AB1E, haha Commented Aug 11, 2017 at 17:38 # Retina, 58 bytes ¶\d+ ¶¶1$&$* +1(?=.*¶¶.(.)*)(((?<-1>.)*).(.*)¶)¶.*$2$3$4


Try it online! Explanation:

¶\d+


Match the indices (which are never on the first line, so are always preceded by a newline).

¶¶1$&$*


Double-space the indices, convert to unary, and add 1 (because zeros are hard in Retina).

+1


Repeatedly change the first match, which is always the current value of the string.

   (?=.*¶¶.(.)*)


Retrieve the next index in $#1.  ( . ¶)  Capture the string, including the $#1th character and one newline.

                 ((?<-1>.)*) (.*)


Separately capture the prefix and suffix of the $#1th character of the string.  ¶.*  Match the index. $2$3$4


Replace the string with itself and the index with the prefix and suffix of the $#1th character. # Pyth, 8 bytes .u.DNYEz  Demonstration Reduce, starting with the string and iterating over the list of indices, on the deletion function. # PowerShell, 54 58 bytes param($s,$x),-1+$x|%{$z=$_;$i=0;-join($s=$s|?{$z-ne$i++})}  Try it online! ## Explanation Takes input as a char array ([char[]]). Iterates through the array of indices ($x) plus an injected first element of -1, then for each one, assigns the current element to $z, initializes $i to 0, then iterates through the array of characters ($s), returning a new array of only the characters whose index ($i) does not equal (-ne) the current index to exclude ($z). This new array is assigned back to $s, while simultaneously being returned (this happens when the assignment is done in parentheses). That returned result is -joined to form a string which is sent out to the pipeline.

Injecting -1 at the beginning ensures that the original string will be printed, since it's the first element and an index will never match -1.

• Very clever way of pulling out the appropriate indices. Commented Aug 15, 2017 at 13:14

# Perl 5, 55 bytes (54 + "-l")

sub{print($s=shift);for(@_){substr$s,$_,1,"";print$s}}


Try it online!

• Nice! I came up with a very similar approach, but as a full program (using -pa) for 44 bytes: $_=<>;substr$_,shift@F,print,""while@F&&$_ Commented Aug 14, 2017 at 8:35 • Nice! Not sure you need the final &&$_ since you can assume the input is valid (the list of indices can't be longer than the string). Using the return value of print as the number of characters is quite slick. Commented Aug 14, 2017 at 12:14
• Ah, that's true! I didn't notice that part of the spec! I thought my answer was far too similar to yours to post separately though! Commented Aug 14, 2017 at 18:23

# MATL, 8 bytes

ii"t[]@(


Indexing is 1-based.

### Explanation

i      % Input string. Input has to be done explicitly so that the string
% will be displayed even if the row vector of indices is empty
i      % Input row vector of indices
"      % For each
t    %   Duplicate current string
[]   %   Push empty array
@    %   Push current index
(    %   Assignment indexing: write [] to string at specified index
% End (implicit). Display stack (implicit)


# C# (.NET Core), 878774 70 bytes

S=>I=>{for(int i=0;;S=S.Remove(I[i++],1))System.Console.WriteLine(S);}


Try it online!

Just goes to show that recursion isn't always the best solution. This is actually shorter than my original invalid answer. Still prints to STDOUT rather than returning, which is necessary because it ends with an error.

-4 bytes thanks to TheLethalCoder

• Per a recent meta consensus (that I can't find) recursive lambdas in C# are disallowed unless you specify what they compile to in the byte count. Therefore, a full method is shorter in this case. I am downvoting until this is fixed, let me know when. Commented Aug 14, 2017 at 8:07
• @TheLethalCoder I may not agree with the consensus, but it does seem to be consensus. I've updated my answer. Commented Aug 14, 2017 at 13:18
• 70 bytes. Use currying and move one statement into the loop to stop needing the loop braces. Commented Aug 15, 2017 at 15:05
• @TheLethalCoder Ah, so THAT's how you use currying in C#! I knew it was shorter for exactly two arguments, but it always ended up complaining about some part of my syntax. Thanks for the improvements Commented Aug 15, 2017 at 15:30
• No worries and yeah the first one must always be a Func that returns the other Func, Action, Predicate,... Commented Aug 15, 2017 at 15:38

# J, 23 bytes

>@({&.>/\.&.|.@;<^:4"0)


Try it online!

• Brilliant use of negative boxing. Commented Jun 29, 2022 at 16:06

# Vyxalo, 4 bytes

(…n⟇


Try it Online!

Prints lists of chars

(…n⟇ # 'o' flag forces output of the last value
(    # For every number in the list...
…   # Print the string without popping
n  # Get the current value
⟇ # Remove the char at the index
# The result is printed on next iteration


# Pyth, 10 bytes

Rule changes saved me 1 byte:

V+E0Q .(QN


Try it online!

# Pyth, 11 bytes

V+E0sQ .(QN


Try it online!

• @miles isn't stderr output allowed by default? Commented Aug 11, 2017 at 16:29
• @Mr.Xcoder I thought stderr output was ignored in that case... Commented Aug 11, 2017 at 16:31
• @miles I will remove mine and flag it to a mod if we do not remove them. Thanks! Commented Aug 11, 2017 at 16:36

# Gaia, 9 bytes

+⟪Seḥ+⟫⊣ṣ


I should really add a "delete at index" function...

Try it online!

### Explanation

+          Add the string to the list
⟪Seḥ+⟫⊣   Cumulatively reduce by this block:
S         Split around index n
e        Dump the list
ḥ       Remove the first char of the second part
+      Concatenate back together
ṣ  Join the result with newlines


# V, 12 bytes

òdt,GÙ@-|xHx


Try it online!

This is 1-indexed, input is like:

11,10,9,4,3,2,1,
codegolfing


### Explanation

ò              ' <M-r>ecursively until error
dt,           ' (d)elete (t)o the next , (errors when no more commas)
G          ' (G)oto the last line
Ù         ' Duplicate it down
|      ' Goto column ...
@-       ' (deleted number from the short register)
x     ' And delete the character there
H    ' Go back home
x   ' And delete the comma that I missed

• How do I use an empty list of indices x? Commented Aug 11, 2017 at 17:08
• @miles By adding a few bytes :). Simply an empty first line will now work. Would you be OK if I took lists with a trailing comma? IE 1,2,3,. Empty list would be nothing, Singleton would be 1, Commented Aug 11, 2017 at 17:19
• Sure, you can use that input format. Commented Aug 11, 2017 at 17:22

# Swift 3, 80 bytes

func f(l:[String],c:[Int]){var t=l;for i in c{print(t);t.remove(at:i)};print(t)}


Try it online!

# Pyth, 8 bytes

+zm=z.Dz


Test suite!

# explanation

+zm=z.DzdQ    # implicit: input and iteration variable
m      Q    # for each of the elements of the first input (the array of numbers, Q)
.Dzd     # remove that index from the second input (the string, z)
=z         # Store that new value in z
+z            # prepend the starting value


# Python 2, 54

X,S=input()
for a in X:
print S
S=S[:a]+S[a+1:]
print S


Try It Online

• I only see the last string in the output. Commented Aug 11, 2017 at 16:40
• I messed up... One moment... Commented Aug 11, 2017 at 16:40