diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-28 15:23:33 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-28 15:23:33 +0100 |
| commit | 1478301bf14e05ce900cb87cbefb8405a123e0ab (patch) | |
| tree | 50c43292223314803394787ced0e4c43baf597d3 /challenge-268 | |
| parent | dfa08da69b588157ee30f7ad572646552a1b65b8 (diff) | |
| download | perlweeklychallenge-club-1478301bf14e05ce900cb87cbefb8405a123e0ab.tar.gz perlweeklychallenge-club-1478301bf14e05ce900cb87cbefb8405a123e0ab.tar.bz2 perlweeklychallenge-club-1478301bf14e05ce900cb87cbefb8405a123e0ab.zip | |
Add Perl solution to challenge 268
Diffstat (limited to 'challenge-268')
| -rw-r--r-- | challenge-268/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-268/paulo-custodio/perl/ch-1.pl | 52 | ||||
| -rw-r--r-- | challenge-268/paulo-custodio/perl/ch-2.pl | 44 | ||||
| -rw-r--r-- | challenge-268/paulo-custodio/t/test-1.yaml | 25 | ||||
| -rw-r--r-- | challenge-268/paulo-custodio/t/test-2.yaml | 15 |
5 files changed, 138 insertions, 0 deletions
diff --git a/challenge-268/paulo-custodio/Makefile b/challenge-268/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-268/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-268/paulo-custodio/perl/ch-1.pl b/challenge-268/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..71802c3800 --- /dev/null +++ b/challenge-268/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,52 @@ +#!/usr/bin/env perl + +# Challenge 268 +# +# Input: @x = (3, 7, 5) +# @y = (9, 5, 7) +# Output: 2 +# +# The magic number is 2. +# @x = (3, 7, 5) +# + 2 2 2 +# @y = (5, 9, 7) +# +# Example 2 +# +# Input: @x = (1, 2, 1) +# @y = (5, 4, 4) +# Output: 3 +# +# The magic number is 3. +# @x = (1, 2, 1) +# + 3 3 3 +# @y = (5, 4, 4) +# +# Example 3 +# +# Input: @x = (2) +# @y = (5) +# Output: 3 + +use Modern::Perl; + +my($x, $y) = split /,/, "@ARGV"; +my @x = split ' ', $x; +my @y = split ' ', $y; + +say magic_number(\@x, \@y); + +sub magic_number { + my($x, $y) = @_; + + my @x = sort {$a <=> $b} @$x; + my @y = sort {$a <=> $b} @$y; + + return -1 if scalar(@x) != scalar(@y); + + my $delta = $y[0]-$x[0]; + for (0 .. $#x) { + return -1 if $y[$_]-$x[$_] != $delta; + } + return $delta; +} diff --git a/challenge-268/paulo-custodio/perl/ch-2.pl b/challenge-268/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..5072831943 --- /dev/null +++ b/challenge-268/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +# Challenge 268 +# +# Task 2: Number Game +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints, with even number of elements. +# +# Write a script to create a new array made up of elements of the given array. +# Pick the two smallest integers and add it to new array in decreasing order +# i.e. high to low. Keep doing until the given array is empty. +# Example 1 +# +# Input: @ints = (2, 5, 3, 4) +# Output: (3, 2, 5, 4) +# +# Round 1: we picked (2, 3) and push it to the new array (3, 2) +# Round 2: we picked the remaining (4, 5) and push it to the new array (5, 4) +# +# Example 2 +# +# Input: @ints = (9, 4, 1, 3, 6, 4, 6, 1) +# Output: (1, 1, 4, 3, 6, 4, 9, 6) +# +# Example 3 +# +# Input: @ints = (1, 2, 2, 3) +# Output: (2, 1, 3, 2) + +use Modern::Perl; + +say join " ", number_game(@ARGV); + +sub number_game { + my(@ints) = @_; + my @out; + while (@ints) { + @ints = sort {$a <=> $b} @ints; + push @out, $ints[1], $ints[0]; + splice @ints, 0, 2; + } + return @out; +} diff --git a/challenge-268/paulo-custodio/t/test-1.yaml b/challenge-268/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..dabdffb7a6 --- /dev/null +++ b/challenge-268/paulo-custodio/t/test-1.yaml @@ -0,0 +1,25 @@ +- setup: + cleanup: + args: 3 7 5 , 9 5 7 + input: + output: 2 +- setup: + cleanup: + args: 1 2 1 , 5 4 4 + input: + output: 3 +- setup: + cleanup: + args: 1 2 1 , 5 4 3 + input: + output: -1 +- setup: + cleanup: + args: 1 2 1 , 5 4 + input: + output: -1 +- setup: + cleanup: + args: 2 , 5 + input: + output: 3 diff --git a/challenge-268/paulo-custodio/t/test-2.yaml b/challenge-268/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..9cc8213279 --- /dev/null +++ b/challenge-268/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 2 5 3 4 + input: + output: 3 2 5 4 +- setup: + cleanup: + args: 9 4 1 3 6 4 6 1 + input: + output: 1 1 4 3 6 4 9 6 +- setup: + cleanup: + args: 1 2 2 3 + input: + output: 2 1 3 2 |
