diff options
| -rw-r--r-- | challenge-217/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-217/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-217/jeanluc2020/perl/ch-1.pl | 69 | ||||
| -rwxr-xr-x | challenge-217/jeanluc2020/perl/ch-2.pl | 74 |
4 files changed, 145 insertions, 0 deletions
diff --git a/challenge-217/jeanluc2020/blog-1.txt b/challenge-217/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..6d650ff709 --- /dev/null +++ b/challenge-217/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-217-1.html diff --git a/challenge-217/jeanluc2020/blog-2.txt b/challenge-217/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..8505a12af1 --- /dev/null +++ b/challenge-217/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-217-2.html diff --git a/challenge-217/jeanluc2020/perl/ch-1.pl b/challenge-217/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..eb33b2b768 --- /dev/null +++ b/challenge-217/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,69 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-217/#TASK1 +# +# Task 1: Sorted Matrix +# ===================== +# +# You are given a n x n matrix where n >= 2. +# +# Write a script to find 3rd smallest element in the sorted matrix. +# +## Example 1 +## +## Input: @matrix = ([3, 1, 2], [5, 2, 4], [0, 1, 3]) +## Output: 1 +## +## The sorted list of the given matrix: 0, 1, 1, 2, 2, 3, 3, 4, 5. +## The 3rd smallest of the sorted list is 1. +# +## Example 2 +## +## Input: @matrix = ([2, 1], [4, 5]) +## Output: 4 +## +## The sorted list of the given matrix: 1, 2, 4, 5. +## The 3rd smallest of the sorted list is 4. +# +## Example 3 +## +## Input: @matrix = ([1, 0, 3], [0, 0, 0], [1, 2, 1]) +## Output: 0 +## +## The sorted list of the given matrix: 0, 0, 0, 0, 1, 1, 1, 2, 3. +## The 3rd smallest of the sorted list is 0. +# +############################################################ +## +## discussion +## +############################################################ +# +# This is straight forward. First, we create an array that +# consists of all elements from the matrix - @all in our case, +# which we fill while printing the input matrix. +# Then, we sort that. From the sorted array, we print the third +# element as the output. + +use strict; +use warnings; + +sorted_matrix([3, 1, 2], [5, 2, 4], [0, 1, 3]); +sorted_matrix([2, 1], [4, 5]); +sorted_matrix([1, 0, 3], [0, 0, 0], [1, 2, 1]); + +sub sorted_matrix { + my @matrix = @_; + print "Input: ("; + my @all = (); + my $first = 1; + foreach my $part (@matrix) { + print ", " unless $first; + $first = 0; + print "[" . join(",",@$part) . "]"; + push @all, @$part; + } + print ")\n"; + my @sorted = sort {$a<=>$b} @all; + print "Output: $sorted[2]\n"; +} + diff --git a/challenge-217/jeanluc2020/perl/ch-2.pl b/challenge-217/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..12dc8a986e --- /dev/null +++ b/challenge-217/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,74 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-217/#TASK2 +# +# Task 2: Max Number +# ================== +# +# You are given a list of positive integers. +# +# Write a script to concatenate the integers to form the highest possible value. +# +## Example 1: +## +## Input: @list = (1, 23) +## Output: 231 +# +## Example 2: +## +## Input: @list = (10, 3, 2) +## Output: 3210 +# +## Example 3: +## +## Input: @list = (31, 2, 4, 10) +## Output: 431210 +# +## Example 4: +## +## Input: @list = (5, 11, 4, 1, 2) +## Output: 542111 +# +## Example 5: +## +## Input: @list = (1, 10) +## Output: 110 +# +############################################################ +## +## discussion +## +############################################################ +# +# We simply create all possible permutations of the input list. +# Each permutation represents one possible value, so we just +# pick the highest value. + +use strict; +use warnings; + +max_number(1, 23); +max_number(10, 3, 2); +max_number(31, 2, 4, 10); +max_number(5, 11, 4, 1, 2); +max_number(1, 10); + +sub max_number { + my @list = @_; + print "Input: (" . join(", ", @list) . ")\n"; + print "Output: " . get_max(@list) . "\n"; +} + +sub get_max { + my @list = @_; + return "" unless @list; + my $max = 0; + foreach my $index (0..$#list) { + my @rest = (); + @rest = @list[0..$index-1] if $index > 0; + push @rest, @list[$index+1..$#list] if $index < $#list; + my $current = $list[$index] . get_max(@rest); + $max = $current if $current > $max; + } + return $max; +} + |
