Matchmaker, Matchmaker, make me a matchFivenum 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
Managing and organizing the massively increased number of classes after switching to SOLID?
Why was hardware diversification an asset for the IBM PC ecosystem?
Storming Area 51
Is the genetic term "polycistronic" still used in modern biology?
Print the last, middle and first character of your code
How do you glue a text to a point?
How did the hit man miss?
Good resources for solving techniques (Metaheuristics, MILP, CP etc)
Why weren't bootable game disks ever common on the IBM PC?
definition of "percentile"
Graduate student with abysmal English writing skills, how to help
Is a request to book a business flight ticket for a graduate student an unreasonable one?
If a non-friend comes across my Steam Wishlist, how easily can he gift me one of the games?
Integer Lists of Noah
How to convert a file with several spaces into a tab-delimited file?
How can a dictatorship government be beneficial to a dictator in a post-scarcity society?
Are there any sports for which the world's best player is female?
Cracking the Coding Interview — 1.5 One Away
How were Martello towers supposed to work?
Cops: The Hidden OEIS Substring
US Civil War story: man hanged from a bridge
Why does the U.S. tolerate foreign influence from Saudi Arabia and Israel on its domestic policies while not tolerating that from China or Russia?
Why are all my yellow 2V/20mA LEDs burning out with 330k Ohm resistor?
Optimization terminology: "Exact" v. "Approximate"
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;
$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 integerstable
, a possibly empty list/array of integersnomatch
, a single integer valueincomparables
, a possibly empty list/array of integers
Output:
- a single array/list of integers
O
of equal length tox
, where each valueO[i]
represents either:- The index
j
of the first value intable
wheretable[j]==x[i]
nomatch
, indicating that no value intable
is equal tox[i]
OR thatx[i]
is in the list ofincomparables
.
- The index
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
$endgroup$
add a comment |
$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 integerstable
, a possibly empty list/array of integersnomatch
, a single integer valueincomparables
, a possibly empty list/array of integers
Output:
- a single array/list of integers
O
of equal length tox
, where each valueO[i]
represents either:- The index
j
of the first value intable
wheretable[j]==x[i]
nomatch
, indicating that no value intable
is equal tox[i]
OR thatx[i]
is in the list ofincomparables
.
- The index
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
$endgroup$
$begingroup$
If someone can solve this in R without usingmatch
or%in%
(which is implemented by usingmatch
), I will happily award a 150-rep bounty.
$endgroup$
– Giuseppe
6 hours ago
add a comment |
$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 integerstable
, a possibly empty list/array of integersnomatch
, a single integer valueincomparables
, a possibly empty list/array of integers
Output:
- a single array/list of integers
O
of equal length tox
, where each valueO[i]
represents either:- The index
j
of the first value intable
wheretable[j]==x[i]
nomatch
, indicating that no value intable
is equal tox[i]
OR thatx[i]
is in the list ofincomparables
.
- The index
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
$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 integerstable
, a possibly empty list/array of integersnomatch
, a single integer valueincomparables
, a possibly empty list/array of integers
Output:
- a single array/list of integers
O
of equal length tox
, where each valueO[i]
represents either:- The index
j
of the first value intable
wheretable[j]==x[i]
nomatch
, indicating that no value intable
is equal tox[i]
OR thatx[i]
is in the list ofincomparables
.
- The index
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
code-golf array-manipulation
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 usingmatch
or%in%
(which is implemented by usingmatch
), I will happily award a 150-rep bounty.
$endgroup$
– Giuseppe
6 hours ago
add a comment |
$begingroup$
If someone can solve this in R without usingmatch
or%in%
(which is implemented by usingmatch
), 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
add a comment |
8 Answers
8
active
oldest
votes
$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 ,
$endgroup$
add a comment |
$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
.
$endgroup$
$begingroup$
I'll award this a bounty once the question is eligible.
$endgroup$
– Giuseppe
6 mins ago
add a comment |
$begingroup$
Ruby, 44 bytes
Zero-indexed.
->x,t,n,ix.map
Try it online!
$endgroup$
add a comment |
$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!
$endgroup$
$begingroup$
I'm not sure I could do better although I was eschewing the use of%in%
andmatch
; 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
add a comment |
$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.
$endgroup$
add a comment |
$begingroup$
Japt, 14 bytes
Ë!XøD ©ÒVbD ªW
Try it
$endgroup$
add a comment |
$begingroup$
Python 3, 60 bytes
lambda x,t,n,i:[v in*t-*iand-~t.index(v)or n for v in x]
Try it online!
$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-*ielse n
has the exact same bytecount as your currentv in*t-*iand-~t.index(v)or n
solution, haha
$endgroup$
– Value Ink
6 hours ago
|
show 1 more comment
$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
$endgroup$
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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 ,
$endgroup$
add a comment |
$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 ,
$endgroup$
add a comment |
$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 ,
$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 ,
edited 5 hours ago
answered 6 hours ago
Jonathan AllanJonathan Allan
57.3k5 gold badges43 silver badges181 bronze badges
57.3k5 gold badges43 silver badges181 bronze badges
add a comment |
add a comment |
$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
.
$endgroup$
$begingroup$
I'll award this a bounty once the question is eligible.
$endgroup$
– Giuseppe
6 mins ago
add a comment |
$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
.
$endgroup$
$begingroup$
I'll award this a bounty once the question is eligible.
$endgroup$
– Giuseppe
6 mins ago
add a comment |
$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
.
$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
.
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
6 mins ago
add a comment |
$begingroup$
I'll award this a bounty once the question is eligible.
$endgroup$
– Giuseppe
6 mins ago
$begingroup$
I'll award this a bounty once the question is eligible.
$endgroup$
– Giuseppe
6 mins ago
$begingroup$
I'll award this a bounty once the question is eligible.
$endgroup$
– Giuseppe
6 mins ago
add a comment |
$begingroup$
Ruby, 44 bytes
Zero-indexed.
->x,t,n,ix.map
Try it online!
$endgroup$
add a comment |
$begingroup$
Ruby, 44 bytes
Zero-indexed.
->x,t,n,ix.map
Try it online!
$endgroup$
add a comment |
$begingroup$
Ruby, 44 bytes
Zero-indexed.
->x,t,n,ix.map
Try it online!
$endgroup$
Ruby, 44 bytes
Zero-indexed.
->x,t,n,ix.map
Try it online!
answered 7 hours ago
Value InkValue Ink
8,7957 silver badges32 bronze badges
8,7957 silver badges32 bronze badges
add a comment |
add a comment |
$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!
$endgroup$
$begingroup$
I'm not sure I could do better although I was eschewing the use of%in%
andmatch
; 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
add a comment |
$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!
$endgroup$
$begingroup$
I'm not sure I could do better although I was eschewing the use of%in%
andmatch
; 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
add a comment |
$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!
$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!
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%
andmatch
; 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
add a comment |
$begingroup$
I'm not sure I could do better although I was eschewing the use of%in%
andmatch
; 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
add a comment |
$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.
$endgroup$
add a comment |
$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.
$endgroup$
add a comment |
$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.
$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.
edited 2 hours ago
answered 6 hours ago
Nick KennedyNick Kennedy
4,4848 silver badges14 bronze badges
4,4848 silver badges14 bronze badges
add a comment |
add a comment |
$begingroup$
Japt, 14 bytes
Ë!XøD ©ÒVbD ªW
Try it
$endgroup$
add a comment |
$begingroup$
Japt, 14 bytes
Ë!XøD ©ÒVbD ªW
Try it
$endgroup$
add a comment |
$begingroup$
Japt, 14 bytes
Ë!XøD ©ÒVbD ªW
Try it
$endgroup$
Japt, 14 bytes
Ë!XøD ©ÒVbD ªW
Try it
answered 7 hours ago
ShaggyShaggy
20.3k3 gold badges20 silver badges69 bronze badges
20.3k3 gold badges20 silver badges69 bronze badges
add a comment |
add a comment |
$begingroup$
Python 3, 60 bytes
lambda x,t,n,i:[v in*t-*iand-~t.index(v)or n for v in x]
Try it online!
$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-*ielse n
has the exact same bytecount as your currentv in*t-*iand-~t.index(v)or n
solution, haha
$endgroup$
– Value Ink
6 hours ago
|
show 1 more comment
$begingroup$
Python 3, 60 bytes
lambda x,t,n,i:[v in*t-*iand-~t.index(v)or n for v in x]
Try it online!
$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-*ielse n
has the exact same bytecount as your currentv in*t-*iand-~t.index(v)or n
solution, haha
$endgroup$
– Value Ink
6 hours ago
|
show 1 more comment
$begingroup$
Python 3, 60 bytes
lambda x,t,n,i:[v in*t-*iand-~t.index(v)or n for v in x]
Try it online!
$endgroup$
Python 3, 60 bytes
lambda x,t,n,i:[v in*t-*iand-~t.index(v)or n for v in x]
Try it online!
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-*ielse n
has the exact same bytecount as your currentv in*t-*iand-~t.index(v)or n
solution, haha
$endgroup$
– Value Ink
6 hours ago
|
show 1 more comment
$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-*ielse n
has the exact same bytecount as your currentv in*t-*iand-~t.index(v)or n
solution, haha
$endgroup$
– Value Ink
6 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-*ielse n
has the exact same bytecount as your current v in*t-*iand-~t.index(v)or n
solution, haha$endgroup$
– Value Ink
6 hours ago
$begingroup$
Ah, fair show. Incidentally,
t.index(v)if v in*t-*ielse n
has the exact same bytecount as your current v in*t-*iand-~t.index(v)or n
solution, haha$endgroup$
– Value Ink
6 hours ago
|
show 1 more comment
$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
$endgroup$
add a comment |
$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
$endgroup$
add a comment |
$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
$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
answered 2 hours ago
NeilNeil
86.2k8 gold badges46 silver badges183 bronze badges
86.2k8 gold badges46 silver badges183 bronze badges
add a comment |
add a comment |
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).
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
$begingroup$
If someone can solve this in R without using
match
or%in%
(which is implemented by usingmatch
), I will happily award a 150-rep bounty.$endgroup$
– Giuseppe
6 hours ago