diff options
| author | Thomas Köhler <jean-luc@picard.franken.de> | 2024-01-08 11:31:26 +0100 |
|---|---|---|
| committer | Thomas Köhler <jean-luc@picard.franken.de> | 2024-01-08 11:31:26 +0100 |
| commit | 2f0103fc32a161b83ebfbc34b7ccce5d26200d45 (patch) | |
| tree | 723fcff5d3faa4b40a2415946071da2cd10a0d7f /challenge-251 | |
| parent | 9a485c9bac8e3887b165d67c9aa81d71cdd42f01 (diff) | |
| download | perlweeklychallenge-club-2f0103fc32a161b83ebfbc34b7ccce5d26200d45.tar.gz perlweeklychallenge-club-2f0103fc32a161b83ebfbc34b7ccce5d26200d45.tar.bz2 perlweeklychallenge-club-2f0103fc32a161b83ebfbc34b7ccce5d26200d45.zip | |
Add solution 251
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
Diffstat (limited to 'challenge-251')
| -rw-r--r-- | challenge-251/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-251/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-251/jeanluc2020/perl/ch-1.pl | 95 | ||||
| -rwxr-xr-x | challenge-251/jeanluc2020/perl/ch-2.pl | 95 | ||||
| -rwxr-xr-x | challenge-251/jeanluc2020/python/ch-1.py | 87 | ||||
| -rwxr-xr-x | challenge-251/jeanluc2020/python/ch-2.py | 82 |
6 files changed, 361 insertions, 0 deletions
diff --git a/challenge-251/jeanluc2020/blog-1.txt b/challenge-251/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..26c059d208 --- /dev/null +++ b/challenge-251/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-251-1.html diff --git a/challenge-251/jeanluc2020/blog-2.txt b/challenge-251/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..9816093212 --- /dev/null +++ b/challenge-251/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-251-2.html diff --git a/challenge-251/jeanluc2020/perl/ch-1.pl b/challenge-251/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..64648b97c2 --- /dev/null +++ b/challenge-251/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,95 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-251/#TASK1 +# +# Task 1: Concatenation Value +# =========================== +# +# You are given an array of integers, @ints. +# +# Write a script to find the concatenation value of the given array. +# +# The concatenation of two numbers is the number formed by concatenating their +# numerals. +# +## For example, the concatenation of 10, 21 is 1021. +## The concatenation value of @ints is initially equal to 0. +## Perform this operation until @ints becomes empty: +## +## If there exists more than one number in @ints, pick the first element +## and last element in @ints respectively and add the value of their +## concatenation to the concatenation value of @ints, then delete the +## first and last element from @ints. +## +## If one element exists, add its value to the concatenation value of +## @ints, then delete it. +# +## Example 1 +## +## Input: @ints = (6, 12, 25, 1) +## Output: 1286 +## +## 1st operation: concatenation of 6 and 1 is 61 +## 2nd operation: concaternation of 12 and 25 is 1225 +## +## Concatenation Value => 61 + 1225 => 1286 +# +## Example 2 +## +## Input: @ints = (10, 7, 31, 5, 2, 2) +## Output: 489 +## +## 1st operation: concatenation of 10 and 2 is 102 +## 2nd operation: concatenation of 7 and 2 is 72 +## 3rd operation: concatenation of 31 and 5 is 315 +## +## Concatenation Value => 102 + 72 + 315 => 489 +# +## Example 3 +## +## Input: @ints = (1, 2, 10) +## Output: 112 +## +## 1st operation: concatenation of 1 and 10 is 110 +## 2nd operation: only element left is 2 +## +## Concatenation Value => 110 + 2 => 112 +# +############################################################ +## +## discussion +## +############################################################ +# +# Starting with a sum of 0, we call a function that just concatenates +# the first and last elements of @ints and adds that number to the +# current sum, then we call the function recursively with the new +# current sum and the remainder of the array. Of course handle the +# case where there is only one element in the array by returning the +# current sum + the last element, and the case of an empty array by +# returning the current sum. + +use strict; +use warnings; + +concatenation_value(6, 12, 25, 1); +concatenation_value(10, 7, 31, 5, 2, 2); +concatenation_value(1, 2, 10); + +sub concatenation_value { + my @ints = @_; + print "Input: (" . join(", ", @ints) . ")\n"; + my $result = concatenation_value_(0, @ints); + print "Output: $result\n"; +} + +sub concatenation_value_ { + my ($current, @ints) = @_; + return $current unless @ints; + if(@ints >= 2) { + my $first = shift @ints; + my $last = pop @ints; + return concatenation_value_($current + "$first$last", @ints); + } else { + return $current + $ints[0]; + } +} diff --git a/challenge-251/jeanluc2020/perl/ch-2.pl b/challenge-251/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..6c41933c38 --- /dev/null +++ b/challenge-251/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,95 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-251/#TASK2 +# +# Task 2: Lucky Numbers +# ===================== +# +# You are given a m x n matrix of distinct numbers. +# +# Write a script to return the lucky number, if there is one, or -1 if not. +# +## A lucky number is an element of the matrix such that it is +## the minimum element in its row and maximum in its column. +# +## Example 1 +## +## Input: $matrix = [ [ 3, 7, 8], +## [ 9, 11, 13], +## [15, 16, 17] ]; +## Output: 15 +## +## 15 is the only lucky number since it is the minimum in its row +## and the maximum in its column. +# +## Example 2 +## +## Input: $matrix = [ [ 1, 10, 4, 2], +## [ 9, 3, 8, 7], +## [15, 16, 17, 12] ]; +## Output: 12 +# +## Example 3 +## +## Input: $matrix = [ [7 ,8], +## [1 ,2] ]; +## Output: 7 +# +############################################################ +## +## discussion +## +############################################################ +# +# For a lucky number, we know: +# Since the minimum in a row is at the same time the maximum in +# a column, there is no number bigger in that column, so in all +# other rows, there is a number that is smaller than the current +# number, so the minimum in that other row has to be smaller or +# equal to this number; being smaller, it can't be bigger than any +# of the numbers in the original row, so it's can't be a lucky number +# as well. +# So we know we have at most one lucky number. +# Finding it is simple: In each row, find the minimum number, then +# check whether it is as well the maximum number in its column. + +use strict; +use warnings; +use List::Util qw(min max); + +lucky_numbers([ [ 3, 7, 8], [ 9, 11, 13], [15, 16, 17] ]); +lucky_numbers([ [ 1, 10, 4, 2], [ 9, 3, 8, 7], [15, 16, 17, 12] ]); +lucky_numbers([ [7 ,8], [1 ,2] ]); +lucky_numbers([ [7 ,8], [10 ,2] ]); + +sub lucky_numbers { + my $matrix = shift; + print "Input: [\n"; + foreach my $row (@$matrix) { + print " [" . join(", ", @$row) . "],\n"; + } + print " ];\n"; + my $rows = scalar(@$matrix); + my $columns = scalar(@{$matrix->[0]}); + my $row_min = []; + my $column_max = []; + foreach my $column (0..$columns-1) { + my @column_elements = (); + foreach my $row (0..$rows-1) { + push @column_elements, $matrix->[$row]->[$column]; + } + push @$column_max, max(@column_elements); + } + foreach my $i (0..$rows-1) { + push @$row_min, min(@{$matrix->[$i]}); + foreach my $j (0..$columns-1) { + my $elem = $matrix->[$i]->[$j]; + if($elem == $row_min->[$i] && $elem == $column_max->[$j]) { + print "Output: $elem\n"; + return; + } + } + } + print "Output: -1\n"; +} + + diff --git a/challenge-251/jeanluc2020/python/ch-1.py b/challenge-251/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..9f0b248134 --- /dev/null +++ b/challenge-251/jeanluc2020/python/ch-1.py @@ -0,0 +1,87 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-251/#TASK1 +# +# Task 1: Concatenation Value +# =========================== +# +# You are given an array of integers, @ints. +# +# Write a script to find the concatenation value of the given array. +# +# The concatenation of two numbers is the number formed by concatenating their +# numerals. +# +## For example, the concatenation of 10, 21 is 1021. +## The concatenation value of @ints is initially equal to 0. +## Perform this operation until @ints becomes empty: +## +## If there exists more than one number in @ints, pick the first element +## and last element in @ints respectively and add the value of their +## concatenation to the concatenation value of @ints, then delete the +## first and last element from @ints. +## +## If one element exists, add its value to the concatenation value of +## @ints, then delete it. +# +## Example 1 +## +## Input: @ints = (6, 12, 25, 1) +## Output: 1286 +## +## 1st operation: concatenation of 6 and 1 is 61 +## 2nd operation: concaternation of 12 and 25 is 1225 +## +## Concatenation Value => 61 + 1225 => 1286 +# +## Example 2 +## +## Input: @ints = (10, 7, 31, 5, 2, 2) +## Output: 489 +## +## 1st operation: concatenation of 10 and 2 is 102 +## 2nd operation: concatenation of 7 and 2 is 72 +## 3rd operation: concatenation of 31 and 5 is 315 +## +## Concatenation Value => 102 + 72 + 315 => 489 +# +## Example 3 +## +## Input: @ints = (1, 2, 10) +## Output: 112 +## +## 1st operation: concatenation of 1 and 10 is 110 +## 2nd operation: only element left is 2 +## +## Concatenation Value => 110 + 2 => 112 +# +############################################################ +## +## discussion +## +############################################################ +# +# Starting with a sum of 0, we call a function that just concatenates +# the first and last elements of @ints and adds that number to the +# current sum, then we call the function recursively with the new +# current sum and the remainder of the array. Of course handle the +# case where there is only one element in the array by returning the +# current sum + the last element, and the case of an empty array by +# returning the current sum. + +def concatenation_value_(current: int, ints: list) -> int: + if len(ints) == 0: + return current + if len(ints) == 1: + return current + ints[0] + value = str(ints[0]) + str(ints[-1]) + return concatenation_value_(current + int(value), ints[1:-1]) + +def concatenation_value(ints: list) -> None: + print("Input: (", ", ".join([str(x) for x in ints]), ")") + result = concatenation_value_(0, ints) + print(f"Output: {result}") + +concatenation_value([6, 12, 25, 1]) +concatenation_value([10, 7, 31, 5, 2, 2]) +concatenation_value([1, 2, 10]) + diff --git a/challenge-251/jeanluc2020/python/ch-2.py b/challenge-251/jeanluc2020/python/ch-2.py new file mode 100755 index 0000000000..33dd88684d --- /dev/null +++ b/challenge-251/jeanluc2020/python/ch-2.py @@ -0,0 +1,82 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-251/#TASK2 +# +# Task 2: Lucky Numbers +# ===================== +# +# You are given a m x n matrix of distinct numbers. +# +# Write a script to return the lucky number, if there is one, or -1 if not. +# +## A lucky number is an element of the matrix such that it is +## the minimum element in its row and maximum in its column. +# +## Example 1 +## +## Input: $matrix = [ [ 3, 7, 8], +## [ 9, 11, 13], +## [15, 16, 17] ]; +## Output: 15 +## +## 15 is the only lucky number since it is the minimum in its row +## and the maximum in its column. +# +## Example 2 +## +## Input: $matrix = [ [ 1, 10, 4, 2], +## [ 9, 3, 8, 7], +## [15, 16, 17, 12] ]; +## Output: 12 +# +## Example 3 +## +## Input: $matrix = [ [7 ,8], +## [1 ,2] ]; +## Output: 7 +# +############################################################ +## +## discussion +## +############################################################ +# +# For a lucky number, we know: +# Since the minimum in a row is at the same time the maximum in +# a column, there is no number bigger in that column, so in all +# other rows, there is a number that is smaller than the current +# number, so the minimum in that other row has to be smaller or +# equal to this number; being smaller, it can't be bigger than any +# of the numbers in the original row, so it's can't be a lucky number +# as well. +# So we know we have at most one lucky number. +# Finding it is simple: In each row, find the minimum number, then +# check whether it is as well the maximum number in its column. + +def lucky_numbers(matrix: list) -> None: + print("Input: ["); + for row in matrix: + print(" [", ", ".join([str(x) for x in row]), "],") + print(" ];") + rows = len(matrix) + columns = len(matrix[0]) + row_min = [] + column_max = [] + for column in range(columns): + column_elements = [] + for row in range(rows): + column_elements.append(matrix[row][column]) + column_max.append(max(column_elements)) + for i in range(rows): + row_min.append(min(matrix[i])) + for j in range(columns): + elem = matrix[i][j] + if elem == row_min[i] and elem == column_max[j]: + print(f"Output: {elem}") + return + print("Output: -1") + +lucky_numbers([ [ 3, 7, 8], [ 9, 11, 13], [15, 16, 17] ]) +lucky_numbers([ [ 1, 10, 4, 2], [ 9, 3, 8, 7], [15, 16, 17, 12] ]) +lucky_numbers([ [7 ,8], [1 ,2] ]) +lucky_numbers([ [7 ,8], [10 ,2] ]) + |
