Matchmaker, Matchmaker, make me a matchFivenum and a little bitGeneralised Array RiffleAre these trees...

How can one write good dialogue in a story without sounding wooden?

During copyediting, journal disagrees about spelling of paper's main topic

Are there any sports for which the world's best player is female?

Confirming the Identity of a (Friendly) Reviewer After the Reviews

Combining latex input and sed

Why doesn't sea level show seasonality?

Why are Hobbits so fond of mushrooms?

How do you glue a text to a point?

What is this triple-transistor arrangement called?

Cracking the Coding Interview — 1.5 One Away

How can a dictatorship government be beneficial to a dictator in a post-scarcity society?

How can I effectively communicate to recruiters that a phone call is not possible?

How were Martello towers supposed to work?

RPI3B+: What are the four components below the HDMI connector called?

Is there a way to know which symbolic expression mathematica used

Is a request to book a business flight ticket for a graduate student an unreasonable one?

How to md5 a list of filepaths contained in a file?

Received a dinner invitation through my employer's email, is it ok to attend?

Ownership of a PhD Student's Research

Is anyone advocating the promotion of homosexuality in UK schools?

What steps should I take to lawfully visit the United States as a tourist immediately after visiting on a B-1 visa?

Why do players in the past play much longer tournaments than today's top players?

Using Newton's shell theorem to accelerate a spaceship

Why isn't there research to build a standard lunar, or Martian mobility platform?



Matchmaker, Matchmaker, make me a match


Fivenum and a little bitGeneralised Array RiffleAre these trees isomorphic?Generate a looping arrayFind the indices of values in one list in anotherIs this a function?Compare the averages of my listsWhere's my value?Disappearing ElementsMy array should equal this, but it doesn't!Maximum summed subsequences with non-adjacent items






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







7












$begingroup$


