|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +=pod |
| 4 | +
|
| 5 | +-------------------------------------------------------------------------------------------------------------- |
| 6 | +TITLE AND ATTRIBUTION: |
| 7 | +Solutions in Perl for The Weekly Challenge 264-2, |
| 8 | +written by Robbie Hatley on Mon Apr 08, 2024. |
| 9 | +
|
| 10 | +-------------------------------------------------------------------------------------------------------------- |
| 11 | +PROBLEM DESCRIPTION: |
| 12 | +Task 264-2: Target Array |
| 13 | +Submitted by: Mohammad Sajid Anwar |
| 14 | +You are given two arrays of integers, @source and @indices. |
| 15 | +The @indices can only contains integers 0 <= i < size of @source. |
| 16 | +Write a script to create target array by inserting at index |
| 17 | +$indices[i] the value $source[i]. |
| 18 | +
|
| 19 | +Example 1: |
| 20 | +Input: @source = (0, 1, 2, 3, 4) |
| 21 | + @indices = (0, 1, 2, 2, 1) |
| 22 | +Output: (0, 4, 1, 3, 2) |
| 23 | +@source @indices @target |
| 24 | +0 0 (0) |
| 25 | +1 1 (0, 1) |
| 26 | +2 2 (0, 1, 2) |
| 27 | +3 2 (0, 1, 3, 2) |
| 28 | +4 1 (0, 4, 1, 3, 2) |
| 29 | +
|
| 30 | +Example 2: |
| 31 | +Input: @source = (1, 2, 3, 4, 0) |
| 32 | + @indices = (0, 1, 2, 3, 0) |
| 33 | +Output: (0, 1, 2, 3, 4) |
| 34 | +@source @indices @target |
| 35 | +1 0 (1) |
| 36 | +2 1 (1, 2) |
| 37 | +3 2 (1, 2, 3) |
| 38 | +4 3 (1, 2, 3, 4) |
| 39 | +0 0 (0, 1, 2, 3, 4) |
| 40 | +
|
| 41 | +Example 3: |
| 42 | +Input: @source = (1) |
| 43 | + @indices = (0) |
| 44 | +Output: (1) |
| 45 | +
|
| 46 | +-------------------------------------------------------------------------------------------------------------- |
| 47 | +PROBLEM NOTES: |
| 48 | +This is one of those few Weekly Challenges where Task 2 is simpler than Task 1 (usually it's the other way |
| 49 | +around). I'll solve this problem by using Perl's built-in "splice" feature to "splice" desired elements from |
| 50 | +the "source" array into the target array being constructed, using the indices from the "indices" array to |
| 51 | +specify insertion points. |
| 52 | +
|
| 53 | +-------------------------------------------------------------------------------------------------------------- |
| 54 | +IO NOTES: |
| 55 | +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a |
| 56 | +single-quoted array of arrays of two arrays of integers, with the second array of each inner pair being |
| 57 | +insertion indices for the first array, in proper Perl syntax, like so: |
| 58 | +./ch-2.pl '([[18,32,74],[0,0,0,]],[[18,32,74],[0,1,2]])' |
| 59 | +
|
| 60 | +Output is to STDOUT and will be each input followed by the corresponding output. |
| 61 | +
|
| 62 | +=cut |
| 63 | + |
| 64 | +# ------------------------------------------------------------------------------------------------------------ |
| 65 | +# PRAGMAS, MODULES, AND SUBS: |
| 66 | + |
| 67 | +use v5.36; |
| 68 | +use strict; |
| 69 | +use warnings; |
| 70 | +sub target_array ($s, $i) { |
| 71 | + my @t = (); |
| 72 | + for ( my $ii=0 ; $ii <= $#$s ; ++$ii ) { |
| 73 | + splice @t, $$i[$ii], 0, $$s[$ii]; |
| 74 | + } |
| 75 | + return @t; |
| 76 | +} |
| 77 | + |
| 78 | +# ------------------------------------------------------------------------------------------------------------ |
| 79 | +# INPUTS: |
| 80 | +my @arrays = @ARGV ? eval($ARGV[0]) : |
| 81 | +( |
| 82 | + # Example 1 Input: |
| 83 | + [ |
| 84 | + [0, 1, 2, 3, 4], |
| 85 | + [0, 1, 2, 2, 1], |
| 86 | + ], |
| 87 | + # Expected Output: (0, 4, 1, 3, 2) |
| 88 | + |
| 89 | + # Example 2 Input: |
| 90 | + [ |
| 91 | + [1, 2, 3, 4, 0], |
| 92 | + [0, 1, 2, 3, 0], |
| 93 | + ], |
| 94 | + # Expected Output: (0, 1, 2, 3, 4) |
| 95 | + |
| 96 | + # Example 3 Input: |
| 97 | + [ |
| 98 | + [1], |
| 99 | + [0], |
| 100 | + ], |
| 101 | + # Expected Output: (1) |
| 102 | +); |
| 103 | + |
| 104 | +# ------------------------------------------------------------------------------------------------------------ |
| 105 | +# MAIN BODY OF PROGRAM: |
| 106 | +for my $aref (@arrays) { |
| 107 | + say ''; |
| 108 | + say 'Source array = (', join(', ', @{$aref->[0]}), ')'; |
| 109 | + say 'Indices array = (', join(', ', @{$aref->[1]}), ')'; |
| 110 | + my @target = target_array($aref->[0], $aref->[1]); |
| 111 | + say 'Target array = (', join(', ', @target ), ')'; |
| 112 | +} |
0 commit comments