Reorder a Master List Based on a Reordered SubsetEpcot World ReorderN-chotomize a listRandom subset...

Why do I get two different answers for this counting problem?

Does detail obscure or enhance action?

I'm flying to France today and my passport expires in less than 2 months

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

RSA: Danger of using p to create q

Alternative to sending password over mail?

Arrow those variables!

Watching something be written to a file live with tail

How do I draw and define two right triangles next to each other?

What is the word for reserving something for yourself before others do?

dbcc cleantable batch size explanation

LWC SFDX source push error TypeError: LWC1009: decl.moveTo is not a function

Client team has low performances and low technical skills: we always fix their work and now they stop collaborate with us. How to solve?

Languages that we cannot (dis)prove to be Context-Free

Paid for article while in US on F-1 visa?

How to draw a waving flag in TikZ

Why doesn't H₄O²⁺ exist?

Did Shadowfax go to Valinor?

Definite integral giving negative value as a result?

Character reincarnated...as a snail

Can you really stack all of this on an Opportunity Attack?

How to format long polynomial?

Why can't I see bouncing of a switch on an oscilloscope?

Was any UN Security Council vote triple-vetoed?



Reorder a Master List Based on a Reordered Subset


Epcot World ReorderN-chotomize a listRandom subset generatorFind Subset FactorsSort a difference listThe Three 'R's: Reverse, Reorder, RepeatDeep Search a ListN-bit Variation on Subset-SumFind unique elements based on a given keySubset Sum Orderings













19












$begingroup$


I recently had a problem to solve at work where I had two lists: a master list, and a smaller list that contains a subset of the items in the master list potentially in a different order. I needed to reorder the master list in such a way that the items in the subset would appear in the same order without changing the order of the items not found in the list and keeping items in the same location whenever possible. Okay, that probably sounds confusing, so I'll break it down:




  • The master list defines the default order of items.

  • The subset list defines relative order of certain items.

  • Where the master list has two elements out of order according to the subset list, the item that is earlier in the master list should be moved to the earliest index where it is in the correct location relative to other items within the subset list. (i.e. immediately after the later item)


Your task is to implement this reordering algorithm.



Example Test Cases



Master: [1, 2, 3]
Subset: []
Result: [1, 2, 3]




Master: [9001, 42, 69, 1337, 420]
Subset: [69]
Result: [9001, 42, 69, 1337, 420]




Master: [9001, 42, 69, 1337, 420, 99, 255]
Subset: [69, 9001, 1337]
Result: [42, 69, 9001, 1337, 420, 99, 255]




Master: [1, 2, 3, 4, 5]
Subset: [2, 5]
Result: [1, 2, 3, 4, 5]




Master: [apple, banana, carrot, duck, elephant]
Subset: [duck, apple]
Result: [banana, carrot, duck, apple, elephant]




Master: [Alice, Betty, Carol, Debbie, Elaine, Felicia, Georgia, Helen, Ilene, Julia]
Subset: [Betty, Felicia, Carol, Julia]
Result: [Alice, Betty, Debbie, Elaine, Felicia, Carol, Georgia, Helen, Ilene, Julia]




Master: [snake, lizard, frog, werewolf, vulture, dog, human]
Subset: [snake, werewolf, lizard, human, dog]
Result: [snake, frog, werewolf, lizard, vulture, human, dog]




Master: [Pete, Rob, Jeff, Stan, Chris, Doug, Reggie, Paul, Alex]
Subset: [Jeff, Stan, Pete, Paul]
Result: [Rob, Jeff, Stan, Pete, Chris, Doug, Reggie, Paul, Alex]




Master: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Subset: [8, 1, 2, 12, 11, 10]
Result: [3, 4, 5, 6, 7, 8, 1, 2, 9, 12, 11, 10]




Master: [lol, rofl, lmao, roflmao, lqtm, smh, jk, wat]
Subset: [wat, lmao, rofl]
Result: [lol, roflmao, lqtm, smh, jk, wat, lmao, rofl]


Rules




  • Standard loopholes, yadda yadda, convenient I/O, blah blah.

  • Even though the examples use numbers and strings, you only need to support one element type, whether that's integers, strings, or anything else with well-defined equality semantics, including heterogeneous lists if that's convenient in your language.

  • You may assume both the master list and the subset list contain no duplicates

  • You may assume that all items found in the subset list are found in the master list

  • Either list may be empty

  • You must, at minimum, support arrays up to 100 elements long.

  • Reordering may be implemented in-place or through the creation of a new list/array.


Happy Golfing!










share|improve this question









$endgroup$












  • $begingroup$
    A nice, beefy problem.
    $endgroup$
    – Jonah
    yesterday










  • $begingroup$
    Is 8 1 3 4 5 6 7 2 9 12 11 10 a valid solution to the second-to-last one?
    $endgroup$
    – Ven
    15 hours ago










  • $begingroup$
    @Ven No. Even though that fits within the constraints of keeping the subset items in the same relative order, I wanted to make sure there was only one correct answer, so the earlier out-of-order item should be moved to be after the later out-of-order item.
    $endgroup$
    – Beefster
    13 hours ago
















19












$begingroup$


I recently had a problem to solve at work where I had two lists: a master list, and a smaller list that contains a subset of the items in the master list potentially in a different order. I needed to reorder the master list in such a way that the items in the subset would appear in the same order without changing the order of the items not found in the list and keeping items in the same location whenever possible. Okay, that probably sounds confusing, so I'll break it down:




  • The master list defines the default order of items.

  • The subset list defines relative order of certain items.

  • Where the master list has two elements out of order according to the subset list, the item that is earlier in the master list should be moved to the earliest index where it is in the correct location relative to other items within the subset list. (i.e. immediately after the later item)


Your task is to implement this reordering algorithm.



Example Test Cases



Master: [1, 2, 3]
Subset: []
Result: [1, 2, 3]




Master: [9001, 42, 69, 1337, 420]
Subset: [69]
Result: [9001, 42, 69, 1337, 420]




Master: [9001, 42, 69, 1337, 420, 99, 255]
Subset: [69, 9001, 1337]
Result: [42, 69, 9001, 1337, 420, 99, 255]




Master: [1, 2, 3, 4, 5]
Subset: [2, 5]
Result: [1, 2, 3, 4, 5]




Master: [apple, banana, carrot, duck, elephant]
Subset: [duck, apple]
Result: [banana, carrot, duck, apple, elephant]




Master: [Alice, Betty, Carol, Debbie, Elaine, Felicia, Georgia, Helen, Ilene, Julia]
Subset: [Betty, Felicia, Carol, Julia]
Result: [Alice, Betty, Debbie, Elaine, Felicia, Carol, Georgia, Helen, Ilene, Julia]




Master: [snake, lizard, frog, werewolf, vulture, dog, human]
Subset: [snake, werewolf, lizard, human, dog]
Result: [snake, frog, werewolf, lizard, vulture, human, dog]




Master: [Pete, Rob, Jeff, Stan, Chris, Doug, Reggie, Paul, Alex]
Subset: [Jeff, Stan, Pete, Paul]
Result: [Rob, Jeff, Stan, Pete, Chris, Doug, Reggie, Paul, Alex]




Master: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Subset: [8, 1, 2, 12, 11, 10]
Result: [3, 4, 5, 6, 7, 8, 1, 2, 9, 12, 11, 10]




Master: [lol, rofl, lmao, roflmao, lqtm, smh, jk, wat]
Subset: [wat, lmao, rofl]
Result: [lol, roflmao, lqtm, smh, jk, wat, lmao, rofl]


Rules




  • Standard loopholes, yadda yadda, convenient I/O, blah blah.

  • Even though the examples use numbers and strings, you only need to support one element type, whether that's integers, strings, or anything else with well-defined equality semantics, including heterogeneous lists if that's convenient in your language.

  • You may assume both the master list and the subset list contain no duplicates

  • You may assume that all items found in the subset list are found in the master list

  • Either list may be empty

  • You must, at minimum, support arrays up to 100 elements long.

  • Reordering may be implemented in-place or through the creation of a new list/array.