(we won't be finding a Find or catching a tryCatch, though)



This is part two of a multi-part series of implementing some interesting R functions. Part one can be found here.



The task:



You are to implement R's match function in as few bytes as possible.



Input:





  • x, a possibly empty list/array of integers


  • table, a possibly empty list/array of integers


  • nomatch, a single integer value


  • incomparables, a possibly empty list/array of integers


Output:




  • a single array/list of integers O of equal length to x, where each value O[i] represents either:


    • The index j of the first value in table where table[j]==x[i]


    • nomatch, indicating that no value in table is equal to x[i] OR that x[i] is in the list of incomparables.




Test Cases



All in the form x, table, nomatch, incomparables -> output
outputs

[], [1,2,3], 0, [5] -> []

[1, 2, 3], [], 0, [5] -> [0, 0, 0]

[9, 4, 3, 6, 3], [9, 8, 7, 6, 5, 4, 3, 2, 1], -1, [4] -> [1, -1, 7, 4, 7]

[8, 6, 7, 5, 3, 0, 9], [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6], 1000, [1] -> [12, 8, 14, 5, 1, 1000, 6]


More test cases can be generated as needed.



Additional rules:




  • R has 1-based indices, but a consistent alternative-based indices are acceptable. So you can use indices that start at 3 or 17 or whatever, but this must be consistent, and you must indicate this in your answer.

  • If you chosen language has a builtin that does this, please also implement your own solution.

  • Explanations are appreciated.


This is code-golf, so shortest solution in bytes wins!










share|improve this question









$endgroup$












  • $begingroup$
    If someone can solve this in R without using match or %in% (which is implemented by using match), I will happily award a 150-rep bounty.
    $endgroup$
    – Giuseppe
    6 hours ago




















7












$begingroup$


(we won't be finding a Find or catching a tryCatch, though)



This is part two of a multi-part series of implementing some interesting R functions. Part one can be found here.



The task:



You are to implement R's match function in as few bytes as possible.



Input:





  • x, a possibly empty list/array of integers


  • table, a possibly empty list/array of integers


  • nomatch, a single integer value


  • incomparables, a possibly empty list/array of integers


Output:




  • a single array/list of integers O of equal length to x, where each value O[i] represents either:


    • The index j of the first value in table where table[j]==x[i]


    • nomatch, indicating that no value in table is equal to x[i] OR that x[i] is in the list of incomparables.




Test Cases



All in the form x, table, nomatch, incomparables -> output
outputs

[], [1,2,3], 0, [5] -> []

[1, 2, 3], [], 0, [5] -> [0, 0, 0]

[9, 4, 3, 6, 3], [9, 8, 7, 6, 5, 4, 3, 2, 1], -1, [4] -> [1, -1, 7, 4, 7]

[8, 6, 7, 5, 3, 0, 9], [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6], 1000, [1] -> [12, 8, 14, 5, 1, 1000, 6]


More test cases can be generated as needed.



Additional rules:




  • R has 1-based indices, but a consistent alternative-based indices are acceptable. So you can use indices that start at 3 or 17 or whatever, but this must be consistent, and you must indicate this in your answer.

  • If you chosen language has a builtin that does this, please also implement your own solution.

  • Explanations are appreciated.


This is code-golf, so shortest solution in bytes wins!










share|improve this question









$endgroup$












  • $begingroup$
    If someone can solve this in R without using match or %in% (which is implemented by using match), I will happily award a 150-rep bounty.
    $endgroup$
    – Giuseppe
    6 hours ago
















7












7








7





$begingroup$


(we won't be finding a Find or catching a tryCatch, though)



This is part two of a multi-part series of implementing some interesting R functions. Part one can be found here.



The task:



You are to implement R's match function in as few bytes as possible.



Input:





  • x, a possibly empty list/array of integers


  • table, a possibly empty list/array of integers


  • nomatch, a single integer value


  • incomparables, a possibly empty list/array of integers


Output:




  • a single array/list of integers O of equal length to x, where each value O[i] represents either:


    • The index j of the first value in table where table[j]==x[i]


    • nomatch, indicating that no value in table is equal to x[i] OR that x[i] is in the list of incomparables.




Test Cases



All in the form x, table, nomatch, incomparables -> output
outputs

[], [1,2,3], 0, [5] -> []

[1, 2, 3], [], 0, [5] -> [0, 0, 0]

[9, 4, 3, 6, 3], [9, 8, 7, 6, 5, 4, 3, 2, 1], -1, [4] -> [1, -1, 7, 4, 7]

[8, 6, 7, 5, 3, 0, 9], [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6], 1000, [1] -> [12, 8, 14, 5, 1, 1000, 6]


More test cases can be generated as needed.



Additional rules:




  • R has 1-based indices, but a consistent alternative-based indices are acceptable. So you can use indices that start at 3 or 17 or whatever, but this must be consistent, and you must indicate this in your answer.

  • If you chosen language has a builtin that does this, please also implement your own solution.

  • Explanations are appreciated.


This is code-golf, so shortest solution in bytes wins!










share|improve this question









$endgroup$




(we won't be finding a Find or catching a tryCatch, though)



This is part two of a multi-part series of implementing some interesting R functions. Part one can be found here.



The task:



You are to implement R's match function in as few bytes as possible.



Input:





  • x, a possibly empty list/array of integers


  • table, a possibly empty list/array of integers


  • nomatch, a single integer value


  • incomparables, a possibly empty list/array of integers


Output:




  • a single array/list of integers O of equal length to x, where each value O[i] represents either:


    • The index j of the first value in table where table[j]==x[i]


    • nomatch, indicating that no value in table is equal to x[i] OR that x[i] is in the list of incomparables.




Test Cases



All in the form x, table, nomatch, incomparables -> output
outputs

[], [1,2,3], 0, [5] -> []

[1, 2, 3], [], 0, [5] -> [0, 0, 0]

[9, 4, 3, 6, 3], [9, 8, 7, 6, 5, 4, 3, 2, 1], -1, [4] -> [1, -1, 7, 4, 7]

[8, 6, 7, 5, 3, 0, 9], [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6], 1000, [1] -> [12, 8, 14, 5, 1, 1000, 6]


More test cases can be generated as needed.



Additional rules:




  • R has 1-based indices, but a consistent alternative-based indices are acceptable. So you can use indices that start at 3 or 17 or whatever, but this must be consistent, and you must indicate this in your answer.

  • If you chosen language has a builtin that does this, please also implement your own solution.

  • Explanations are appreciated.


This is code-golf, so shortest solution in bytes wins!







code-golf array-manipulation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 9 hours ago









GiuseppeGiuseppe

18.9k3 gold badges14 silver badges62 bronze badges




18.9k3 gold badges14 silver badges62 bronze badges












  • $begingroup$
    If someone can solve this in R without using match or %in% (which is implemented by using match), I will happily award a 150-rep bounty.
    $endgroup$
    – Giuseppe
    6 hours ago




















  • $begingroup$
    If someone can solve this in R without using match or %in% (which is implemented by using match), I will happily award a 150-rep bounty.
    $endgroup$
    – Giuseppe
    6 hours ago


















$begingroup$
If someone can solve this in R without using match or %in% (which is implemented by using match), I will happily award a 150-rep bounty.
$endgroup$
– Giuseppe
6 hours ago






$begingroup$
If someone can solve this in R without using match or %in% (which is implemented by using match), I will happily award a 150-rep bounty.
$endgroup$
– Giuseppe
6 hours ago












8 Answers
8






active

oldest

votes


















4












$begingroup$


Jelly,  10  8 bytes



-2 thanks to Erik the Outgolfer



,⁷y⁵iⱮ⁶o


A full program accepting four command line arguments, incomparables nomatch table x which prints a Jelly representation* of the list of R's match function results.



Try it online!



How?



e.g. with incomparables nomatch table x = [1,4], 2, [2,4], [4,3,2,1,0]:



,⁷y⁵iⱮ⁶o - Main Link: list, incomparables; list, nomatch
⁷ - newline character 'n'
, - pair (incompararables) with (right) [[1,4],'n']
⁵ - 5th argument (3rd input = table) [2,4]
y - translate (right) with lookup (left) [2,'n']
⁶ - 6th argument (4th input = x) [4,3,2,1,0]
Ɱ - map with:
i - first index of (right) in (left) [0,0,1,0,0]
o - logical OR [2,2,1,2,2]




* An empty list is represented as nothing, a list of lenth one is represented as just the item, while other lists are enclosed in [] and delimited by ,






share|improve this answer











$endgroup$





















    3












    $begingroup$


    R, 83 bytes





    function(x,t,n,i)sapply(x,function(a)c(which(a==t/!rowSums(outer(t,i,`==`))),n)[1])


    Try it online!



    Avoids match, %in% and setdiff.






    share|improve this answer









    $endgroup$













    • $begingroup$
      I'll award this a bounty once the question is eligible.
      $endgroup$
      – Giuseppe
      19 mins ago



















    2












    $begingroup$


    Ruby, 44 bytes



    Zero-indexed.





    ->x,t,n,i{x.map{|e|i-[e]==i&&t.index(e)||n}}


    Try it online!






    share|improve this answer









    $endgroup$





















      2












      $begingroup$


      R, 55 bytes



      In this case, the code doesn't use match with its full functionality, it is just used as an index function. First R answer, so probably incredibly inefficient byte-wise!



      Note (thanks to Giuseppe for the info): %in% and setdiff are also both internally implemented using match, so completely getting rid of this surprisingly useful function will result in a mess. Therefore, there is a 150-rep bounty with no deadline for this! (note that setdiff is allowed, though)





      function(x,t,n,i)ifelse(x%in%setdiff(t,i),match(x,t),n)


      Try it online!



      or...




      R, 5 bytes





      match


      Try it online!






      share|improve this answer











      $endgroup$













      • $begingroup$
        I'm not sure I could do better although I was eschewing the use of %in% and match; if you want to find a good golfy answer without either of those functions (likely to be horrible), I'll bounty this.
        $endgroup$
        – Giuseppe
        6 hours ago










      • $begingroup$
        Ah lol, I just commented in golfR about that...
        $endgroup$
        – Mr. Xcoder
        6 hours ago



















      2












      $begingroup$


      Jelly, 9 8 bytes



      ṣK¥ƒiⱮo⁶


      Try it online!



      A full program that takes three arguments: [[table], incomparables], x, nomatch in that order.






      share|improve this answer











      $endgroup$





















        1












        $begingroup$


        Japt, 14 bytes



        Ë!XøD ©ÒVbD ªW


        Try it






        share|improve this answer









        $endgroup$





















          1












          $begingroup$


          Python 3, 60 bytes





          lambda x,t,n,i:[v in{*t}-{*i}and-~t.index(v)or n for v in x]


          Try it online!






          share|improve this answer











          $endgroup$













          • $begingroup$
            What features of this are specific to 3.8? Looks to me like this could work for any subversion of Python 3.
            $endgroup$
            – Theo
            7 hours ago










          • $begingroup$
            Well, it is not specific to 3.8. I just copy-pasted the auto-generated template on TIO so I didn't notice I used 3.8. Thanks for the heads-up, will adjust.
            $endgroup$
            – Mr. Xcoder
            7 hours ago






          • 1




            $begingroup$
            R has 1-based indices, but a consistent alternative-based indices are acceptable. So you can take out the -~ and just use 0-indexing for -1 bytes.
            $endgroup$
            – Value Ink
            7 hours ago






          • 1




            $begingroup$
            @ValueInk That fails for the 3rd test case (and in general when a matching element is at the beginning of a list), since 0 is falsy in Python.
            $endgroup$
            – Mr. Xcoder
            7 hours ago






          • 1




            $begingroup$
            Ah, fair show. Incidentally, t.index(v)if v in{*t}-{*i}else n has the exact same bytecount as your current v in{*t}-{*i}and-~t.index(v)or n solution, haha
            $endgroup$
            – Value Ink
            7 hours ago



















          1












          $begingroup$


          Charcoal, 14 bytes



          IEθ∨∧¬№ει⊕⌕ηιζ


          Try it online! Link is to verbose version of code. 1-indexed. Explanation:



            θ             First input (x)
          E Map over elements
          ε Fourth input (incomparables)
          № Count occurrences of
          ι Current element
          ¬ Is zero
          ∧ Logical And
          η Second input (table)
          ⌕ Find 0-based index of
          ι Current element
          ⊕ Convert to 1-indexed
          ∨ Logical Or
          ζ Third input (nomatch)
          I Cast to string
          Implicitly print on separate lines





          share|improve this answer









          $endgroup$
















            Your Answer






            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%2f188162%2fmatchmaker-matchmaker-make-me-a-match%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            8 Answers
            8






            active

            oldest

            votes








            8 Answers
            8






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4












            $begingroup$


            Jelly,  10  8 bytes



            -2 thanks to Erik the Outgolfer



            ,⁷y⁵iⱮ⁶o


            A full program accepting four command line arguments, incomparables nomatch table x which prints a Jelly representation* of the list of R's match function results.



            Try it online!



            How?



            e.g. with incomparables nomatch table x = [1,4], 2, [2,4], [4,3,2,1,0]:



            ,⁷y⁵iⱮ⁶o - Main Link: list, incomparables; list, nomatch
            ⁷ - newline character 'n'
            , - pair (incompararables) with (right) [[1,4],'n']
            ⁵ - 5th argument (3rd input = table) [2,4]
            y - translate (right) with lookup (left) [2,'n']
            ⁶ - 6th argument (4th input = x) [4,3,2,1,0]
            Ɱ - map with:
            i - first index of (right) in (left) [0,0,1,0,0]
            o - logical OR [2,2,1,2,2]




            * An empty list is represented as nothing, a list of lenth one is represented as just the item, while other lists are enclosed in [] and delimited by ,






            share|improve this answer











            $endgroup$


















              4












              $begingroup$


              Jelly,  10  8 bytes



              -2 thanks to Erik the Outgolfer



              ,⁷y⁵iⱮ⁶o


              A full program accepting four command line arguments, incomparables nomatch table x which prints a Jelly representation* of the list of R's match function results.



              Try it online!



              How?



              e.g. with incomparables nomatch table x = [1,4], 2, [2,4], [4,3,2,1,0]:



              ,⁷y⁵iⱮ⁶o - Main Link: list, incomparables; list, nomatch
              ⁷ - newline character 'n'
              , - pair (incompararables) with (right) [[1,4],'n']
              ⁵ - 5th argument (3rd input = table) [2,4]
              y - translate (right) with lookup (left) [2,'n']
              ⁶ - 6th argument (4th input = x) [4,3,2,1,0]
              Ɱ - map with:
              i - first index of (right) in (left) [0,0,1,0,0]
              o - logical OR [2,2,1,2,2]




              * An empty list is represented as nothing, a list of lenth one is represented as just the item, while other lists are enclosed in [] and delimited by ,






              share|improve this answer











              $endgroup$
















                4












                4








                4





                $begingroup$


                Jelly,  10  8 bytes



                -2 thanks to Erik the Outgolfer



                ,⁷y⁵iⱮ⁶o


                A full program accepting four command line arguments, incomparables nomatch table x which prints a Jelly representation* of the list of R's match function results.



                Try it online!



                How?



                e.g. with incomparables nomatch table x = [1,4], 2, [2,4], [4,3,2,1,0]:



                ,⁷y⁵iⱮ⁶o - Main Link: list, incomparables; list, nomatch
                ⁷ - newline character 'n'
                , - pair (incompararables) with (right) [[1,4],'n']
                ⁵ - 5th argument (3rd input = table) [2,4]
                y - translate (right) with lookup (left) [2,'n']
                ⁶ - 6th argument (4th input = x) [4,3,2,1,0]
                Ɱ - map with:
                i - first index of (right) in (left) [0,0,1,0,0]
                o - logical OR [2,2,1,2,2]




                * An empty list is represented as nothing, a list of lenth one is represented as just the item, while other lists are enclosed in [] and delimited by ,






                share|improve this answer











                $endgroup$




                Jelly,  10  8 bytes



                -2 thanks to Erik the Outgolfer



                ,⁷y⁵iⱮ⁶o


                A full program accepting four command line arguments, incomparables nomatch table x which prints a Jelly representation* of the list of R's match function results.



                Try it online!



                How?



                e.g. with incomparables nomatch table x = [1,4], 2, [2,4], [4,3,2,1,0]:



                ,⁷y⁵iⱮ⁶o - Main Link: list, incomparables; list, nomatch
                ⁷ - newline character 'n'
                , - pair (incompararables) with (right) [[1,4],'n']
                ⁵ - 5th argument (3rd input = table) [2,4]
                y - translate (right) with lookup (left) [2,'n']
                ⁶ - 6th argument (4th input = x) [4,3,2,1,0]
                Ɱ - map with:
                i - first index of (right) in (left) [0,0,1,0,0]
                o - logical OR [2,2,1,2,2]




                * An empty list is represented as nothing, a list of lenth one is represented as just the item, while other lists are enclosed in [] and delimited by ,







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 6 hours ago

























                answered 7 hours ago









                Jonathan AllanJonathan Allan

                57.3k5 gold badges43 silver badges181 bronze badges




                57.3k5 gold badges43 silver badges181 bronze badges

























                    3












                    $begingroup$


                    R, 83 bytes





                    function(x,t,n,i)sapply(x,function(a)c(which(a==t/!rowSums(outer(t,i,`==`))),n)[1])


                    Try it online!



                    Avoids match, %in% and setdiff.






                    share|improve this answer









                    $endgroup$













                    • $begingroup$
                      I'll award this a bounty once the question is eligible.
                      $endgroup$
                      – Giuseppe
                      19 mins ago
















                    3












                    $begingroup$


                    R, 83 bytes





                    function(x,t,n,i)sapply(x,function(a)c(which(a==t/!rowSums(outer(t,i,`==`))),n)[1])


                    Try it online!



                    Avoids match, %in% and setdiff.






                    share|improve this answer









                    $endgroup$













                    • $begingroup$
                      I'll award this a bounty once the question is eligible.
                      $endgroup$
                      – Giuseppe
                      19 mins ago














                    3












                    3








                    3





                    $begingroup$


                    R, 83 bytes





                    function(x,t,n,i)sapply(x,function(a)c(which(a==t/!rowSums(outer(t,i,`==`))),n)[1])


                    Try it online!



                    Avoids match, %in% and setdiff.






                    share|improve this answer









                    $endgroup$




                    R, 83 bytes





                    function(x,t,n,i)sapply(x,function(a)c(which(a==t/!rowSums(outer(t,i,`==`))),n)[1])


                    Try it online!



                    Avoids match, %in% and setdiff.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 5 hours ago









                    Nick KennedyNick Kennedy

                    4,4848 silver badges14 bronze badges




                    4,4848 silver badges14 bronze badges












                    • $begingroup$
                      I'll award this a bounty once the question is eligible.
                      $endgroup$
                      – Giuseppe
                      19 mins ago


















                    • $begingroup$
                      I'll award this a bounty once the question is eligible.
                      $endgroup$
                      – Giuseppe
                      19 mins ago
















                    $begingroup$
                    I'll award this a bounty once the question is eligible.
                    $endgroup$
                    – Giuseppe
                    19 mins ago




                    $begingroup$
                    I'll award this a bounty once the question is eligible.
                    $endgroup$
                    – Giuseppe
                    19 mins ago











                    2












                    $begingroup$


                    Ruby, 44 bytes



                    Zero-indexed.





                    ->x,t,n,i{x.map{|e|i-[e]==i&&t.index(e)||n}}


                    Try it online!






                    share|improve this answer









                    $endgroup$


















                      2












                      $begingroup$


                      Ruby, 44 bytes



                      Zero-indexed.





                      ->x,t,n,i{x.map{|e|i-[e]==i&&t.index(e)||n}}


                      Try it online!






                      share|improve this answer









                      $endgroup$
















                        2












                        2








                        2





                        $begingroup$


                        Ruby, 44 bytes



                        Zero-indexed.





                        ->x,t,n,i{x.map{|e|i-[e]==i&&t.index(e)||n}}


                        Try it online!






                        share|improve this answer









                        $endgroup$




                        Ruby, 44 bytes



                        Zero-indexed.





                        ->x,t,n,i{x.map{|e|i-[e]==i&&t.index(e)||n}}


                        Try it online!







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered 7 hours ago









                        Value InkValue Ink

                        8,7957 silver badges32 bronze badges




                        8,7957 silver badges32 bronze badges























                            2












                            $begingroup$


                            R, 55 bytes



                            In this case, the code doesn't use match with its full functionality, it is just used as an index function. First R answer, so probably incredibly inefficient byte-wise!



                            Note (thanks to Giuseppe for the info): %in% and setdiff are also both internally implemented using match, so completely getting rid of this surprisingly useful function will result in a mess. Therefore, there is a 150-rep bounty with no deadline for this! (note that setdiff is allowed, though)





                            function(x,t,n,i)ifelse(x%in%setdiff(t,i),match(x,t),n)


                            Try it online!



                            or...




                            R, 5 bytes





                            match


                            Try it online!






                            share|improve this answer











                            $endgroup$













                            • $begingroup$
                              I'm not sure I could do better although I was eschewing the use of %in% and match; if you want to find a good golfy answer without either of those functions (likely to be horrible), I'll bounty this.
                              $endgroup$
                              – Giuseppe
                              6 hours ago










                            • $begingroup$
                              Ah lol, I just commented in golfR about that...
                              $endgroup$
                              – Mr. Xcoder
                              6 hours ago
















                            2












                            $begingroup$


                            R, 55 bytes



                            In this case, the code doesn't use match with its full functionality, it is just used as an index function. First R answer, so probably incredibly inefficient byte-wise!



                            Note (thanks to Giuseppe for the info): %in% and setdiff are also both internally implemented using match, so completely getting rid of this surprisingly useful function will result in a mess. Therefore, there is a 150-rep bounty with no deadline for this! (note that setdiff is allowed, though)





                            function(x,t,n,i)ifelse(x%in%setdiff(t,i),match(x,t),n)


                            Try it online!



                            or...




                            R, 5 bytes





                            match


                            Try it online!






                            share|improve this answer











                            $endgroup$













                            • $begingroup$
                              I'm not sure I could do better although I was eschewing the use of %in% and match; if you want to find a good golfy answer without either of those functions (likely to be horrible), I'll bounty this.
                              $endgroup$
                              – Giuseppe
                              6 hours ago










                            • $begingroup$
                              Ah lol, I just commented in golfR about that...
                              $endgroup$
                              – Mr. Xcoder
                              6 hours ago














                            2












                            2








                            2





                            $begingroup$


                            R, 55 bytes



                            In this case, the code doesn't use match with its full functionality, it is just used as an index function. First R answer, so probably incredibly inefficient byte-wise!



                            Note (thanks to Giuseppe for the info): %in% and setdiff are also both internally implemented using match, so completely getting rid of this surprisingly useful function will result in a mess. Therefore, there is a 150-rep bounty with no deadline for this! (note that setdiff is allowed, though)





                            function(x,t,n,i)ifelse(x%in%setdiff(t,i),match(x,t),n)


                            Try it online!



                            or...




                            R, 5 bytes





                            match


                            Try it online!






                            share|improve this answer











                            $endgroup$




                            R, 55 bytes



                            In this case, the code doesn't use match with its full functionality, it is just used as an index function. First R answer, so probably incredibly inefficient byte-wise!



                            Note (thanks to Giuseppe for the info): %in% and setdiff are also both internally implemented using match, so completely getting rid of this surprisingly useful function will result in a mess. Therefore, there is a 150-rep bounty with no deadline for this! (note that setdiff is allowed, though)





                            function(x,t,n,i)ifelse(x%in%setdiff(t,i),match(x,t),n)


                            Try it online!



                            or...




                            R, 5 bytes





                            match


                            Try it online!







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 6 hours ago

























                            answered 6 hours ago









                            Mr. XcoderMr. Xcoder

                            32.7k7 gold badges61 silver badges202 bronze badges




                            32.7k7 gold badges61 silver badges202 bronze badges












                            • $begingroup$
                              I'm not sure I could do better although I was eschewing the use of %in% and match; if you want to find a good golfy answer without either of those functions (likely to be horrible), I'll bounty this.
                              $endgroup$
                              – Giuseppe
                              6 hours ago










                            • $begingroup$
                              Ah lol, I just commented in golfR about that...
                              $endgroup$
                              – Mr. Xcoder
                              6 hours ago


















                            • $begingroup$
                              I'm not sure I could do better although I was eschewing the use of %in% and match; if you want to find a good golfy answer without either of those functions (likely to be horrible), I'll bounty this.
                              $endgroup$
                              – Giuseppe
                              6 hours ago










                            • $begingroup$
                              Ah lol, I just commented in golfR about that...
                              $endgroup$
                              – Mr. Xcoder
                              6 hours ago
















                            $begingroup$
                            I'm not sure I could do better although I was eschewing the use of %in% and match; if you want to find a good golfy answer without either of those functions (likely to be horrible), I'll bounty this.
                            $endgroup$
                            – Giuseppe
                            6 hours ago




                            $begingroup$
                            I'm not sure I could do better although I was eschewing the use of %in% and match; if you want to find a good golfy answer without either of those functions (likely to be horrible), I'll bounty this.
                            $endgroup$
                            – Giuseppe
                            6 hours ago












                            $begingroup$
                            Ah lol, I just commented in golfR about that...
                            $endgroup$
                            – Mr. Xcoder
                            6 hours ago




                            $begingroup$
                            Ah lol, I just commented in golfR about that...
                            $endgroup$
                            – Mr. Xcoder
                            6 hours ago











                            2












                            $begingroup$


                            Jelly, 9 8 bytes



                            ṣK¥ƒiⱮo⁶


                            Try it online!



                            A full program that takes three arguments: [[table], incomparables], x, nomatch in that order.






                            share|improve this answer











                            $endgroup$


















                              2












                              $begingroup$


                              Jelly, 9 8 bytes



                              ṣK¥ƒiⱮo⁶


                              Try it online!



                              A full program that takes three arguments: [[table], incomparables], x, nomatch in that order.






                              share|improve this answer











                              $endgroup$
















                                2












                                2








                                2





                                $begingroup$


                                Jelly, 9 8 bytes



                                ṣK¥ƒiⱮo⁶


                                Try it online!



                                A full program that takes three arguments: [[table], incomparables], x, nomatch in that order.






                                share|improve this answer











                                $endgroup$




                                Jelly, 9 8 bytes



                                ṣK¥ƒiⱮo⁶


                                Try it online!



                                A full program that takes three arguments: [[table], incomparables], x, nomatch in that order.







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited 2 hours ago

























                                answered 6 hours ago









                                Nick KennedyNick Kennedy

                                4,4848 silver badges14 bronze badges




                                4,4848 silver badges14 bronze badges























                                    1












                                    $begingroup$


                                    Japt, 14 bytes



                                    Ë!XøD ©ÒVbD ªW


                                    Try it






                                    share|improve this answer









                                    $endgroup$


















                                      1












                                      $begingroup$


                                      Japt, 14 bytes



                                      Ë!XøD ©ÒVbD ªW


                                      Try it






                                      share|improve this answer









                                      $endgroup$
















                                        1












                                        1








                                        1





                                        $begingroup$


                                        Japt, 14 bytes



                                        Ë!XøD ©ÒVbD ªW


                                        Try it






                                        share|improve this answer









                                        $endgroup$




                                        Japt, 14 bytes



                                        Ë!XøD ©ÒVbD ªW


                                        Try it







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered 8 hours ago









                                        ShaggyShaggy

                                        20.3k3 gold badges20 silver badges69 bronze badges




                                        20.3k3 gold badges20 silver badges69 bronze badges























                                            1












                                            $begingroup$


                                            Python 3, 60 bytes





                                            lambda x,t,n,i:[v in{*t}-{*i}and-~t.index(v)or n for v in x]


                                            Try it online!






                                            share|improve this answer











                                            $endgroup$













                                            • $begingroup$
                                              What features of this are specific to 3.8? Looks to me like this could work for any subversion of Python 3.
                                              $endgroup$
                                              – Theo
                                              7 hours ago










                                            • $begingroup$
                                              Well, it is not specific to 3.8. I just copy-pasted the auto-generated template on TIO so I didn't notice I used 3.8. Thanks for the heads-up, will adjust.
                                              $endgroup$
                                              – Mr. Xcoder
                                              7 hours ago






                                            • 1




                                              $begingroup$
                                              R has 1-based indices, but a consistent alternative-based indices are acceptable. So you can take out the -~ and just use 0-indexing for -1 bytes.
                                              $endgroup$
                                              – Value Ink
                                              7 hours ago






                                            • 1




                                              $begingroup$
                                              @ValueInk That fails for the 3rd test case (and in general when a matching element is at the beginning of a list), since 0 is falsy in Python.
                                              $endgroup$
                                              – Mr. Xcoder
                                              7 hours ago






                                            • 1




                                              $begingroup$
                                              Ah, fair show. Incidentally, t.index(v)if v in{*t}-{*i}else n has the exact same bytecount as your current v in{*t}-{*i}and-~t.index(v)or n solution, haha
                                              $endgroup$
                                              – Value Ink
                                              7 hours ago
















                                            1












                                            $begingroup$


                                            Python 3, 60 bytes





                                            lambda x,t,n,i:[v in{*t}-{*i}and-~t.index(v)or n for v in x]


                                            Try it online!






                                            share|improve this answer











                                            $endgroup$













                                            • $begingroup$
                                              What features of this are specific to 3.8? Looks to me like this could work for any subversion of Python 3.
                                              $endgroup$
                                              – Theo
                                              7 hours ago










                                            • $begingroup$
                                              Well, it is not specific to 3.8. I just copy-pasted the auto-generated template on TIO so I didn't notice I used 3.8. Thanks for the heads-up, will adjust.
                                              $endgroup$
                                              – Mr. Xcoder
                                              7 hours ago






                                            • 1




                                              $begingroup$
                                              R has 1-based indices, but a consistent alternative-based indices are acceptable. So you can take out the -~ and just use 0-indexing for -1 bytes.
                                              $endgroup$
                                              – Value Ink
                                              7 hours ago






                                            • 1




                                              $begingroup$
                                              @ValueInk That fails for the 3rd test case (and in general when a matching element is at the beginning of a list), since 0 is falsy in Python.
                                              $endgroup$
                                              – Mr. Xcoder
                                              7 hours ago






                                            • 1




                                              $begingroup$
                                              Ah, fair show. Incidentally, t.index(v)if v in{*t}-{*i}else n has the exact same bytecount as your current v in{*t}-{*i}and-~t.index(v)or n solution, haha
                                              $endgroup$
                                              – Value Ink
                                              7 hours ago














                                            1












                                            1








                                            1





                                            $begingroup$


                                            Python 3, 60 bytes





                                            lambda x,t,n,i:[v in{*t}-{*i}and-~t.index(v)or n for v in x]


                                            Try it online!






                                            share|improve this answer











                                            $endgroup$




                                            Python 3, 60 bytes





                                            lambda x,t,n,i:[v in{*t}-{*i}and-~t.index(v)or n for v in x]


                                            Try it online!







                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited 7 hours ago

























                                            answered 8 hours ago









                                            Mr. XcoderMr. Xcoder

                                            32.7k7 gold badges61 silver badges202 bronze badges




                                            32.7k7 gold badges61 silver badges202 bronze badges












                                            • $begingroup$
                                              What features of this are specific to 3.8? Looks to me like this could work for any subversion of Python 3.
                                              $endgroup$
                                              – Theo
                                              7 hours ago










                                            • $begingroup$
                                              Well, it is not specific to 3.8. I just copy-pasted the auto-generated template on TIO so I didn't notice I used 3.8. Thanks for the heads-up, will adjust.
                                              $endgroup$
                                              – Mr. Xcoder
                                              7 hours ago






                                            • 1




                                              $begingroup$
                                              R has 1-based indices, but a consistent alternative-based indices are acceptable. So you can take out the -~ and just use 0-indexing for -1 bytes.
                                              $endgroup$
                                              – Value Ink
                                              7 hours ago






                                            • 1




                                              $begingroup$
                                              @ValueInk That fails for the 3rd test case (and in general when a matching element is at the beginning of a list), since 0 is falsy in Python.
                                              $endgroup$
                                              – Mr. Xcoder
                                              7 hours ago






                                            • 1




                                              $begingroup$
                                              Ah, fair show. Incidentally, t.index(v)if v in{*t}-{*i}else n has the exact same bytecount as your current v in{*t}-{*i}and-~t.index(v)or n solution, haha
                                              $endgroup$
                                              – Value Ink
                                              7 hours ago


















                                            • $begingroup$
                                              What features of this are specific to 3.8? Looks to me like this could work for any subversion of Python 3.
                                              $endgroup$
                                              – Theo
                                              7 hours ago










                                            • $begingroup$
                                              Well, it is not specific to 3.8. I just copy-pasted the auto-generated template on TIO so I didn't notice I used 3.8. Thanks for the heads-up, will adjust.
                                              $endgroup$
                                              – Mr. Xcoder
                                              7 hours ago






                                            • 1




                                              $begingroup$
                                              R has 1-based indices, but a consistent alternative-based indices are acceptable. So you can take out the -~ and just use 0-indexing for -1 bytes.
                                              $endgroup$
                                              – Value Ink
                                              7 hours ago






                                            • 1




                                              $begingroup$
                                              @ValueInk That fails for the 3rd test case (and in general when a matching element is at the beginning of a list), since 0 is falsy in Python.
                                              $endgroup$
                                              – Mr. Xcoder
                                              7 hours ago






                                            • 1




                                              $begingroup$
                                              Ah, fair show. Incidentally, t.index(v)if v in{*t}-{*i}else n has the exact same bytecount as your current v in{*t}-{*i}and-~t.index(v)or n solution, haha
                                              $endgroup$
                                              – Value Ink
                                              7 hours ago
















                                            $begingroup$
                                            What features of this are specific to 3.8? Looks to me like this could work for any subversion of Python 3.
                                            $endgroup$
                                            – Theo
                                            7 hours ago




                                            $begingroup$
                                            What features of this are specific to 3.8? Looks to me like this could work for any subversion of Python 3.
                                            $endgroup$
                                            – Theo
                                            7 hours ago












                                            $begingroup$
                                            Well, it is not specific to 3.8. I just copy-pasted the auto-generated template on TIO so I didn't notice I used 3.8. Thanks for the heads-up, will adjust.
                                            $endgroup$
                                            – Mr. Xcoder
                                            7 hours ago




                                            $begingroup$
                                            Well, it is not specific to 3.8. I just copy-pasted the auto-generated template on TIO so I didn't notice I used 3.8. Thanks for the heads-up, will adjust.
                                            $endgroup$
                                            – Mr. Xcoder
                                            7 hours ago




                                            1




                                            1




                                            $begingroup$
                                            R has 1-based indices, but a consistent alternative-based indices are acceptable. So you can take out the -~ and just use 0-indexing for -1 bytes.
                                            $endgroup$
                                            – Value Ink
                                            7 hours ago




                                            $begingroup$
                                            R has 1-based indices, but a consistent alternative-based indices are acceptable. So you can take out the -~ and just use 0-indexing for -1 bytes.
                                            $endgroup$
                                            – Value Ink
                                            7 hours ago




                                            1




                                            1




                                            $begingroup$
                                            @ValueInk That fails for the 3rd test case (and in general when a matching element is at the beginning of a list), since 0 is falsy in Python.
                                            $endgroup$
                                            – Mr. Xcoder
                                            7 hours ago




                                            $begingroup$
                                            @ValueInk That fails for the 3rd test case (and in general when a matching element is at the beginning of a list), since 0 is falsy in Python.
                                            $endgroup$
                                            – Mr. Xcoder
                                            7 hours ago




                                            1




                                            1




                                            $begingroup$
                                            Ah, fair show. Incidentally, t.index(v)if v in{*t}-{*i}else n has the exact same bytecount as your current v in{*t}-{*i}and-~t.index(v)or n solution, haha
                                            $endgroup$
                                            – Value Ink
                                            7 hours ago




                                            $begingroup$
                                            Ah, fair show. Incidentally, t.index(v)if v in{*t}-{*i}else n has the exact same bytecount as your current v in{*t}-{*i}and-~t.index(v)or n solution, haha
                                            $endgroup$
                                            – Value Ink
                                            7 hours ago











                                            1












                                            $begingroup$


                                            Charcoal, 14 bytes



                                            IEθ∨∧¬№ει⊕⌕ηιζ


                                            Try it online! Link is to verbose version of code. 1-indexed. Explanation:



                                              θ             First input (x)
                                            E Map over elements
                                            ε Fourth input (incomparables)
                                            № Count occurrences of
                                            ι Current element
                                            ¬ Is zero
                                            ∧ Logical And
                                            η Second input (table)
                                            ⌕ Find 0-based index of
                                            ι Current element
                                            ⊕ Convert to 1-indexed
                                            ∨ Logical Or
                                            ζ Third input (nomatch)
                                            I Cast to string
                                            Implicitly print on separate lines





                                            share|improve this answer









                                            $endgroup$


















                                              1












                                              $begingroup$


                                              Charcoal, 14 bytes



                                              IEθ∨∧¬№ει⊕⌕ηιζ


                                              Try it online! Link is to verbose version of code. 1-indexed. Explanation:



                                                θ             First input (x)
                                              E Map over elements
                                              ε Fourth input (incomparables)
                                              № Count occurrences of
                                              ι Current element
                                              ¬ Is zero
                                              ∧ Logical And
                                              η Second input (table)
                                              ⌕ Find 0-based index of
                                              ι Current element
                                              ⊕ Convert to 1-indexed
                                              ∨ Logical Or
                                              ζ Third input (nomatch)
                                              I Cast to string
                                              Implicitly print on separate lines





                                              share|improve this answer









                                              $endgroup$
















                                                1












                                                1








                                                1





                                                $begingroup$


                                                Charcoal, 14 bytes



                                                IEθ∨∧¬№ει⊕⌕ηιζ


                                                Try it online! Link is to verbose version of code. 1-indexed. Explanation:



                                                  θ             First input (x)
                                                E Map over elements
                                                ε Fourth input (incomparables)
                                                № Count occurrences of
                                                ι Current element
                                                ¬ Is zero
                                                ∧ Logical And
                                                η Second input (table)
                                                ⌕ Find 0-based index of
                                                ι Current element
                                                ⊕ Convert to 1-indexed
                                                ∨ Logical Or
                                                ζ Third input (nomatch)
                                                I Cast to string
                                                Implicitly print on separate lines





                                                share|improve this answer









                                                $endgroup$




                                                Charcoal, 14 bytes



                                                IEθ∨∧¬№ει⊕⌕ηιζ


                                                Try it online! Link is to verbose version of code. 1-indexed. Explanation:



                                                  θ             First input (x)
                                                E Map over elements
                                                ε Fourth input (incomparables)
                                                № Count occurrences of
                                                ι Current element
                                                ¬ Is zero
                                                ∧ Logical And
                                                η Second input (table)
                                                ⌕ Find 0-based index of
                                                ι Current element
                                                ⊕ Convert to 1-indexed
                                                ∨ Logical Or
                                                ζ Third input (nomatch)
                                                I Cast to string
                                                Implicitly print on separate lines






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered 3 hours ago









                                                NeilNeil

                                                86.2k8 gold badges46 silver badges183 bronze badges




                                                86.2k8 gold badges46 silver badges183 bronze badges






























                                                    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%2f188162%2fmatchmaker-matchmaker-make-me-a-match%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

                                                    Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                                                    Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                                                    Ciclooctatetraenă Vezi și | Bibliografie | Meniu de navigare637866text4148569-500570979m