/
ch-2.pl
executable file
·68 lines (60 loc) · 1.48 KB
/
ch-2.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env perl
#
# You are given two arrays of integers, @source and @indices. The @indices can
# only contains integers 0 <= i < size of @source.
#
# Write a script to create target array by insert at index $indices[i] the
# value $source[i].
# Example 1
#
# Input: @source = (0, 1, 2, 3, 4)
# @indices = (0, 1, 2, 2, 1)
# Output: (0, 4, 1, 3, 2)
#
# @source @indices @target
# 0 0 (0)
# 1 1 (0, 1)
# 2 2 (0, 1, 2)
# 3 2 (0, 1, 3, 2)
# 4 1 (0, 4, 1, 3, 2)
#
# Example 2
# Input: @source = (1, 2, 3, 4, 0)
# @indices = (0, 1, 2, 3, 0)
# Output: (0, 1, 2, 3, 4)
#
# @source @indices @target
# 1 0 (1)
# 2 1 (1, 2)
# 3 2 (1, 2, 3)
# 4 3 (1, 2, 3, 4)
# 0 0 (0, 1, 2, 3, 4)
#
# Example 3
#
# Input: @source = (1)
# @indices = (0)
# Output: (1)
#
use strict;
use warnings;
use Test2::V0 -no_srand => 1;
use Data::Dumper;
my $cases = [
[[[0, 1, 2, 3, 4], [0, 1, 2, 2, 1]], [0, 4, 1, 3, 2], 'Example 1'],
[[[1, 2, 3, 4, 0], [0, 1, 2, 3, 0]], [0, 1, 2, 3, 4], 'Example 2'],
[[[1], [0]], [1], 'Example 3'],
];
sub target_array
{
my $sources = $_[0]->[0];
my $indices = $_[0]->[1];
my @res;
splice @res, $indices->[$_], 0, $sources->[$_] for 0..$#$sources;
return \@res;
}
for (@$cases) {
is(target_array($_->[0]), $_->[1], $_->[2]);
}
done_testing();
exit 0;