Happy Golfing!










share|improve this question









$endgroup$












  • $begingroup$
    A nice, beefy problem.
    $endgroup$
    – Jonah
    yesterday










  • $begingroup$
    Is 8 1 3 4 5 6 7 2 9 12 11 10 a valid solution to the second-to-last one?
    $endgroup$
    – Ven
    15 hours ago










  • $begingroup$
    @Ven No. Even though that fits within the constraints of keeping the subset items in the same relative order, I wanted to make sure there was only one correct answer, so the earlier out-of-order item should be moved to be after the later out-of-order item.
    $endgroup$
    – Beefster
    13 hours ago














19












19








19


1



$begingroup$


I recently had a problem to solve at work where I had two lists: a master list, and a smaller list that contains a subset of the items in the master list potentially in a different order. I needed to reorder the master list in such a way that the items in the subset would appear in the same order without changing the order of the items not found in the list and keeping items in the same location whenever possible. Okay, that probably sounds confusing, so I'll break it down:




  • The master list defines the default order of items.

  • The subset list defines relative order of certain items.

  • Where the master list has two elements out of order according to the subset list, the item that is earlier in the master list should be moved to the earliest index where it is in the correct location relative to other items within the subset list. (i.e. immediately after the later item)


Your task is to implement this reordering algorithm.



Example Test Cases



Master: [1, 2, 3]
Subset: []
Result: [1, 2, 3]




Master: [9001, 42, 69, 1337, 420]
Subset: [69]
Result: [9001, 42, 69, 1337, 420]




Master: [9001, 42, 69, 1337, 420, 99, 255]
Subset: [69, 9001, 1337]
Result: [42, 69, 9001, 1337, 420, 99, 255]




Master: [1, 2, 3, 4, 5]
Subset: [2, 5]
Result: [1, 2, 3, 4, 5]




Master: [apple, banana, carrot, duck, elephant]
Subset: [duck, apple]
Result: [banana, carrot, duck, apple, elephant]




Master: [Alice, Betty, Carol, Debbie, Elaine, Felicia, Georgia, Helen, Ilene, Julia]
Subset: [Betty, Felicia, Carol, Julia]
Result: [Alice, Betty, Debbie, Elaine, Felicia, Carol, Georgia, Helen, Ilene, Julia]




Master: [snake, lizard, frog, werewolf, vulture, dog, human]
Subset: [snake, werewolf, lizard, human, dog]
Result: [snake, frog, werewolf, lizard, vulture, human, dog]




Master: [Pete, Rob, Jeff, Stan, Chris, Doug, Reggie, Paul, Alex]
Subset: [Jeff, Stan, Pete, Paul]
Result: [Rob, Jeff, Stan, Pete, Chris, Doug, Reggie, Paul, Alex]




Master: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Subset: [8, 1, 2, 12, 11, 10]
Result: [3, 4, 5, 6, 7, 8, 1, 2, 9, 12, 11, 10]




Master: [lol, rofl, lmao, roflmao, lqtm, smh, jk, wat]
Subset: [wat, lmao, rofl]
Result: [lol, roflmao, lqtm, smh, jk, wat, lmao, rofl]


Rules




  • Standard loopholes, yadda yadda, convenient I/O, blah blah.

  • Even though the examples use numbers and strings, you only need to support one element type, whether that's integers, strings, or anything else with well-defined equality semantics, including heterogeneous lists if that's convenient in your language.

  • You may assume both the master list and the subset list contain no duplicates

  • You may assume that all items found in the subset list are found in the master list

  • Either list may be empty

  • You must, at minimum, support arrays up to 100 elements long.

  • Reordering may be implemented in-place or through the creation of a new list/array.


Happy Golfing!










share|improve this question









$endgroup$




I recently had a problem to solve at work where I had two lists: a master list, and a smaller list that contains a subset of the items in the master list potentially in a different order. I needed to reorder the master list in such a way that the items in the subset would appear in the same order without changing the order of the items not found in the list and keeping items in the same location whenever possible. Okay, that probably sounds confusing, so I'll break it down:




  • The master list defines the default order of items.

  • The subset list defines relative order of certain items.

  • Where the master list has two elements out of order according to the subset list, the item that is earlier in the master list should be moved to the earliest index where it is in the correct location relative to other items within the subset list. (i.e. immediately after the later item)


Your task is to implement this reordering algorithm.



Example Test Cases



Master: [1, 2, 3]
Subset: []
Result: [1, 2, 3]




Master: [9001, 42, 69, 1337, 420]
Subset: [69]
Result: [9001, 42, 69, 1337, 420]




Master: [9001, 42, 69, 1337, 420, 99, 255]
Subset: [69, 9001, 1337]
Result: [42, 69, 9001, 1337, 420, 99, 255]




Master: [1, 2, 3, 4, 5]
Subset: [2, 5]
Result: [1, 2, 3, 4, 5]




Master: [apple, banana, carrot, duck, elephant]
Subset: [duck, apple]
Result: [banana, carrot, duck, apple, elephant]




Master: [Alice, Betty, Carol, Debbie, Elaine, Felicia, Georgia, Helen, Ilene, Julia]
Subset: [Betty, Felicia, Carol, Julia]
Result: [Alice, Betty, Debbie, Elaine, Felicia, Carol, Georgia, Helen, Ilene, Julia]




Master: [snake, lizard, frog, werewolf, vulture, dog, human]
Subset: [snake, werewolf, lizard, human, dog]
Result: [snake, frog, werewolf, lizard, vulture, human, dog]




Master: [Pete, Rob, Jeff, Stan, Chris, Doug, Reggie, Paul, Alex]
Subset: [Jeff, Stan, Pete, Paul]
Result: [Rob, Jeff, Stan, Pete, Chris, Doug, Reggie, Paul, Alex]




Master: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Subset: [8, 1, 2, 12, 11, 10]
Result: [3, 4, 5, 6, 7, 8, 1, 2, 9, 12, 11, 10]




Master: [lol, rofl, lmao, roflmao, lqtm, smh, jk, wat]
Subset: [wat, lmao, rofl]
Result: [lol, roflmao, lqtm, smh, jk, wat, lmao, rofl]


Rules




  • Standard loopholes, yadda yadda, convenient I/O, blah blah.

  • Even though the examples use numbers and strings, you only need to support one element type, whether that's integers, strings, or anything else with well-defined equality semantics, including heterogeneous lists if that's convenient in your language.

  • You may assume both the master list and the subset list contain no duplicates

  • You may assume that all items found in the subset list are found in the master list

  • Either list may be empty

  • You must, at minimum, support arrays up to 100 elements long.

  • Reordering may be implemented in-place or through the creation of a new list/array.


Happy Golfing!







code-golf array-manipulation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









BeefsterBeefster

2,4321141




2,4321141












  • $begingroup$
    A nice, beefy problem.
    $endgroup$
    – Jonah
    yesterday










  • $begingroup$
    Is 8 1 3 4 5 6 7 2 9 12 11 10 a valid solution to the second-to-last one?
    $endgroup$
    – Ven
    15 hours ago










  • $begingroup$
    @Ven No. Even though that fits within the constraints of keeping the subset items in the same relative order, I wanted to make sure there was only one correct answer, so the earlier out-of-order item should be moved to be after the later out-of-order item.
    $endgroup$
    – Beefster
    13 hours ago


















  • $begingroup$
    A nice, beefy problem.
    $endgroup$
    – Jonah
    yesterday










  • $begingroup$
    Is 8 1 3 4 5 6 7 2 9 12 11 10 a valid solution to the second-to-last one?
    $endgroup$
    – Ven
    15 hours ago










  • $begingroup$
    @Ven No. Even though that fits within the constraints of keeping the subset items in the same relative order, I wanted to make sure there was only one correct answer, so the earlier out-of-order item should be moved to be after the later out-of-order item.
    $endgroup$
    – Beefster
    13 hours ago
















$begingroup$
A nice, beefy problem.
$endgroup$
– Jonah
yesterday




