| Bytes | Lang | Time | Link |
|---|---|---|---|
| 032 | Java | 240522T224647Z | Unmitiga |
| 420 | Nibbles | 240522T221110Z | DLosc |
| 080 | AWK | 240522T175035Z | C K |
| 025 | Arturo | 240522T083504Z | chunes |
| 001 | Uiua | 231114T234643Z | chunes |
| 005 | R | 170501T175610Z | Giuseppe |
| 056 | PHP | 170501T180509Z | Jör |
| 034 | Perl 5 | 170501T202550Z | Chris |
| 001 | APL Dyalog | 170501T174447Z | user4180 |
| 025 | Clojure | 170502T091427Z | NikoNyrh |
| 080 | Java 7 | 170502T080455Z | Kevin Cr |
| 051 | Bash + coreutils | 170501T231155Z | Digital |
| 032 | Haskell | 170501T221611Z | xnor |
| 032 | C# | 170501T200553Z | MetaColo |
| 030 | Retina | 170501T175922Z | Business |
| 034 | Haskell | 170501T191551Z | nimi |
| 043 | Haskell | 170501T191339Z | Doorknob |
| 002 | MATL | 170501T175901Z | Luis Men |
| 081 | Java | 170501T174942Z | Leaky Nu |
| 026 | V | 170501T180123Z | DJMcMayh |
| 004 | Japt | 170501T181622Z | ETHprodu |
| 001 | k | 170501T175520Z | skeevey |
| 025 | Python 2 | 170501T175247Z | Leaky Nu |
| nan | 170501T175141Z | Brad Gil | |
| 004 | CJam | 170501T175109Z | Business |
| 028 | JavaScript ES6 | 170501T174937Z | Arnauld |
| 025 | Octave | 170501T174900Z | rahnema1 |
| 025 | Mathematica | 170501T174706Z | Martin E |
| 002 | J | 170501T174423Z | Leaky Nu |
| 003 | Jelly | 170501T173800Z | Leaky Nu |
| 005 | 05AB1E | 170501T174020Z | Riley |
| 004 | Pyth | 170501T174016Z | Leaky Nu |
Java, 32 bytes
n->m->n.stream().map(m::indexOf)
Returns a 0-indexed Stream of Integers.
Nibbles, 4 nibbles (2.0 bytes)
.$?_
Uses 1-based indices. Attempt This Online!
Explanation
.$?_$
. Map the following function over
$ the first program argument:
? Find the index of
$ the function argument
_ in the second program argument
The final $ is inferred if omitted.
AWK, 80 bytes
{ORS="";split($1,n,"");split($2,m,"");for(i in n)for(j in m)print n[i]~m[j]?j:X}
Example:
echo "5341 6841253" | awk '{ORS="";split($1,n,"");split($2,m,"");for(i in n)for(j in m)print n[i]~m[j]?j:X}'
6734
R, 20 5 bytes
1-indexed; match is the builtin function that finds the indices in the second input of the elements of the first, i.e., match(n,m) gives the desired answer
match
thanks to @flodel for pointing out that returning a function is perfectly acceptable as an answer!
PHP, 56 Bytes
0 Indexing
output as String
<?foreach($_GET[0]as$v)echo" ".array_flip($_GET[1])[$v];
PHP, 65 Bytes
Output as array
<?foreach($_GET[0]as$v)$r[]=array_flip($_GET[1])[$v];print_r($r);
PHP, 78 Bytes
workaround with array_map
<?print_r(array_map(function($v){return array_flip($_GET[1])[$v];},$_GET[0]));
for not unique arrays replace with array_flip($_GET[1])[$v] array_search($v,$_GET[1])
Perl 5, 38 34 bytes
4 bytes saved thanks to Dada
sub{map$x{$_}//($x{$_}=++$x)x0,@_}
1-indexed. Takes the lists m and n as a single list, like f(@m,@n). The x0 is just to keep the output from starting with 1,2,3,4,5, etc.
APL (Dyalog), 1 byte
⍳
Note: the ⍳ function does not take scalars as its left argument, so to give it a left argument like 54, you have to make it into an array using , like so (,54).
Clojure, 25 bytes
#(map(zipmap %2(range))%)
0-indexed.
Java 7, 80 bytes
void c(int[]a,java.util.List b){for(int i=0;i<a.length;a[i]=b.indexOf(a[i++]));}
0-indexed
Explanation:
void c(int[]a,java.util.List b){ // Method with integer-array and List parameters
for(int i=0;i<a.length; // Loop over the integer-array
a[i]=b.indexOf(a[i++]) // And change every value to the index of the List
); // End of loop (no body)
} // End of method
Test code:
import java.util.Arrays;
class M{
static void c(int[]a,java.util.List b){for(int i=0;i<a.length;a[i]=b.indexOf(a[i++]));}
public static void main(String[] a){
int[] x = new int[]{ 5, 3, 4, 1 };
c(x, Arrays.asList(6, 8, 4, 1, 2, 5, 3, 100));
System.out.println(Arrays.toString(x));
x = new int[]{ 5, 3, 4, 9, 7, 5, 7 };
c(x, Arrays.asList(3, 4, 5, 7, 9));
System.out.println(Arrays.toString(x));
x = new int[]{ 1, 2, 3, 4, 5, 6 };
c(x, Arrays.asList(1, 2, 3, 4, 5, 6));
System.out.println(Arrays.toString(x));
x = new int[]{ 16, 27, 18, 12, 6, 26, 11, 24, 26, 20, 2, 8, 7, 12, 5, 22, 22, 2, 17, 4 };
c(x, Arrays.asList(15, 18, 11, 16, 14, 20, 37, 38, 6, 36, 8, 32, 21, 2, 31, 22, 33, 4, 1, 35, 3, 25, 9, 30, 26, 39, 5, 23, 29, 10, 13, 12, 7, 19, 24, 17, 34, 27, 40, 28));
System.out.println(Arrays.toString(x));
x = new int[]{ 54 };
c(x, Arrays.asList(54));
System.out.println(Arrays.toString(x));
}
}
Output:
[5, 6, 2, 3]
[2, 0, 1, 4, 3, 2, 3]
[0, 1, 2, 3, 4, 5]
[3, 37, 1, 31, 8, 24, 2, 34, 24, 5, 13, 10, 32, 31, 26, 15, 15, 13, 35, 17]
[0]
Bash + coreutils, 51
for n in $1
do grep -wn $n <<<"$2"
done|cut -d: -f1
Previous answer:
s()(sort -$1k2)
nl|s|join -j2 - <(nl<<<"$1"|s)|s n|cut -d\ -f3
Haskell, 32 bytes
a%b=[length$fst$span(/=x)b|x<-a]
Try it online! One-indexed.
Other attempts:
q(h:t)x|x==h=0|1>0=1+q t x;map.q
f b=map$length.fst.($b).span.(/=)
a%b=[until((==x).(b!!))(+1)0|x<-a]
a%b=[until(\y->x==b!!y)(+1)0|x<-a]
import Data.List;map.flip elemIndex
C#, 32 Bytes
(n,m)=>n.Select(i=>m.IndexOf(i))
This is the code as a lambda expression, so it should be valid.
The solution is with a 0 based index. I think it's pretty straigt forward how it works - it simply takes the items of n and selects the indices of the items in m.
Retina, 32 31 30 bytes
1 byte saved thanks to Kritixi Lithos and 1 byte thanks to Martin Ender
(\d+)(?=.*¶(\d+ )*\1 )
$#2
G1`
Uses 0-indexing. Input has a trailing space on each line.
Explanation
(\d+)(?=.*¶(\d+ )*\1 )
$#2
Here we replace every number on the first line by the number of numbers before the same number on the second line.
G1`
Then, we delete the second line, leaving only the new first line as the output.
Haskell, 34 bytes
n#m=[i|a<-n,(i,e)<-zip[1..]m,e==a]
Usage example: [5,3,4,9,7,5,7] # [3,4,5,7,9] -> [3,1,2,5,4,3,4]
The built-in elemIndex is in Data.List and therefore longer than the version above. The outer loop goes through n and the inner loop through pairs of (i,e) where i is the index of e in m. Keep the i where e equals the current element of n.
Haskell, 43 bytes
a*b=[[fst x|x<-zip[0..]b,y==snd x]!!0|y<-a]
a*b= -- define function * with 2 args
[ |y<-a] -- for each elt in first arg
zip[0..]b -- match elts in second arg w/ idxs
-- [a,b,c] -> [[0,a],[1,b],[2,c]]
[fst x|x<- ] -- take first element in each pair
,y==snd x -- if the index matches
!!0 -- first element (always only 1)
MATL, 2 bytes
&m
This uses 1-indexing. Try it online!
Explanation
The meta-function & indicates that the next function will use a (function-specific) secondary default in/out specification. For function m (ismember), & specifies that its second output will be produced. This contains the index of (the first occurrence of) each entry of the first input in the second input.
Java, 104 81 bytes
1 byte thanks to Business cat.
void f(int[]a,int[]b){for(int i=0,j=0;i<b.length;)j=a[j++]==b[i]?0*(b[i++]=j):j;}
V, 26 bytes
jòdf kÄ/-
DÓÓ
ÒC1@"Gòdk
This is a very strange and hacky solution, because V has little to no concept of numbers. Input comes in this format:
6 8 4 1 2 5 3 100
5 3 4 1
With a trailing space on each line.
Hexdump:
00000000: 6af2 6466 206b c42f 122d 0a44 d3d3 0ad2 j.df k./.-.D....
00000010: 0143 311b 4022 47f2 646b .C1.@"G.dk
Explanation:
j " Move down one line (to N) (1)
ò " Recursively:
df " (d)elete until you (f)ind a space. This will be saved into
" register '-' (2)
k " Move up one line (to M)
Ä " Duplicate line M (3)
/<C-r>- " Move the cursor forward until the next occurence of register '-'
" (the number we deleted from N)
" (4)
D " Delete every character *after* the cursor (5)
ÓÓ " Remove everything on this line except for whitespace
Ò<C-a> " Replace every character on this line with `<C-a>`, which is the
" command for incrementing a number (6)
C " Delete this line into register '"', and enter insert mode
1<esc> " Enter a '1' and return to normal mode
@" " Run register '"' as V code (7)
G " Go to the last line (1)
ò " End recursion
dk " Delete the last two lines (m and n)
If this doesn't make it clearer, here are examples of the buffer during the various stages the loop goes through:
Stage 1 (| is the cursor)
6 8 4 1 2 5 3 100
|5 3 4 1
Stage 2:
6 8 4 1 2 5 3 100
|3 4 1
Stage 3:
|6 8 4 1 2 5 3 100
6 8 4 1 2 5 3 100
3 4 1
Stage 4:
6 8 4 1 2 |5 3 100
6 8 4 1 2 5 3 100
3 4 1
Stage 5:
6 8 4 1 2 |
6 8 4 1 2 5 3 100
3 4 1
Stage 6:
|<C-a><C-a><C-a><C-a><C-a>
6 8 4 1 2 5 3 100
3 4 1
Stage 7:
|6
6 8 4 1 2 5 3 100
3 4 1
Back to stage 1:
6
6 8 4 1 2 5 3 100
|3 4 1
Japt, 4 bytes
m!bV
Explanation
There's not much to explain here, but it shows off an interesting feature of Japt. Normally, you would pass a function to m, like so:
mX{VbX}
This is basically U.map(X => V.indexOf(X)) (the U is implicit). However, when you're just performing one operation between two values (b here, on V and X), you can just give the operator and the other value and Japt will make a function out of it. This means mX{X+2} can be golfed to m+2.
However, this doesn't work when the values are in the wrong order (mbV would be short for mX{XbV}). To get around this, you can prepend an exclamation mark to the operator, which tells Japt to swap the operands. This costs an extra byte, but it's still a couple bytes shorter than the alternative. And now you know a little more about Japt.
k, 1
This is a built-in operator in k and uses zero-based indexing.
?
Example:
k)6 8 4 1 2 5 3 100 ? 5 3 4 1
5 6 2 3
Perl 6, 31 bytes
->\n,\m{n.map:{m.first($_,:k)}}
Expanded:
-> \n, \m { # pointy block lambda
n.map: { # map over the values in 「n」
m.first( $_, :k ) # return the key 「:k」 of the first occurrence
}
}
0 indexed
CJam, 4 bytes
{f#}
Anonymous block that expects arguments on the stack and leaves the result on the stack.
Uses 0-indexing.
JavaScript (ES6), 28 bytes
Takes the arrays in currying syntax (n)(m). 0-indexed.
let f =
n=>m=>n.map(v=>m.indexOf(v))
console.log(JSON.stringify(f([5,3,4,1])([6,8,4,1,2,5,3,100])))
console.log(JSON.stringify(f([5,3,4,9,7,5,7])([3,4,5,7,9])))
console.log(JSON.stringify(f([1,2,3,4,5,6])([1,2,3,4,5,6])))
console.log(JSON.stringify(f([16,27,18,12,6,26,11,24,26,20,2,8,7,12,5,22,22,2,17,4])([15,18,11,16,14,20,37,38,6,36,8,32,21,2,31,22,33,4,1,35,3,25,9,30,26,39,5,23,29,10,13,12,7,19,24,17,34,27,40,28])))
console.log(JSON.stringify(f([54])([54])))
Mathematica, 25 bytes
#&@@@PositionIndex@#/@#2&
Takes two inputs m and n, and returns the 1-based indices of n in m.
J, 2 bytes
i.
This is not a complete program, but a built-in function.
Use it as such:
echo 6 8 4 1 2 5 3 100 i. 5 3 4 1
Note that this uses 0-indexing.
Jelly, 3 bytes
iЀ
Specs
- Input: two arguments, the first being m, and the second being n.
- Output: one array
05AB1E, 5 bytes
v²yk,
v # For each value in n (call it y)
² # Push m
y # Push y
k, # Print the 0-indexed index of y in m