diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-20 13:55:07 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-20 13:55:07 +0000 |
| commit | 11cce4eba274267942d772f04f2982854cea331f (patch) | |
| tree | 5e14456555ed418095e8083bdbecf76e8d44dc5d | |
| parent | c2a0f8f1647be50218e0f2032f126f97f8431dda (diff) | |
| parent | c5b1508dd8911da3bd69a1c5639b6de6f9d010f7 (diff) | |
| download | perlweeklychallenge-club-11cce4eba274267942d772f04f2982854cea331f.tar.gz perlweeklychallenge-club-11cce4eba274267942d772f04f2982854cea331f.tar.bz2 perlweeklychallenge-club-11cce4eba274267942d772f04f2982854cea331f.zip | |
Merge pull request #9611 from jeanluc2020/jeanluc-257
Add solution 257.
| -rw-r--r-- | challenge-257/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-257/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-257/jeanluc2020/perl/ch-1.pl | 69 | ||||
| -rwxr-xr-x | challenge-257/jeanluc2020/perl/ch-2.pl | 181 | ||||
| -rwxr-xr-x | challenge-257/jeanluc2020/python/ch-1.py | 64 | ||||
| -rwxr-xr-x | challenge-257/jeanluc2020/python/ch-2.py | 165 |
6 files changed, 481 insertions, 0 deletions
diff --git a/challenge-257/jeanluc2020/blog-1.txt b/challenge-257/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..1eebe583a4 --- /dev/null +++ b/challenge-257/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-257-1.html diff --git a/challenge-257/jeanluc2020/blog-2.txt b/challenge-257/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..c607d08810 --- /dev/null +++ b/challenge-257/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-257-2.html diff --git a/challenge-257/jeanluc2020/perl/ch-1.pl b/challenge-257/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..45c4ec4756 --- /dev/null +++ b/challenge-257/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,69 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-257/#TASK1 +# +# Task 1: Smaller than Current +# ============================ +# +# You are given a array of integers, @ints. +# +# Write a script to find out how many integers are smaller than current i.e. +# foreach ints[i], count ints[j] < ints[i] where i != j. +# +## Example 1 +## +## Input: @ints = (5, 2, 1, 6) +## Output: (2, 1, 0, 3) +## +## For $ints[0] = 5, there are two integers (2,1) smaller than 5. +## For $ints[1] = 2, there is one integer (1) smaller than 2. +## For $ints[2] = 1, there is none integer smaller than 1. +## For $ints[3] = 6, there are three integers (5,2,1) smaller than 6. +# +## Example 2 +## +## Input: @ints = (1, 2, 0, 3) +## Output: (1, 2, 0, 3) +# +## Example 3 +## +## Input: @ints = (0, 1) +## Output: (0, 1) +# +## Example 4 +## +## Input: @ints = (9, 4, 9, 2) +## Output: (2, 1, 2, 0) +# +############################################################ +## +## discussion +## +############################################################ +# +# Walk through the array, and for each element, walk through +# the whole array again, counting the elements that are smaller +# than the one in the outer loop. Put the result of this loop run +# at the end of the final result. +# +use strict; +use warnings; + +smaller_than_current(5, 2, 1, 6); +smaller_than_current(1, 2, 0, 3); +smaller_than_current(0, 1); +smaller_than_current(9, 4, 9, 2); + +sub smaller_than_current { + my @ints = @_; + print "Input: (" . join(", ", @ints) . ")\n"; + my @result = (); + foreach my $i (0..$#ints) { + my $count = 0; + foreach my $j (0..$#ints) { + next if $i == $j; + $count++ if $ints[$i] > $ints[$j]; + } + push @result, $count; + } + print "Output: (" . join(", ", @result) . ")\n"; +} diff --git a/challenge-257/jeanluc2020/perl/ch-2.pl b/challenge-257/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..69de4ea025 --- /dev/null +++ b/challenge-257/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,181 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-257/#TASK2 +# +# Task 2: Reduced Row Echelon +# =========================== +# +# Given a matrix M, check whether the matrix is in reduced row echelon form. +# +# A matrix must have the following properties to be in reduced row echelon form: +# +# 1. If a row does not consist entirely of zeros, then the first +# nonzero number in the row is a 1. We call this the leading 1. +# 2. If there are any rows that consist entirely of zeros, then +# they are grouped together at the bottom of the matrix. +# 3. In any two successive rows that do not consist entirely of zeros, +# the leading 1 in the lower row occurs farther to the right than +# the leading 1 in the higher row. +# 4. Each column that contains a leading 1 has zeros everywhere else +# in that column. +# +# For example: +# +# [ +# [1,0,0,1], +# [0,1,0,2], +# [0,0,1,3] +# ] +# +# The above matrix is in reduced row echelon form since the first nonzero +# number in each row is a 1, leading 1s in each successive row are farther to +# the right, and above and below each leading 1 there are only zeros. +# +# For more information check out this wikipedia article. +# +## Example 1 +## +## Input: $M = [ +## [1, 1, 0], +## [0, 1, 0], +## [0, 0, 0] +## ] +## Output: 0 +# +## Example 2 +## +## Input: $M = [ +## [0, 1,-2, 0, 1], +## [0, 0, 0, 1, 3], +## [0, 0, 0, 0, 0], +## [0, 0, 0, 0, 0] +## ] +## Output: 1 +# +## Example 3 +## +## Input: $M = [ +## [1, 0, 0, 4], +## [0, 1, 0, 7], +## [0, 0, 1,-1] +## ] +## Output: 1 +# +## Example 4 +## +## Input: $M = [ +## [0, 1,-2, 0, 1], +## [0, 0, 0, 0, 0], +## [0, 0, 0, 1, 3], +## [0, 0, 0, 0, 0] +## ] +## Output: 0 +# +## Example 5 +## +## Input: $M = [ +## [0, 1, 0], +## [1, 0, 0], +## [0, 0, 0] +## ] +## Output: 0 +# +## Example 6 +## +## Input: $M = [ +## [4, 0, 0, 0], +## [0, 1, 0, 7], +## [0, 0, 1,-1] +## ] +## Output: 0 +# +############################################################ +## +## discussion +## +############################################################ +# +# Check all of the conditions one by one. Return the correct +# result. + +use strict; +use warnings; + +reduced_row_echelon( [ [1, 1, 0], [0, 1, 0], [0, 0, 0] ] ); +reduced_row_echelon( [ [0, 1,-2, 0, 1], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ] ); +reduced_row_echelon( [ [1, 0, 0, 4], [0, 1, 0, 7], [0, 0, 1,-1] ] ); +reduced_row_echelon( [ [0, 1,-2, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0] ] ); +reduced_row_echelon( [ [0, 1, 0], [1, 0, 0], [0, 0, 0] ] ); +reduced_row_echelon( [ [4, 0, 0, 0], [0, 1, 0, 7], [0, 0, 1,-1] ] ); + +sub reduced_row_echelon { + my $M = shift; + # set some variables for better handling + my @matrix_rows = @$M; + my $rows = @matrix_rows; + my $columns = length($matrix_rows[0]); + # check that all rows are of the same length, otherwise this isn't a matrix + foreach my $i (0..$rows-1) { + if(length($matrix_rows[$i]) != $columns) { + print "Not a matrix: Not all rows have the same number of columns!\n"; + return; + } + } + # print the input matrix + print "Input: [\n"; + foreach my $row (@matrix_rows) { + print " [" . join(", ", @$row) . "],\n"; + } + print " ]\n"; + # initialize a few helper variables + my $last_starting_one = -1; + my $row_num = -1; + my $found_all_zeroes = 0; + # for each row, check all the conditions + foreach my $row (@matrix_rows) { + $row_num++; + my @row_ = @$row; + # find first non-zero number in row + my $first_non_zero = -1; + foreach my $i (0..$#row_) { + if($row_[$i] != 0) { + if($row_[$i] == 1) { + $first_non_zero = $i; + } else { + # we found the first non-zero number, but it's != 1 + # first condition not met + print "Output: 0\n"; + return; + } + last; + } + } + if($first_non_zero == -1) { + # this row consists entirely of zeroes + $found_all_zeroes = 1; + } + if($found_all_zeroes && $first_non_zero != -1) { + # we found a row with all zeroes before, but now we have a non-zero element in the row + # condition 2 not met + print "Output: 0\n"; + return; + } + if($first_non_zero != -1 && $first_non_zero <= $last_starting_one) { + # our first non-zero element is before the first non-zero in the previous row + # condition 3 not met + print "Output: 0\n"; + return; + } + $last_starting_one = $first_non_zero; # for the next round + foreach my $j (0..$#matrix_rows) { + next if $j == $row_num; + if($matrix_rows[$j]->[$first_non_zero] != 0 && $first_non_zero >= 0) { + # we found a row that has a non-zero value in the column that matches + # the first non-zero column in the row we're currently considering + # condition 4 not met + print "Output: 0\n"; + return; + } + } + } + print "Output: 1\n"; +} diff --git a/challenge-257/jeanluc2020/python/ch-1.py b/challenge-257/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..144d39515a --- /dev/null +++ b/challenge-257/jeanluc2020/python/ch-1.py @@ -0,0 +1,64 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-257/#TASK1 +# +# Task 1: Smaller than Current +# ============================ +# +# You are given a array of integers, @ints. +# +# Write a script to find out how many integers are smaller than current i.e. +# foreach ints[i], count ints[j] < ints[i] where i != j. +# +## Example 1 +## +## Input: @ints = (5, 2, 1, 6) +## Output: (2, 1, 0, 3) +## +## For $ints[0] = 5, there are two integers (2,1) smaller than 5. +## For $ints[1] = 2, there is one integer (1) smaller than 2. +## For $ints[2] = 1, there is none integer smaller than 1. +## For $ints[3] = 6, there are three integers (5,2,1) smaller than 6. +# +## Example 2 +## +## Input: @ints = (1, 2, 0, 3) +## Output: (1, 2, 0, 3) +# +## Example 3 +## +## Input: @ints = (0, 1) +## Output: (0, 1) +# +## Example 4 +## +## Input: @ints = (9, 4, 9, 2) +## Output: (2, 1, 2, 0) +# +############################################################ +## +## discussion +## +############################################################ +# +# Walk through the array, and for each element, walk through +# the whole array again, counting the elements that are smaller +# than the one in the outer loop. Put the result of this loop run +# at the end of the final result. +# + +def smaller_than_current(ints: list) -> None: + print("Input: (", ", ".join(str(x) for x in ints), ")", sep="") + result = [] + for i in range(len(ints)): + count = 0 + for j in range(len(ints)): + if i != j and ints[i] > ints[j]: + count += 1 + result.append(count) + print("Output: (", ", ".join(str(x) for x in result), ")", sep="") + +smaller_than_current([5, 2, 1, 6]); +smaller_than_current([1, 2, 0, 3]); +smaller_than_current([0, 1]); +smaller_than_current([9, 4, 9, 2]); + diff --git a/challenge-257/jeanluc2020/python/ch-2.py b/challenge-257/jeanluc2020/python/ch-2.py new file mode 100755 index 0000000000..2594d56ea8 --- /dev/null +++ b/challenge-257/jeanluc2020/python/ch-2.py @@ -0,0 +1,165 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-257/#TASK2 +# +# Task 2: Reduced Row Echelon +# =========================== +# +# Given a matrix M, check whether the matrix is in reduced row echelon form. +# +# A matrix must have the following properties to be in reduced row echelon form: +# +# 1. If a row does not consist entirely of zeros, then the first +# nonzero number in the row is a 1. We call this the leading 1. +# 2. If there are any rows that consist entirely of zeros, then +# they are grouped together at the bottom of the matrix. +# 3. In any two successive rows that do not consist entirely of zeros, +# the leading 1 in the lower row occurs farther to the right than +# the leading 1 in the higher row. +# 4. Each column that contains a leading 1 has zeros everywhere else +# in that column. +# +# For example: +# +# [ +# [1,0,0,1], +# [0,1,0,2], +# [0,0,1,3] +# ] +# +# The above matrix is in reduced row echelon form since the first nonzero +# number in each row is a 1, leading 1s in each successive row are farther to +# the right, and above and below each leading 1 there are only zeros. +# +# For more information check out this wikipedia article. +# +## Example 1 +## +## Input: $M = [ +## [1, 1, 0], +## [0, 1, 0], +## [0, 0, 0] +## ] +## Output: 0 +# +## Example 2 +## +## Input: $M = [ +## [0, 1,-2, 0, 1], +## [0, 0, 0, 1, 3], +## [0, 0, 0, 0, 0], +## [0, 0, 0, 0, 0] +## ] +## Output: 1 +# +## Example 3 +## +## Input: $M = [ +## [1, 0, 0, 4], +## [0, 1, 0, 7], +## [0, 0, 1,-1] +## ] +## Output: 1 +# +## Example 4 +## +## Input: $M = [ +## [0, 1,-2, 0, 1], +## [0, 0, 0, 0, 0], +## [0, 0, 0, 1, 3], +## [0, 0, 0, 0, 0] +## ] +## Output: 0 +# +## Example 5 +## +## Input: $M = [ +## [0, 1, 0], +## [1, 0, 0], +## [0, 0, 0] +## ] +## Output: 0 +# +## Example 6 +## +## Input: $M = [ +## [4, 0, 0, 0], +## [0, 1, 0, 7], +## [0, 0, 1,-1] +## ] +## Output: 0 +# +############################################################ +## +## discussion +## +############################################################ +# +# Check all of the conditions one by one. Return the correct +# result. + +def reduced_row_echelon(M: list) -> None: + # set some variables for better handling + matrix_rows = M + rows = len(matrix_rows) + columns = len(matrix_rows[0]) + # check that all rows are of the same length, otherwise this isn't a matrix + for i in range(rows): + if len(matrix_rows[i]) != columns: + print("Not a matrix: Not all rows have the same number of columns!\n") + return + # print the input matrix + print("Input: [") + for row in matrix_rows: + print(" [", ", ".join(str(x) for x in row), "],", sep="") + print(" ]") + # initialize a few helper variables + last_starting_one = -1 + row_num = -1 + found_all_zeroes = False + # for each row, check all the conditions + for row in matrix_rows: + row_num += 1 + # find first non-zero number in row + first_non_zero = -1 + for i in range(len(row)): + if row[i] != 0: + if row[i] == 1: + first_non_zero = i + else: + # we found the first non-zero number, but it's != 1 + # first condition not met + print("Output: 0") + return + break + if first_non_zero == -1: + # this row consists entirely of zeroes + found_all_zeroes = True + if found_all_zeroes and first_non_zero != -1: + # we found a row with all zeroes before, but now we have a non-zero element in the row + # condition 2 not met + print("Output: 0") + return + if first_non_zero != -1 and first_non_zero <= last_starting_one: + # our first non-zero element is before the first non-zero in the previous row + # condition 3 not met + print("Output: 0") + return + last_starting_one = first_non_zero; # for the next round + for j in range(len(matrix_rows)): + if j == row_num: + continue + if matrix_rows[j][first_non_zero] != 0 and first_non_zero >= 0: + # we found a row that has a non-zero value in the column that matches + # the first non-zero column in the row we're currently considering + # condition 4 not met + print("Output: 0") + return + print("Output: 1") + +reduced_row_echelon( [ [1, 1, 0], [0, 1, 0], [0, 0, 0] ] ); +reduced_row_echelon( [ [0, 1,-2, 0, 1], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ] ); +reduced_row_echelon( [ [1, 0, 0, 4], [0, 1, 0, 7], [0, 0, 1,-1] ] ); +reduced_row_echelon( [ [0, 1,-2, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0] ] ); +reduced_row_echelon( [ [0, 1, 0], [1, 0, 0], [0, 0, 0] ] ); +reduced_row_echelon( [ [4, 0, 0, 0], [0, 1, 0, 7], [0, 0, 1,-1] ] ); + |