$begingroup$
A nice, beefy problem.
$endgroup$
– Jonah
yesterday












$begingroup$
Is 8 1 3 4 5 6 7 2 9 12 11 10 a valid solution to the second-to-last one?
$endgroup$
– Ven
15 hours ago




$begingroup$
Is 8 1 3 4 5 6 7 2 9 12 11 10 a valid solution to the second-to-last one?
$endgroup$
– Ven
15 hours ago












$begingroup$
@Ven No. Even though that fits within the constraints of keeping the subset items in the same relative order, I wanted to make sure there was only one correct answer, so the earlier out-of-order item should be moved to be after the later out-of-order item.
$endgroup$
– Beefster
13 hours ago




$begingroup$
@Ven No. Even though that fits within the constraints of keeping the subset items in the same relative order, I wanted to make sure there was only one correct answer, so the earlier out-of-order item should be moved to be after the later out-of-order item.
$endgroup$
– Beefster
13 hours ago










9 Answers
9






active

oldest

votes


















4












$begingroup$


Retina 0.8.2, 51 bytes



+`(b(w+),(w+)b.*¶.*b)3,(.*b2b)
$1$4,$3
1A`


Try it online! Takes input as a comma-separated list of subwords on the first line and a comma-separated master list of words on the second line. Explanation:



(b(w+),(w+)b.*¶.*b)3,(.*b2b)


Find two adjacent subwords where the second word precedes the first on the master list.



$1$4,$3


Move the second word to appear after the first word in the master list.



+`


Repeat until no words appear out of order.



1A`


Delete the subwords.






share|improve this answer









