The weekly challenge 264 - Task 2: Target Array

 1 #!/usr/bin/env perl
 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-264/#TASK2
 3 #
 4 # Task 2: Target Array
 5 # ====================
 6 #
 7 # You are given two arrays of integers, @source and @indices. The @indices can
 8 # only contains integers 0 <= i < size of @source.
 9 #
10 # Write a script to create target array by insert at index $indices[i] the
11 # value $source[i].
12 #
13 ## Example 1
14 ##
15 ## Input: @source  = (0, 1, 2, 3, 4)
16 ##        @indices = (0, 1, 2, 2, 1)
17 ## Output: (0, 4, 1, 3, 2)
18 ##
19 ## @source  @indices  @target
20 ## 0        0         (0)
21 ## 1        1         (0, 1)
22 ## 2        2         (0, 1, 2)
23 ## 3        2         (0, 1, 3, 2)
24 ## 4        1         (0, 4, 1, 3, 2)
25 #
26 ## Example 2
27 ##
28 ## Input: @source  = (1, 2, 3, 4, 0)
29 ##        @indices = (0, 1, 2, 3, 0)
30 ## Output: (0, 1, 2, 3, 4)
31 ##
32 ## @source  @indices  @target
33 ## 1        0         (1)
34 ## 2        1         (1, 2)
35 ## 3        2         (1, 2, 3)
36 ## 4        3         (1, 2, 3, 4)
37 ## 0        0         (0, 1, 2, 3, 4)
38 #
39 ## Example 3
40 ##
41 ## Input: @source  = (1)
42 ##        @indices = (0)
43 ## Output: (1)
44 #
45 ############################################################
46 ##
47 ## discussion
48 ##
49 ############################################################
50 #
51 # Walk the arrays in parallel using an index $i; keep all
52 # elements < $indices[$i] in your target array, and move
53 # any elements >= $indices[$i] up by one element, creating
54 # space for $source->[$i].
55 # By slicing properly, we can do all of that in a single step.
56 
57 use strict;
58 use warnings;
59 
60 target_array( [0, 1, 2, 3, 4], [0, 1, 2, 2, 1] );
61 target_array( [1, 2, 3, 4, 0], [0, 1, 2, 3, 0] );
62 target_array( [1], [0] );
63 
64 sub target_array {
65    my ($source, $indices) = @_;
66    print "Input: (", join(", ", @$source), "), (", join(", ", @$indices), ")\n";
67    my @target = ();
68    foreach my $i (0..scalar(@$indices)-1) {
69       my $j = $indices->[$i];
70       @target = (@target[0..$j-1], $source->[$i], @target[$j..scalar(@target)-1]);
71    }
72    print "Output: (", join(", ", @target), ")\n";
73 }