17
\$\begingroup\$

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]

Answer:

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'
\$\endgroup\$
  • \$\begingroup\$ May we take S as a list of characters? \$\endgroup\$ – Mr. Xcoder Aug 11 '17 at 16:04
  • \$\begingroup\$ @Mr.Xcoder Sure, I'll add that in the spec. \$\endgroup\$ – miles Aug 11 '17 at 16:06
  • \$\begingroup\$ May we print as a list of characters? \$\endgroup\$ – Erik the Outgolfer Aug 11 '17 at 16:19
  • \$\begingroup\$ Can we skip the first item (the original string) in the output? \$\endgroup\$ – ETHproductions Aug 11 '17 at 16:28
  • \$\begingroup\$ @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. \$\endgroup\$ – miles Aug 11 '17 at 16:31

36 Answers 36

1
\$\begingroup\$

C++ (gcc), 70 65 bytes

As generic unnamed lambda, requiring s to be like std::string but x can be any iterable container of int, even an array. Returns via reference parameter into the input string.

[](auto&s,auto&x){auto t=s;for(int i:x)t+="\n"+s.erase(i,1);s=t;}

no trailing newline: -5 byte

previous 70 byte solution

[](auto&s,auto&x){auto t=s+'\n';for(int i:x)t+=s.erase(i,1)+'\n';s=t;}

Try it online!

The result is a single newline-seperated string.

\$\endgroup\$
0
\$\begingroup\$

R (+pryr), 45 bytes

Different (and longer) solution than @Guiseppe's.

f=pryr::f(`if`(sum(x|1),f(s[-x[1]],x[-1]),s))

which evaluates to the function:

function (s, x) 
if (sum(x | 1)) f(s[-x[1]], x[-1]) else s

Takes input as a list of characters and a list of 1-indexed indices. As long as x is not empty, recursively calls f with the first element from x removed, as well as its corresponding value in s.

\$\endgroup\$
0
\$\begingroup\$

Tampio, 173 bytes

l miinus tyhjyys on l
l miinus a on l lisättynä l:ään avattuna a:lla
l avattuna a:lla lisättynä b:hen on l katkaistuna a:sta plus l jatkettuna a:n seuraajasta miinus b

Explanation:

l miinus tyhjyys on l
l -      []      =  l

l miinus a on l lisättynä l:ään avattuna a:lla
l -      a =  l :        (l     `avattu` a)

l avattuna  a:lla lisättynä b:hen on
l `avattu` (a     :         b)    =

  l katkaistuna a:sta plus l jatkettuna  a:n seuraajasta miinus b
((l `katkaistu` a)    ++  (l `jatkettu` (a   +1)     ) ) -      b

katkaistu and jatkettu are equivalent to take and drop of Haskell, respectively.

\$\endgroup\$
0
\$\begingroup\$

Röda, 29 bytes

f l{[l[:]];[l[:]]if del l[_]}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

J, 30 bytes (unfinished)

(}.@[$:<^:3@{.@[{])`]@.(''-:[)

edit

looks like i read too fast, and output only the final string rather than all stages of it. will try to fix later, but going to leave up anyway for feedback.

explanation

  • ]@.(''-:[) If the left arg is empty -- (''-:[) -- then -- @. -- return the right arg -- ]. Otherwise -- ` --...

Do the following:

      $:                 recurse using these transforms:
         <^:3@{.@[{])    delete specified index of right arg
(}.@[                    behead the left arg (remove first elm)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

J, 23 bytes

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

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.