$endgroup$





















    4












    $begingroup$

    JavaScript (ES6),  96 89 74  71 bytes



    This started as a bulky mess and was eventually shrunk to a rather concise and elegant form. I'd like to thank the .splice() method for its fruitful collaboration on that one. ;)



    Takes input as (master)(subset). Outputs by updating the master list.





    m=>s=>s.map(p=x=>m.splice(p,0,...m.splice(i=m.indexOf(x),p>i||!(p=i))))


    Try it online!



    How?



    We are using two nested .splice() methods on the same array to conditionally move an element from position $i$ to position $p$:



    m.splice(p, 0, ...m.splice(i, condition))


    If the condition is truthy (coerced to $1$):




    • the inner .splice() removes and returns the element at position $i$ as a singleton array $[element]$

    • thanks to the spread syntax, this element is expanded as a 3rd argument for the outer .splice(), which causes it to be inserted back at position $p$


    If the condition is falsy (coerced to $0$):




    • the inner .splice() removes nothing and returns an empty array

    • as a result, the outer .splice() receives undefined as its 3rd argument and nothing is inserted either


    Commented



    m => s =>                 // m[] = master list, s[] = subset list
    s.map( //
    p = // p = position in the master list of the last element from
    // the subset list (initialized to a non-numeric value)
    x => // for each element x in the subset list:
    m.splice( // insert in the master list:
    p, // at position p
    0, // without removing any element
    ...m.splice( // remove from the master list and flatten:
    i = m.indexOf(x), // i = position of x in the master list
    p > i // if p is greater than i, remove x from its current
    // position and insert it at position p
    || !(p = i) // otherwise, set p to i and don't remove/insert anything
    ) // end of inner splice()
    ) // end of outer splice()
    ) // end of map()





    share|improve this answer











    $endgroup$









    • 1




      $begingroup$
      "I'd like to thank the .splice() method for ..." Cue PPCG Oscar's Music... :)
      $endgroup$
      – Chas Brown
      yesterday





















    1












    $begingroup$

    Haskell, 79 bytes



    (m:n)#u@(s:t)|m==s=m:n#t|all(/=m)u=m:n#u|(x,_:z)<-span(/=s)n=(x++s:m:z)#u
    m#_=m


    Try it online!



    (m:n)#u@(s:t)                 -- m: head of master list
    -- n: tail of master list
    -- s: head of subset
    -- t: tail of subset
    -- u: whole subset
    |m==s -- if m==s
    =m:n#t -- return 'm' and append a recursive call with 'n' and 't'
    |all(/=m)u -- if 'm' is not in 'u'
    =m:n#u -- return 'm' and append a recursive call with 'n' and 'u'
    | -- else (note: 's' is element of 'n')
    (x,_:z)<-span(/=s)n -- split 'n' into a list 'x' before element 's' and
    -- a list 'z' after element 's' and
    = (x++s:m:z)#u -- make a recursive call with
    -- x++s:m:z as the new master list (i.e. 'm' inserted into 'n' after 's')
    -- and 'u'
    m # _ = m -- if either list is emtpy, return the master list





    share|improve this answer











    $endgroup$





















      1












      $begingroup$


      J, 49 bytes



      [:(<@({:+i.@>:@-/)@i.~C.])^:(>/@i.~)&.>/]|.@;2<[


      Try it online!



      Will golf further and add explanation tomorrow






      share|improve this answer











      $endgroup$





















        1












        $begingroup$


        Ruby, 73 68 bytes





        ->a,b{0while b.zip(a&b).find{|m,n|m!=n&&a=a[0..a.index(m)]-[n]|a};a}


        Try it online!



        How?




        • The intersection between a and b contains all elements of b, but in the same order as we would find them in a

        • So, if we iterate on b and on the intersection in parallel, as soon as we find a difference, we can relocate a single element.

        • Relocation is done by cutting a on the position of the element we found in b, then removing the element we found in the intersection, and then adding the remainder of a.

        • Repeat from the beginning, until all elements of b are in the right order in a






        share|improve this answer











        $endgroup$













        • $begingroup$
          what is the 0 doing in 0while?
          $endgroup$
          – Jonah
          14 hours ago










        • $begingroup$
          It's just a NOP.
          $endgroup$
          – G B
          13 hours ago










        • $begingroup$
          why is it needed?
          $endgroup$
          – Jonah
          12 hours ago



















        1












        $begingroup$


        Python 2, 124 109 106 99 96 bytes





        def f(a,b,i=0):
        while b[i+1:]:x,y=map(a.index,b)[i:i+2];i+=1;x>y>a.insert(x,a.pop(y))
        return a


        Try it online!






        share|improve this answer











        $endgroup$





















          1












          $begingroup$


          Perl 6, 40 bytes





          {*.permutations.first(*.grep(.any)eq$_)}


          Try it online!



          Anonymous code block that takes the input curried (like f(subList)(masterList), and finds the first lexographical permutation of the indexes of the master list where the elements from the sub list are in the correct order.



          Intuitively, the first satisfying permutation will leave the correctly ordered elements in the original order, while moving the incorrectly placed ones the minimum needed distance forward in order to have them in the correct order, which places them directly after the previous element in the subset.



          Explanation:



          {*                                     } # Anonymous code block that returns a lambda
          .permutations # In all permutations of the master list
          .first( ) # Find the first permutation
          (*.grep(.any) # Where the order of the subset
          eq$_ # Is the same as the given order







          share|improve this answer











          $endgroup$





















            0












            $begingroup$


            Jelly, 9 bytes



            Œ!iⱮṢƑ¥ƇḢ


            Try it online! or Test suite



            Inefficient, particularly with large master lists. Generates all possible permutations, filters out those where the subset is in the wrong order, and then returns the first.



            Alternatively, a faster approach is:




            Jelly, 24 bytes



            i@€MƤFṬœṗƲḊ;JḟF}W€ʋ@¥ṢFị


            Try it online!






            share|improve this answer











            $endgroup$













            • $begingroup$
              This doesn't seem like it would conform to the rule "Where the master list has two elements out of order according to the subset list, the item that is earlier in the master list should be moved to the earliest index where it is in the correct location relative to other items within the subset list. (i.e. immediately after the later item)"
              $endgroup$
              – Beefster
              9 hours ago










            • $begingroup$
              @Beefster it works on the ones I’ve tried so far. I think the ordering of the permutations is such that this is the correct result. Happy to be proved wrong if there’s a counterexample.
              $endgroup$
              – Nick Kennedy
              9 hours ago










            • $begingroup$
              @Beefster I’ve now tried all of your examples except the female names and the 1..12 and the ordering of the result is correct.
              $endgroup$
              – Nick Kennedy
              8 hours ago










            • $begingroup$
              @Beefster My answer has a partial explanation for why this works
              $endgroup$
              – Jo King
              3 hours ago



















            0












            $begingroup$


            C# (Visual C# Interactive Compiler), 136 bytes





            a=>b=>{for(int i=0,j;i<b.Count;)foreach(var e in a.Take(j=a.IndexOf(b[i++])).Intersect(b.Skip(i)).ToList()){a.Remove(e);a.Insert(j,e);}}


            Try it online!






            share|improve this answer











            $endgroup$














              Your Answer





              StackExchange.ifUsing("editor", function () {
              return StackExchange.using("mathjaxEditing", function () {
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              });
              });
              }, "mathjax-editing");

              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "200"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182677%2freorder-a-master-list-based-on-a-reordered-subset%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              9 Answers
              9






              active

              oldest

              votes








              9 Answers
              9






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              4












              $begingroup$


              Retina 0.8.2, 51 bytes



              +`(b(w+),(w+)b.*¶.*b)3,(.*b2b)
              $1$4,$3
              1A`


              Try it online! Takes input as a comma-separated list of subwords on the first line and a comma-separated master list of words on the second line. Explanation:



              (b(w+),(w+)b.*¶.*b)3,(.*b2b)


              Find two adjacent subwords where the second word precedes the first on the master list.



              $1$4,$3


              Move the second word to appear after the first word in the master list.



              +`


              Repeat until no words appear out of order.



              1A`


              Delete the subwords.






              share|improve this answer









              $endgroup$


















                4












                $begingroup$


                Retina 0.8.2, 51 bytes



                +`(b(w+),(w+)b.*¶.*b)3,(.*b2b)
                $1$4,$3
                1A`


                Try it online! Takes input as a comma-separated list of subwords on the first line and a comma-separated master list of words on the second line. Explanation:



                (b(w+),(w+)b.*¶.*b)3,(.*b2b)


                Find two adjacent subwords where the second word precedes the first on the master list.



                $1$4,$3


                Move the second word to appear after the first word in the master list.



                +`


                Repeat until no words appear out of order.



                1A`


                Delete the subwords.






                share|improve this answer









                $endgroup$
















                  4












                  4








                  4





                  $begingroup$


                  Retina 0.8.2, 51 bytes



                  +`(b(w+),(w+)b.*¶.*b)3,(.*b2b)
                  $1$4,$3
                  1A`


                  Try it online! Takes input as a comma-separated list of subwords on the first line and a comma-separated master list of words on the second line. Explanation:



                  (b(w+),(w+)b.*¶.*b)3,(.*b2b)


                  Find two adjacent subwords where the second word precedes the first on the master list.



                  $1$4,$3


                  Move the second word to appear after the first word in the master list.



                  +`


                  Repeat until no words appear out of order.



                  1A`


                  Delete the subwords.






                  share|improve this answer









                  $endgroup$




                  Retina 0.8.2, 51 bytes



                  +`(b(w+),(w+)b.*¶.*b)3,(.*b2b)
                  $1$4,$3
                  1A`


                  Try it online! Takes input as a comma-separated list of subwords on the first line and a comma-separated master list of words on the second line. Explanation:



                  (b(w+),(w+)b.*¶.*b)3,(.*b2b)


                  Find two adjacent subwords where the second word precedes the first on the master list.



                  $1$4,$3


                  Move the second word to appear after the first word in the master list.



                  +`


                  Repeat until no words appear out of order.



                  1A`


                  Delete the subwords.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered yesterday









                  NeilNeil

                  82.5k745179




                  82.5k745179























                      4












                      $begingroup$

                      JavaScript (ES6),  96 89 74  71 bytes



                      This started as a bulky mess and was eventually shrunk to a rather concise and elegant form. I'd like to thank the .splice() method for its fruitful collaboration on that one. ;)



                      Takes input as (master)(subset). Outputs by updating the master list.





                      m=>s=>s.map(p=x=>m.splice(p,0,...m.splice(i=m.indexOf(x),p>i||!(p=i))))


                      Try it online!



                      How?



                      We are using two nested .splice() methods on the same array to conditionally move an element from position $i$ to position $p$:



                      m.splice(p, 0, ...m.splice(i, condition))


                      If the condition is truthy (coerced to $1$):




                      • the inner .splice() removes and returns the element at position $i$ as a singleton array $[element]$

                      • thanks to the spread syntax, this element is expanded as a 3rd argument for the outer .splice(), which causes it to be inserted back at position $p$


                      If the condition is falsy (coerced to $0$):




                      • the inner .splice() removes nothing and returns an empty array

                      • as a result, the outer .splice() receives undefined as its 3rd argument and nothing is inserted either


                      Commented



                      m => s =>                 // m[] = master list, s[] = subset list
                      s.map( //
                      p = // p = position in the master list of the last element from
                      // the subset list (initialized to a non-numeric value)
                      x => // for each element x in the subset list:
                      m.splice( // insert in the master list:
                      p, // at position p
                      0, // without removing any element
                      ...m.splice( // remove from the master list and flatten:
                      i = m.indexOf(x), // i = position of x in the master list
                      p > i // if p is greater than i, remove x from its current
                      // position and insert it at position p
                      || !(p = i) // otherwise, set p to i and don't remove/insert anything
                      ) // end of inner splice()
                      ) // end of outer splice()
                      ) // end of map()





                      share|improve this answer











                      $endgroup$









                      • 1




                        $begingroup$
                        "I'd like to thank the .splice() method for ..." Cue PPCG Oscar's Music... :)
                        $endgroup$
                        – Chas Brown
                        yesterday


















                      4












                      $begingroup$

                      JavaScript (ES6),  96 89 74  71 bytes



                      This started as a bulky mess and was eventually shrunk to a rather concise and elegant form. I'd like to thank the .splice() method for its fruitful collaboration on that one. ;)



                      Takes input as (master)(subset). Outputs by updating the master list.





                      m=>s=>s.map(p=x=>m.splice(p,0,...m.splice(i=m.indexOf(x),p>i||!(p=i))))


                      Try it online!



                      How?



                      We are using two nested .splice() methods on the same array to conditionally move an element from position $i$ to position $p$:



                      m.splice(p, 0, ...m.splice(i, condition))


                      If the condition is truthy (coerced to $1$):




                      • the inner .splice() removes and returns the element at position $i$ as a singleton array $[element]$

                      • thanks to the spread syntax, this element is expanded as a 3rd argument for the outer .splice(), which causes it to be inserted back at position $p$


                      If the condition is falsy (coerced to $0$):




                      • the inner .splice() removes nothing and returns an empty array

                      • as a result, the outer .splice() receives undefined as its 3rd argument and nothing is inserted either


                      Commented



                      m => s =>                 // m[] = master list, s[] = subset list
                      s.map( //
                      p = // p = position in the master list of the last element from
                      // the subset list (initialized to a non-numeric value)
                      x => // for each element x in the subset list:
                      m.splice( // insert in the master list:
                      p, // at position p
                      0, // without removing any element
                      ...m.splice( // remove from the master list and flatten:
                      i = m.indexOf(x), // i = position of x in the master list
                      p > i // if p is greater than i, remove x from its current
                      // position and insert it at position p
                      || !(p = i) // otherwise, set p to i and don't remove/insert anything
                      ) // end of inner splice()
                      ) // end of outer splice()
                      ) // end of map()





                      share|improve this answer











                      $endgroup$









                      • 1




                        $begingroup$
                        "I'd like to thank the .splice() method for ..." Cue PPCG Oscar's Music... :)
                        $endgroup$
                        – Chas Brown
                        yesterday
















                      4












                      4








                      4





                      $begingroup$

                      JavaScript (ES6),  96 89 74  71 bytes



                      This started as a bulky mess and was eventually shrunk to a rather concise and elegant form. I'd like to thank the .splice() method for its fruitful collaboration on that one. ;)



                      Takes input as (master)(subset). Outputs by updating the master list.





                      m=>s=>s.map(p=x=>m.splice(p,0,...m.splice(i=m.indexOf(x),p>i||!(p=i))))


                      Try it online!



                      How?



                      We are using two nested .splice() methods on the same array to conditionally move an element from position $i$ to position $p$:



                      m.splice(p, 0, ...m.splice(i, condition))


                      If the condition is truthy (coerced to $1$):




                      • the inner .splice() removes and returns the element at position $i$ as a singleton array $[element]$

                      • thanks to the spread syntax, this element is expanded as a 3rd argument for the outer .splice(), which causes it to be inserted back at position $p$


                      If the condition is falsy (coerced to $0$):




                      • the inner .splice() removes nothing and returns an empty array

                      • as a result, the outer .splice() receives undefined as its 3rd argument and nothing is inserted either


                      Commented



                      m => s =>                 // m[] = master list, s[] = subset list
                      s.map( //
                      p = // p = position in the master list of the last element from
                      // the subset list (initialized to a non-numeric value)
                      x => // for each element x in the subset list:
                      m.splice( // insert in the master list:
                      p, // at position p
                      0, // without removing any element
                      ...m.splice( // remove from the master list and flatten:
                      i = m.indexOf(x), // i = position of x in the master list
                      p > i // if p is greater than i, remove x from its current
                      // position and insert it at position p
                      || !(p = i) // otherwise, set p to i and don't remove/insert anything
                      ) // end of inner splice()
                      ) // end of outer splice()
                      ) // end of map()





                      share|improve this answer











                      $endgroup$



                      JavaScript (ES6),  96 89 74  71 bytes



                      This started as a bulky mess and was eventually shrunk to a rather concise and elegant form. I'd like to thank the .splice() method for its fruitful collaboration on that one. ;)



                      Takes input as (master)(subset). Outputs by updating the master list.





                      m=>s=>s.map(p=x=>m.splice(p,0,...m.splice(i=m.indexOf(x),p>i||!(p=i))))


                      Try it online!



                      How?



                      We are using two nested .splice() methods on the same array to conditionally move an element from position $i$ to position $p$:



                      m.splice(p, 0, ...m.splice(i, condition))


                      If the condition is truthy (coerced to $1$):




                      • the inner .splice() removes and returns the element at position $i$ as a singleton array $[element]$

                      • thanks to the spread syntax, this element is expanded as a 3rd argument for the outer .splice(), which causes it to be inserted back at position $p$


                      If the condition is falsy (coerced to $0$):




                      • the inner .splice() removes nothing and returns an empty array

                      • as a result, the outer .splice() receives undefined as its 3rd argument and nothing is inserted either


                      Commented



                      m => s =>                 // m[] = master list, s[] = subset list
                      s.map( //
                      p = // p = position in the master list of the last element from
                      // the subset list (initialized to a non-numeric value)
                      x => // for each element x in the subset list:
                      m.splice( // insert in the master list:
                      p, // at position p
                      0, // without removing any element
                      ...m.splice( // remove from the master list and flatten:
                      i = m.indexOf(x), // i = position of x in the master list
                      p > i // if p is greater than i, remove x from its current
                      // position and insert it at position p
                      || !(p = i) // otherwise, set p to i and don't remove/insert anything
                      ) // end of inner splice()
                      ) // end of outer splice()
                      ) // end of map()






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 20 hours ago

























                      answered yesterday









                      ArnauldArnauld

                      80.4k797333




                      80.4k797333








                      • 1




                        $begingroup$
                        "I'd like to thank the .splice() method for ..." Cue PPCG Oscar's Music... :)
                        $endgroup$
                        – Chas Brown
                        yesterday
















                      • 1




                        $begingroup$
                        "I'd like to thank the .splice() method for ..." Cue PPCG Oscar's Music... :)
                        $endgroup$
                        – Chas Brown
                        yesterday










                      1




                      1




                      $begingroup$
                      "I'd like to thank the .splice() method for ..." Cue PPCG Oscar's Music... :)
                      $endgroup$
                      – Chas Brown
                      yesterday






                      $begingroup$
                      "I'd like to thank the .splice() method for ..." Cue PPCG Oscar's Music... :)
                      $endgroup$
                      – Chas Brown
                      yesterday













                      1












                      $begingroup$

                      Haskell, 79 bytes



                      (m:n)#u@(s:t)|m==s=m:n#t|all(/=m)u=m:n#u|(x,_:z)<-span(/=s)n=(x++s:m:z)#u
                      m#_=m


                      Try it online!



                      (m:n)#u@(s:t)                 -- m: head of master list
                      -- n: tail of master list
                      -- s: head of subset
                      -- t: tail of subset
                      -- u: whole subset
                      |m==s -- if m==s
                      =m:n#t -- return 'm' and append a recursive call with 'n' and 't'
                      |all(/=m)u -- if 'm' is not in 'u'
                      =m:n#u -- return 'm' and append a recursive call with 'n' and 'u'
                      | -- else (note: 's' is element of 'n')
                      (x,_:z)<-span(/=s)n -- split 'n' into a list 'x' before element 's' and
                      -- a list 'z' after element 's' and
                      = (x++s:m:z)#u -- make a recursive call with
                      -- x++s:m:z as the new master list (i.e. 'm' inserted into 'n' after 's')
                      -- and 'u'
                      m # _ = m -- if either list is emtpy, return the master list





                      share|improve this answer











                      $endgroup$


















                        1












                        $begingroup$

                        Haskell, 79 bytes



                        (m:n)#u@(s:t)|m==s=m:n#t|all(/=m)u=m:n#u|(x,_:z)<-span(/=s)n=(x++s:m:z)#u
                        m#_=m


                        Try it online!



                        (m:n)#u@(s:t)                 -- m: head of master list
                        -- n: tail of master list
                        -- s: head of subset
                        -- t: tail of subset
                        -- u: whole subset
                        |m==s -- if m==s
                        =m:n#t -- return 'm' and append a recursive call with 'n' and 't'
                        |all(/=m)u -- if 'm' is not in 'u'
                        =m:n#u -- return 'm' and append a recursive call with 'n' and 'u'
                        | -- else (note: 's' is element of 'n')
                        (x,_:z)<-span(/=s)n -- split 'n' into a list 'x' before element 's' and
                        -- a list 'z' after element 's' and
                        = (x++s:m:z)#u -- make a recursive call with
                        -- x++s:m:z as the new master list (i.e. 'm' inserted into 'n' after 's')
                        -- and 'u'
                        m # _ = m -- if either list is emtpy, return the master list





                        share|improve this answer











                        $endgroup$
















                          1












                          1








                          1





                          $begingroup$

                          Haskell, 79 bytes



                          (m:n)#u@(s:t)|m==s=m:n#t|all(/=m)u=m:n#u|(x,_:z)<-span(/=s)n=(x++s:m:z)#u
                          m#_=m


                          Try it online!



                          (m:n)#u@(s:t)                 -- m: head of master list
                          -- n: tail of master list
                          -- s: head of subset
                          -- t: tail of subset
                          -- u: whole subset
                          |m==s -- if m==s
                          =m:n#t -- return 'm' and append a recursive call with 'n' and 't'
                          |all(/=m)u -- if 'm' is not in 'u'
                          =m:n#u -- return 'm' and append a recursive call with 'n' and 'u'
                          | -- else (note: 's' is element of 'n')
                          (x,_:z)<-span(/=s)n -- split 'n' into a list 'x' before element 's' and
                          -- a list 'z' after element 's' and
                          = (x++s:m:z)#u -- make a recursive call with
                          -- x++s:m:z as the new master list (i.e. 'm' inserted into 'n' after 's')
                          -- and 'u'
                          m # _ = m -- if either list is emtpy, return the master list





                          share|improve this answer











                          $endgroup$



                          Haskell, 79 bytes



                          (m:n)#u@(s:t)|m==s=m:n#t|all(/=m)u=m:n#u|(x,_:z)<-span(/=s)n=(x++s:m:z)#u
                          m#_=m


                          Try it online!



                          (m:n)#u@(s:t)                 -- m: head of master list
                          -- n: tail of master list
                          -- s: head of subset
                          -- t: tail of subset
                          -- u: whole subset
                          |m==s -- if m==s
                          =m:n#t -- return 'm' and append a recursive call with 'n' and 't'
                          |all(/=m)u -- if 'm' is not in 'u'
                          =m:n#u -- return 'm' and append a recursive call with 'n' and 'u'
                          | -- else (note: 's' is element of 'n')
                          (x,_:z)<-span(/=s)n -- split 'n' into a list 'x' before element 's' and
                          -- a list 'z' after element 's' and
                          = (x++s:m:z)#u -- make a recursive call with
                          -- x++s:m:z as the new master list (i.e. 'm' inserted into 'n' after 's')
                          -- and 'u'
                          m # _ = m -- if either list is emtpy, return the master list






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited yesterday

























                          answered yesterday









                          niminimi

                          32.6k32489




                          32.6k32489























                              1












                              $begingroup$


                              J, 49 bytes



                              [:(<@({:+i.@>:@-/)@i.~C.])^:(>/@i.~)&.>/]|.@;2<[


                              Try it online!



                              Will golf further and add explanation tomorrow






                              share|improve this answer











                              $endgroup$


















                                1












                                $begingroup$


                                J, 49 bytes



                                [:(<@({:+i.@>:@-/)@i.~C.])^:(>/@i.~)&.>/]|.@;2<[


                                Try it online!



                                Will golf further and add explanation tomorrow






                                share|improve this answer











                                $endgroup$
















                                  1












                                  1








                                  1





                                  $begingroup$


                                  J, 49 bytes



                                  [:(<@({:+i.@>:@-/)@i.~C.])^:(>/@i.~)&.>/]|.@;2<[


                                  Try it online!



                                  Will golf further and add explanation tomorrow






                                  share|improve this answer











                                  $endgroup$




                                  J, 49 bytes



                                  [:(<@({:+i.@>:@-/)@i.~C.])^:(>/@i.~)&.>/]|.@;2<[


                                  Try it online!



                                  Will golf further and add explanation tomorrow







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited 23 hours ago

























                                  answered 23 hours ago









                                  JonahJonah

                                  2,5611017




                                  2,5611017























                                      1












                                      $begingroup$


                                      Ruby, 73 68 bytes





                                      ->a,b{0while b.zip(a&b).find{|m,n|m!=n&&a=a[0..a.index(m)]-[n]|a};a}


                                      Try it online!



                                      How?




                                      • The intersection between a and b contains all elements of b, but in the same order as we would find them in a

                                      • So, if we iterate on b and on the intersection in parallel, as soon as we find a difference, we can relocate a single element.

                                      • Relocation is done by cutting a on the position of the element we found in b, then removing the element we found in the intersection, and then adding the remainder of a.

                                      • Repeat from the beginning, until all elements of b are in the right order in a






                                      share|improve this answer











                                      $endgroup$













                                      • $begingroup$
                                        what is the 0 doing in 0while?
                                        $endgroup$
                                        – Jonah
                                        14 hours ago










                                      • $begingroup$
                                        It's just a NOP.
                                        $endgroup$
                                        – G B
                                        13 hours ago










                                      • $begingroup$
                                        why is it needed?
                                        $endgroup$
                                        – Jonah
                                        12 hours ago
















                                      1












                                      $begingroup$


                                      Ruby, 73 68 bytes





                                      ->a,b{0while b.zip(a&b).find{|m,n|m!=n&&a=a[0..a.index(m)]-[n]|a};a}


                                      Try it online!



                                      How?




                                      • The intersection between a and b contains all elements of b, but in the same order as we would find them in a

                                      • So, if we iterate on b and on the intersection in parallel, as soon as we find a difference, we can relocate a single element.

                                      • Relocation is done by cutting a on the position of the element we found in b, then removing the element we found in the intersection, and then adding the remainder of a.

                                      • Repeat from the beginning, until all elements of b are in the right order in a






                                      share|improve this answer











                                      $endgroup$













                                      • $begingroup$
                                        what is the 0 doing in 0while?
                                        $endgroup$
                                        – Jonah
                                        14 hours ago










                                      • $begingroup$
                                        It's just a NOP.
                                        $endgroup$
                                        – G B
                                        13 hours ago










                                      • $begingroup$
                                        why is it needed?
                                        $endgroup$
                                        – Jonah
                                        12 hours ago














                                      1












                                      1








                                      1





                                      $begingroup$


                                      Ruby, 73 68 bytes





                                      ->a,b{0while b.zip(a&b).find{|m,n|m!=n&&a=a[0..a.index(m)]-[n]|a};a}


                                      Try it online!



                                      How?




                                      • The intersection between a and b contains all elements of b, but in the same order as we would find them in a

                                      • So, if we iterate on b and on the intersection in parallel, as soon as we find a difference, we can relocate a single element.

                                      • Relocation is done by cutting a on the position of the element we found in b, then removing the element we found in the intersection, and then adding the remainder of a.

                                      • Repeat from the beginning, until all elements of b are in the right order in a






                                      share|improve this answer











                                      $endgroup$




                                      Ruby, 73 68 bytes





                                      ->a,b{0while b.zip(a&b).find{|m,n|m!=n&&a=a[0..a.index(m)]-[n]|a};a}


                                      Try it online!



                                      How?




                                      • The intersection between a and b contains all elements of b, but in the same order as we would find them in a

                                      • So, if we iterate on b and on the intersection in parallel, as soon as we find a difference, we can relocate a single element.

                                      • Relocation is done by cutting a on the position of the element we found in b, then removing the element we found in the intersection, and then adding the remainder of a.

                                      • Repeat from the beginning, until all elements of b are in the right order in a







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited 21 hours ago

























                                      answered 22 hours ago









                                      G BG B

                                      8,1961429




                                      8,1961429












                                      • $begingroup$
                                        what is the 0 doing in 0while?
                                        $endgroup$
                                        – Jonah
                                        14 hours ago










                                      • $begingroup$
                                        It's just a NOP.
                                        $endgroup$
                                        – G B
                                        13 hours ago










                                      • $begingroup$
                                        why is it needed?
                                        $endgroup$
                                        – Jonah
                                        12 hours ago


















                                      • $begingroup$
                                        what is the 0 doing in 0while?
                                        $endgroup$
                                        – Jonah
                                        14 hours ago










                                      • $begingroup$
                                        It's just a NOP.
                                        $endgroup$
                                        – G B
                                        13 hours ago










                                      • $begingroup$
                                        why is it needed?
                                        $endgroup$
                                        – Jonah
                                        12 hours ago
















                                      $begingroup$
                                      what is the 0 doing in 0while?
                                      $endgroup$
                                      – Jonah
                                      14 hours ago




                                      $begingroup$
                                      what is the 0 doing in 0while?
                                      $endgroup$
                                      – Jonah
                                      14 hours ago












                                      $begingroup$
                                      It's just a NOP.
                                      $endgroup$
                                      – G B
                                      13 hours ago




                                      $begingroup$
                                      It's just a NOP.
                                      $endgroup$
                                      – G B
                                      13 hours ago












                                      $begingroup$
                                      why is it needed?
                                      $endgroup$
                                      – Jonah
                                      12 hours ago




                                      $begingroup$
                                      why is it needed?
                                      $endgroup$
                                      – Jonah
                                      12 hours ago











                                      1












                                      $begingroup$


                                      Python 2, 124 109 106 99 96 bytes





                                      def f(a,b,i=0):
                                      while b[i+1:]:x,y=map(a.index,b)[i:i+2];i+=1;x>y>a.insert(x,a.pop(y))
                                      return a


                                      Try it online!






                                      share|improve this answer











                                      $endgroup$


















                                        1












                                        $begingroup$


                                        Python 2, 124 109 106 99 96 bytes





                                        def f(a,b,i=0):
                                        while b[i+1:]:x,y=map(a.index,b)[i:i+2];i+=1;x>y>a.insert(x,a.pop(y))
                                        return a


                                        Try it online!






                                        share|improve this answer











                                        $endgroup$
















                                          1












                                          1








                                          1





                                          $begingroup$


                                          Python 2, 124 109 106 99 96 bytes





                                          def f(a,b,i=0):
                                          while b[i+1:]:x,y=map(a.index,b)[i:i+2];i+=1;x>y>a.insert(x,a.pop(y))
                                          return a


                                          Try it online!






                                          share|improve this answer











                                          $endgroup$




                                          Python 2, 124 109 106 99 96 bytes





                                          def f(a,b,i=0):
                                          while b[i+1:]:x,y=map(a.index,b)[i:i+2];i+=1;x>y>a.insert(x,a.pop(y))
                                          return a


                                          Try it online!







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited 4 hours ago

























                                          answered yesterday









                                          Chas BrownChas Brown

                                          5,1291523




                                          5,1291523























                                              1












                                              $begingroup$


                                              Perl 6, 40 bytes





                                              {*.permutations.first(*.grep(.any)eq$_)}


                                              Try it online!



                                              Anonymous code block that takes the input curried (like f(subList)(masterList), and finds the first lexographical permutation of the indexes of the master list where the elements from the sub list are in the correct order.



                                              Intuitively, the first satisfying permutation will leave the correctly ordered elements in the original order, while moving the incorrectly placed ones the minimum needed distance forward in order to have them in the correct order, which places them directly after the previous element in the subset.



                                              Explanation:



                                              {*                                     } # Anonymous code block that returns a lambda
                                              .permutations # In all permutations of the master list
                                              .first( ) # Find the first permutation
                                              (*.grep(.any) # Where the order of the subset
                                              eq$_ # Is the same as the given order







                                              share|improve this answer











                                              $endgroup$


















                                                1












                                                $begingroup$


                                                Perl 6, 40 bytes





                                                {*.permutations.first(*.grep(.any)eq$_)}


                                                Try it online!



                                                Anonymous code block that takes the input curried (like f(subList)(masterList), and finds the first lexographical permutation of the indexes of the master list where the elements from the sub list are in the correct order.



                                                Intuitively, the first satisfying permutation will leave the correctly ordered elements in the original order, while moving the incorrectly placed ones the minimum needed distance forward in order to have them in the correct order, which places them directly after the previous element in the subset.



                                                Explanation:



                                                {*                                     } # Anonymous code block that returns a lambda
                                                .permutations # In all permutations of the master list
                                                .first( ) # Find the first permutation
                                                (*.grep(.any) # Where the order of the subset
                                                eq$_ # Is the same as the given order







                                                share|improve this answer











                                                $endgroup$
















                                                  1












                                                  1








                                                  1





                                                  $begingroup$


                                                  Perl 6, 40 bytes





                                                  {*.permutations.first(*.grep(.any)eq$_)}


                                                  Try it online!



                                                  Anonymous code block that takes the input curried (like f(subList)(masterList), and finds the first lexographical permutation of the indexes of the master list where the elements from the sub list are in the correct order.



                                                  Intuitively, the first satisfying permutation will leave the correctly ordered elements in the original order, while moving the incorrectly placed ones the minimum needed distance forward in order to have them in the correct order, which places them directly after the previous element in the subset.



                                                  Explanation:



                                                  {*                                     } # Anonymous code block that returns a lambda
                                                  .permutations # In all permutations of the master list
                                                  .first( ) # Find the first permutation
                                                  (*.grep(.any) # Where the order of the subset
                                                  eq$_ # Is the same as the given order







                                                  share|improve this answer











                                                  $endgroup$




                                                  Perl 6, 40 bytes





                                                  {*.permutations.first(*.grep(.any)eq$_)}


                                                  Try it online!



                                                  Anonymous code block that takes the input curried (like f(subList)(masterList), and finds the first lexographical permutation of the indexes of the master list where the elements from the sub list are in the correct order.



                                                  Intuitively, the first satisfying permutation will leave the correctly ordered elements in the original order, while moving the incorrectly placed ones the minimum needed distance forward in order to have them in the correct order, which places them directly after the previous element in the subset.



                                                  Explanation:



                                                  {*                                     } # Anonymous code block that returns a lambda
                                                  .permutations # In all permutations of the master list
                                                  .first( ) # Find the first permutation
                                                  (*.grep(.any) # Where the order of the subset
                                                  eq$_ # Is the same as the given order








                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited 3 hours ago

























                                                  answered 16 hours ago









                                                  Jo KingJo King

                                                  26.4k364130




                                                  26.4k364130























                                                      0












                                                      $begingroup$


                                                      Jelly, 9 bytes



                                                      Œ!iⱮṢƑ¥ƇḢ


                                                      Try it online! or Test suite



                                                      Inefficient, particularly with large master lists. Generates all possible permutations, filters out those where the subset is in the wrong order, and then returns the first.



                                                      Alternatively, a faster approach is:




                                                      Jelly, 24 bytes



                                                      i@€MƤFṬœṗƲḊ;JḟF}W€ʋ@¥ṢFị


                                                      Try it online!






                                                      share|improve this answer











                                                      $endgroup$













                                                      • $begingroup$
                                                        This doesn't seem like it would conform to the rule "Where the master list has two elements out of order according to the subset list, the item that is earlier in the master list should be moved to the earliest index where it is in the correct location relative to other items within the subset list. (i.e. immediately after the later item)"
                                                        $endgroup$
                                                        – Beefster
                                                        9 hours ago










                                                      • $begingroup$
                                                        @Beefster it works on the ones I’ve tried so far. I think the ordering of the permutations is such that this is the correct result. Happy to be proved wrong if there’s a counterexample.
                                                        $endgroup$
                                                        – Nick Kennedy
                                                        9 hours ago










                                                      • $begingroup$
                                                        @Beefster I’ve now tried all of your examples except the female names and the 1..12 and the ordering of the result is correct.
                                                        $endgroup$
                                                        – Nick Kennedy
                                                        8 hours ago










                                                      • $begingroup$
                                                        @Beefster My answer has a partial explanation for why this works
                                                        $endgroup$
                                                        – Jo King
                                                        3 hours ago
















                                                      0












                                                      $begingroup$


                                                      Jelly, 9 bytes



                                                      Œ!iⱮṢƑ¥ƇḢ


                                                      Try it online! or Test suite



                                                      Inefficient, particularly with large master lists. Generates all possible permutations, filters out those where the subset is in the wrong order, and then returns the first.



                                                      Alternatively, a faster approach is:




                                                      Jelly, 24 bytes



                                                      i@€MƤFṬœṗƲḊ;JḟF}W€ʋ@¥ṢFị


                                                      Try it online!






                                                      share|improve this answer











                                                      $endgroup$













                                                      • $begingroup$
                                                        This doesn't seem like it would conform to the rule "Where the master list has two elements out of order according to the subset list, the item that is earlier in the master list should be moved to the earliest index where it is in the correct location relative to other items within the subset list. (i.e. immediately after the later item)"
                                                        $endgroup$
                                                        – Beefster
                                                        9 hours ago










                                                      • $begingroup$
                                                        @Beefster it works on the ones I’ve tried so far. I think the ordering of the permutations is such that this is the correct result. Happy to be proved wrong if there’s a counterexample.
                                                        $endgroup$
                                                        – Nick Kennedy
                                                        9 hours ago










                                                      • $begingroup$
                                                        @Beefster I’ve now tried all of your examples except the female names and the 1..12 and the ordering of the result is correct.
                                                        $endgroup$
                                                        – Nick Kennedy
                                                        8 hours ago










                                                      • $begingroup$
                                                        @Beefster My answer has a partial explanation for why this works
                                                        $endgroup$
                                                        – Jo King
                                                        3 hours ago














                                                      0












                                                      0








                                                      0





                                                      $begingroup$


                                                      Jelly, 9 bytes



                                                      Œ!iⱮṢƑ¥ƇḢ


                                                      Try it online! or Test suite



                                                      Inefficient, particularly with large master lists. Generates all possible permutations, filters out those where the subset is in the wrong order, and then returns the first.



                                                      Alternatively, a faster approach is:




                                                      Jelly, 24 bytes



                                                      i@€MƤFṬœṗƲḊ;JḟF}W€ʋ@¥ṢFị


                                                      Try it online!






                                                      share|improve this answer











                                                      $endgroup$




                                                      Jelly, 9 bytes



                                                      Œ!iⱮṢƑ¥ƇḢ


                                                      Try it online! or Test suite



                                                      Inefficient, particularly with large master lists. Generates all possible permutations, filters out those where the subset is in the wrong order, and then returns the first.



                                                      Alternatively, a faster approach is:




                                                      Jelly, 24 bytes



                                                      i@€MƤFṬœṗƲḊ;JḟF}W€ʋ@¥ṢFị


                                                      Try it online!







                                                      share|improve this answer














                                                      share|improve this answer



                                                      share|improve this answer








                                                      edited 8 hours ago

























                                                      answered 9 hours ago









                                                      Nick KennedyNick Kennedy

                                                      1,31649




                                                      1,31649












                                                      • $begingroup$
                                                        This doesn't seem like it would conform to the rule "Where the master list has two elements out of order according to the subset list, the item that is earlier in the master list should be moved to the earliest index where it is in the correct location relative to other items within the subset list. (i.e. immediately after the later item)"
                                                        $endgroup$
                                                        – Beefster
                                                        9 hours ago










                                                      • $begingroup$
                                                        @Beefster it works on the ones I’ve tried so far. I think the ordering of the permutations is such that this is the correct result. Happy to be proved wrong if there’s a counterexample.
                                                        $endgroup$
                                                        – Nick Kennedy
                                                        9 hours ago










                                                      • $begingroup$
                                                        @Beefster I’ve now tried all of your examples except the female names and the 1..12 and the ordering of the result is correct.
                                                        $endgroup$
                                                        – Nick Kennedy
                                                        8 hours ago










                                                      • $begingroup$
                                                        @Beefster My answer has a partial explanation for why this works
                                                        $endgroup$
                                                        – Jo King
                                                        3 hours ago


















                                                      • $begingroup$
                                                        This doesn't seem like it would conform to the rule "Where the master list has two elements out of order according to the subset list, the item that is earlier in the master list should be moved to the earliest index where it is in the correct location relative to other items within the subset list. (i.e. immediately after the later item)"
                                                        $endgroup$
                                                        – Beefster
                                                        9 hours ago










                                                      • $begingroup$
                                                        @Beefster it works on the ones I’ve tried so far. I think the ordering of the permutations is such that this is the correct result. Happy to be proved wrong if there’s a counterexample.
                                                        $endgroup$
                                                        – Nick Kennedy
                                                        9 hours ago










                                                      • $begingroup$
                                                        @Beefster I’ve now tried all of your examples except the female names and the 1..12 and the ordering of the result is correct.
                                                        $endgroup$
                                                        – Nick Kennedy
                                                        8 hours ago










                                                      • $begingroup$
                                                        @Beefster My answer has a partial explanation for why this works
                                                        $endgroup$
                                                        – Jo King
                                                        3 hours ago
















                                                      $begingroup$
                                                      This doesn't seem like it would conform to the rule "Where the master list has two elements out of order according to the subset list, the item that is earlier in the master list should be moved to the earliest index where it is in the correct location relative to other items within the subset list. (i.e. immediately after the later item)"
                                                      $endgroup$
                                                      – Beefster
                                                      9 hours ago




                                                      $begingroup$
                                                      This doesn't seem like it would conform to the rule "Where the master list has two elements out of order according to the subset list, the item that is earlier in the master list should be moved to the earliest index where it is in the correct location relative to other items within the subset list. (i.e. immediately after the later item)"
                                                      $endgroup$
                                                      – Beefster
                                                      9 hours ago












                                                      $begingroup$
                                                      @Beefster it works on the ones I’ve tried so far. I think the ordering of the permutations is such that this is the correct result. Happy to be proved wrong if there’s a counterexample.
                                                      $endgroup$
                                                      – Nick Kennedy
                                                      9 hours ago




                                                      $begingroup$
                                                      @Beefster it works on the ones I’ve tried so far. I think the ordering of the permutations is such that this is the correct result. Happy to be proved wrong if there’s a counterexample.
                                                      $endgroup$
                                                      – Nick Kennedy
                                                      9 hours ago












                                                      $begingroup$
                                                      @Beefster I’ve now tried all of your examples except the female names and the 1..12 and the ordering of the result is correct.
                                                      $endgroup$
                                                      – Nick Kennedy
                                                      8 hours ago




                                                      $begingroup$
                                                      @Beefster I’ve now tried all of your examples except the female names and the 1..12 and the ordering of the result is correct.
                                                      $endgroup$
                                                      – Nick Kennedy
                                                      8 hours ago












                                                      $begingroup$
                                                      @Beefster My answer has a partial explanation for why this works
                                                      $endgroup$
                                                      – Jo King
                                                      3 hours ago




                                                      $begingroup$
                                                      @Beefster My answer has a partial explanation for why this works
                                                      $endgroup$
                                                      – Jo King
                                                      3 hours ago











                                                      0












                                                      $begingroup$


                                                      C# (Visual C# Interactive Compiler), 136 bytes





                                                      a=>b=>{for(int i=0,j;i<b.Count;)foreach(var e in a.Take(j=a.IndexOf(b[i++])).Intersect(b.Skip(i)).ToList()){a.Remove(e);a.Insert(j,e);}}


                                                      Try it online!






                                                      share|improve this answer











                                                      $endgroup$


















                                                        0












                                                        $begingroup$


                                                        C# (Visual C# Interactive Compiler), 136 bytes





                                                        a=>b=>{for(int i=0,j;i<b.Count;)foreach(var e in a.Take(j=a.IndexOf(b[i++])).Intersect(b.Skip(i)).ToList()){a.Remove(e);a.Insert(j,e);}}


                                                        Try it online!






                                                        share|improve this answer











                                                        $endgroup$
















                                                          0












                                                          0








                                                          0





                                                          $begingroup$


                                                          C# (Visual C# Interactive Compiler), 136 bytes





                                                          a=>b=>{for(int i=0,j;i<b.Count;)foreach(var e in a.Take(j=a.IndexOf(b[i++])).Intersect(b.Skip(i)).ToList()){a.Remove(e);a.Insert(j,e);}}


                                                          Try it online!






                                                          share|improve this answer











                                                          $endgroup$




                                                          C# (Visual C# Interactive Compiler), 136 bytes





                                                          a=>b=>{for(int i=0,j;i<b.Count;)foreach(var e in a.Take(j=a.IndexOf(b[i++])).Intersect(b.Skip(i)).ToList()){a.Remove(e);a.Insert(j,e);}}


                                                          Try it online!







                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited 3 hours ago

























                                                          answered 3 hours ago









                                                          danadana

                                                          1,921167




                                                          1,921167






























                                                              draft saved

                                                              draft discarded




















































                                                              If this is an answer to a challenge…




                                                              • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.


                                                              • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
                                                                Explanations of your answer make it more interesting to read and are very much encouraged.


                                                              • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.



                                                              More generally…




                                                              • …Please make sure to answer the question and provide sufficient detail.


                                                              • …Avoid asking for help, clarification or responding to other answers (use comments instead).





                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function () {
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182677%2freorder-a-master-list-based-on-a-reordered-subset%23new-answer', 'question_page');
                                                              }
                                                              );

                                                              Post as a guest















                                                              Required, but never shown





















































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown

































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown







                                                              Popular posts from this blog

                                                              Why not use the yoke to control yaw, as well as pitch and roll? Announcing the arrival of...

                                                              Couldn't open a raw socket. Error: Permission denied (13) (nmap)Is it possible to run networking commands...

                                                              VNC viewer RFB protocol error: bad desktop size 0x0I Cannot Type the Key 'd' (lowercase) in VNC Viewer...