diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-25 20:49:54 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-25 20:49:54 +0100 |
| commit | fffcc16f576d39fc05d8c04f654f872945e94ae8 (patch) | |
| tree | 7c0b74725ee6d12cb8a60b58eaff50727f8e7e79 | |
| parent | 5b1904aff4cad75a4cd781479289c40a0dc474af (diff) | |
| parent | 009c16c2546a3c4ac0746a2b8dc524283046ea40 (diff) | |
| download | perlweeklychallenge-club-fffcc16f576d39fc05d8c04f654f872945e94ae8.tar.gz perlweeklychallenge-club-fffcc16f576d39fc05d8c04f654f872945e94ae8.tar.bz2 perlweeklychallenge-club-fffcc16f576d39fc05d8c04f654f872945e94ae8.zip | |
Merge pull request #10694 from pauloscustodio/master
Add Perl solution
| -rw-r--r-- | challenge-257/paulo-custodio/perl/ch-2.pl | 170 | ||||
| -rw-r--r-- | challenge-257/paulo-custodio/t/test-2.yaml | 30 | ||||
| -rw-r--r-- | challenge-258/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-258/paulo-custodio/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-258/paulo-custodio/perl/ch-2.pl | 44 | ||||
| -rw-r--r-- | challenge-258/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-258/paulo-custodio/t/test-2.yaml | 15 |
7 files changed, 302 insertions, 0 deletions
diff --git a/challenge-257/paulo-custodio/perl/ch-2.pl b/challenge-257/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..200c14f745 --- /dev/null +++ b/challenge-257/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,170 @@ +#!/usr/bin/env perl + +# Challenge 257 +# +# Task 2: Reduced Row Echelon +# Submitted by: Ali Moradi +# 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 + +use Modern::Perl; + +my $matrix = parse_matrix("@ARGV"); +say is_reduced_row_echelon($matrix); + +sub parse_matrix { + my($text) = @_; + my @lines = split(/\]\s*,/, $text); + my $matrix = []; + for (@lines) { + s/^[^-0-9]+//; + my @nums = split /[^-0-9]+/, $_; + push @$matrix, \@nums; + } + return $matrix; +} + +sub is_reduced_row_echelon { + my($matrix) = @_; + my @matrix = @$matrix; + + @matrix = move_zero_rows_to_end(@matrix); + + # leading 1 is indented + my $last_one_col = -1; + for (@matrix) { + my @row = @$_; + my $col = first_non_zero_col(@row); + if ($col >= 0) { + if ($row[$col] != 1) { + return 0; + } + elsif ($col <= $last_one_col) { + return 0; + } + else { + $last_one_col = $col; + } + } + } + + # column of leading one is all zeros + for my $row (0 .. $#matrix) { + my @row = @{$matrix[$row]}; + my $col = first_non_zero_col(@row); + if ($col >= 0) { + for my $zero_row (0 .. $row-1, $row+1 .. $#matrix) { + if ($matrix[$zero_row][$col] != 0) { + return 0; + } + } + } + } + + return 1; +} + +sub move_zero_rows_to_end { + my(@matrix) = @_; + + my @new_matrix; + my @zeros; + for (@matrix) { + my @row = @$_; + if (is_zero_row(@row)) { + push @zeros, \@row; + } + else { + push @new_matrix, \@row; + } + } + @matrix = (@new_matrix, @zeros); + return @matrix; +} + +sub first_non_zero_col { + my(@row) = @_; + my $col = 0; + while ($col < @row && $row[$col] == 0) { + $col++; + } + if ($col == scalar(@row)) { + return -1; + } + else { + return $col; + } +} + +sub is_zero_row { + my(@row) = @_; + return first_non_zero_col(@row) == -1; +} diff --git a/challenge-257/paulo-custodio/t/test-2.yaml b/challenge-257/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..e728c6701e --- /dev/null +++ b/challenge-257/paulo-custodio/t/test-2.yaml @@ -0,0 +1,30 @@ +- setup: + cleanup: + args: '[ [1, 1, 0], [0, 1, 0], [0, 0, 0] ]' + input: + output: 0 +- setup: + cleanup: + args: '[ [0, 1,-2, 0, 1], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ]' + input: + output: 1 +- setup: + cleanup: + args: '[ [1, 0, 0, 4], [0, 1, 0, 7], [0, 0, 1,-1] ]' + input: + output: 1 +- setup: + cleanup: + args: '[ [0, 1,-2, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0] ]' + input: + output: 1 +- setup: + cleanup: + args: '[ [0, 1, 0], [1, 0, 0], [0, 0, 0] ]' + input: + output: 0 +- setup: + cleanup: + args: '[ [4, 0, 0, 0], [0, 1, 0, 7], [0, 0, 1,-1] ]' + input: + output: 0 diff --git a/challenge-258/paulo-custodio/Makefile b/challenge-258/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-258/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-258/paulo-custodio/perl/ch-1.pl b/challenge-258/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..2cf654752e --- /dev/null +++ b/challenge-258/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +# Challenge 258 +# +# Task 1: Count Even Digits Number +# Submitted by: Mohammad Sajid Anwar +# You are given a array of positive integers, @ints. +# +# Write a script to find out how many integers have even number of digits. +# +# Example 1 +# Input: @ints = (10, 1, 111, 24, 1000) +# Output: 3 +# +# There are 3 integers having even digits i.e. 10, 24 and 1000. +# Example 2 +# Input: @ints = (111, 1, 11111) +# Output: 0 +# Example 3 +# Input: @ints = (2, 8, 1024, 256) +# Output: 1 + +use Modern::Perl; + +my @ints = @ARGV; +say scalar grep {length($_) % 2 == 0} @ints; diff --git a/challenge-258/paulo-custodio/perl/ch-2.pl b/challenge-258/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..b7770dee60 --- /dev/null +++ b/challenge-258/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +# Challenge 258 +# +# Task 2: Sum of Values +# Submitted by: Mohammad Sajid Anwar +# You are given an array of integers, @int and an integer $k. +# +# Write a script to find the sum of values whose index binary representation has exactly $k number of 1-bit set. +# +# Example 1 +# Input: @ints = (2, 5, 9, 11, 3), $k = 1 +# Output: 17 +# +# Binary representation of index 0 = 0 +# Binary representation of index 1 = 1 +# Binary representation of index 2 = 10 +# Binary representation of index 3 = 11 +# Binary representation of index 4 = 100 +# +# So the indices 1, 2 and 4 have total one 1-bit sets. +# Therefore the sum, $ints[1] + $ints[2] + $ints[4] = 17 +# Example 2 +# Input: @ints = (2, 5, 9, 11, 3), $k = 2 +# Output: 11 +# Example 3 +# Input: @ints = (2, 5, 9, 11, 3), $k = 0 +# Output: 2 + +use Modern::Perl; +use List::Util 'sum'; + +my($k, @ints) = @ARGV; +say sum + map {$_->[1]} + grep {num_ones($_->[0]) == $k} + map {[$_, $ints[$_]]} 0 .. $#ints; + +sub num_ones { + my($n) = @_; + my $bin = sprintf("%b", $n); + my $ones = $bin =~ tr/1/1/; + return $ones; +} diff --git a/challenge-258/paulo-custodio/t/test-1.yaml b/challenge-258/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..5138f53225 --- /dev/null +++ b/challenge-258/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 10 1 111 24 1000 + input: + output: 3 +- setup: + cleanup: + args: 111 1 11111 + input: + output: 0 +- setup: + cleanup: + args: 2 8 1024 256 + input: + output: 1 diff --git a/challenge-258/paulo-custodio/t/test-2.yaml b/challenge-258/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..1f8234084e --- /dev/null +++ b/challenge-258/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 2 5 9 11 3 + input: + output: 17 +- setup: + cleanup: + args: 2 2 5 9 11 3 + input: + output: 11 +- setup: + cleanup: + args: 0 2 5 9 11 3 + input: + output: 2 |
