diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-05-29 00:12:58 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-05-29 00:12:58 +0100 |
| commit | 3f7d7e29c9d50eb0f0c90b20972de05fb1887fd1 (patch) | |
| tree | a697fc2f7381494f9c1a67416839e5c7f14d1065 /challenge-218 | |
| parent | 698c4c2f393cf6366acc3a4e4ec8f2d3c0268824 (diff) | |
| download | perlweeklychallenge-club-3f7d7e29c9d50eb0f0c90b20972de05fb1887fd1.tar.gz perlweeklychallenge-club-3f7d7e29c9d50eb0f0c90b20972de05fb1887fd1.tar.bz2 perlweeklychallenge-club-3f7d7e29c9d50eb0f0c90b20972de05fb1887fd1.zip | |
- Added solutions by Matthias Muth.
- Added solutions by Robbie Hatley.
- Added solutions by Roger Bell_West.
- Added solutions by Lubos Kolouch.
- Added solutions by Solathian.
- Added solutions by Athanasius.
- Added solutions by Simon Green.
- Added solutions by Arne Sommer.
- Added solutions by Bruce Gray.
- Added solutions by Cheok-Yin Fung.
- Added solutions by Jan Krnavek.
- Added solutions by BarrOff.
- Added solutions by Jaldhar H. Vyas.
- Added solutions by Robert Ransbottom.
- Added solutions by Robert DiCicco.
- Added solutions by Andrea Piseri.
Diffstat (limited to 'challenge-218')
| -rw-r--r-- | challenge-218/robert-dicicco/perl/ch-2.pl | 137 | ||||
| -rw-r--r-- | challenge-218/robert-dicicco/ruby/ch-2.rb | 124 |
2 files changed, 261 insertions, 0 deletions
diff --git a/challenge-218/robert-dicicco/perl/ch-2.pl b/challenge-218/robert-dicicco/perl/ch-2.pl new file mode 100644 index 0000000000..b8ed872b42 --- /dev/null +++ b/challenge-218/robert-dicicco/perl/ch-2.pl @@ -0,0 +1,137 @@ +#!/usr/bin/env perl +#--------------------------------------------- +# AUTHOR: Robert DiCicco +# DATE : 2023-05-27 +# Challenge 218 MatrixScore.py ( Perl ) +#--------------------------------------------- +use strict; +use warnings; +use feature 'say'; + +my @matrix = ( [0,0,1,1], + [1,0,1,0], + [1,1,0,0] ) ; + +sub GetColVal { + my $c = shift; + my @testmat = (); + my $row = 0; + + while ($row < 3) { + push(@testmat, $matrix[$row][$c]); + $row++; + } + + my $colval = binary_to_decimal(\@testmat); + return $colval; +} + + +sub ToggleCol { + my $c = shift; + my $ov = GetColVal($c); + my $row = 0; + my @testmat = (); + my $testval = 0; + while ($row < 3) { + my $x = $matrix[$row][$c]; + $x == 0 ? push(@testmat,1) : push(@testmat,0); + $row++; + $testval = binary_to_decimal(\@testmat); + } + if ($testval > $ov) { + my $x = 0; + while ($x < 4) { + $matrix[$x][$c] = $testmat[$x]; + $x++; + } + print("Toggled column ",$c+1,"\n"); + ShowMatrix(); + } +} + +sub ToggleRow { + my $r = shift; + my $ov = shift; + my @testmat = (); + my $col = 0; + while ($col <= 3) { + my $x = $matrix[$r][$col]; + $x == 0 ? push(@testmat,1) : push(@testmat,0); + $col++; + } + my $testval = binary_to_decimal(\@testmat); + if ($testval > $ov){ + @{$matrix[$r]} = @testmat; + say "Toggle row ",$r+1; + ShowMatrix(); + } +} + +sub binary_to_decimal { + my $b = shift; + my @binary_array = @{$b}; + my $decimal = 0; + my $power = scalar @binary_array - 1; + for my $digit (@binary_array) { + $decimal += $digit * (2 ** $power); + $power -= 1; + } + return $decimal; +} + +sub ShowMatrix { + my $total = 0; + my $cnt = 0; + + while ($cnt < 3) { + print("@{$matrix[$cnt]}\n"); + $total += binary_to_decimal($matrix[$cnt]); + $cnt++; + } + say "Total = ",$total; + say ""; +} + +ShowMatrix(); +my $myrow = 0; +while ($myrow < 3) { + my $bd = binary_to_decimal($matrix[$myrow]); + ToggleRow($myrow,$bd); + $myrow++; +} + +my $mycol = 0; +while($mycol < 4) { + ToggleCol($mycol); + $mycol++; +} + +#--------------------------------------------- +# SAMPLE OUTPUT +# perl .\MatrixScore.pl +# 0 0 1 1 +# 1 0 1 0 +# 1 1 0 0 +# Total = 25 + +# Toggle row 1 +# 1 1 0 0 +# 1 0 1 0 +# 1 1 0 0 +# Total = 34 + +# Toggled column 3 +# 1 1 1 0 +# 1 0 0 0 +# 1 1 1 0 +# Total = 36 + +# Toggled column 4 +# 1 1 1 1 +# 1 0 0 1 +# 1 1 1 1 +# Total = 39 +#--------------------------------------------- + + diff --git a/challenge-218/robert-dicicco/ruby/ch-2.rb b/challenge-218/robert-dicicco/ruby/ch-2.rb new file mode 100644 index 0000000000..976ce5a278 --- /dev/null +++ b/challenge-218/robert-dicicco/ruby/ch-2.rb @@ -0,0 +1,124 @@ +#!/usr/bin/env ruby +#--------------------------------------------- +# AUTHOR: Robert DiCicco +# DATE : 2023-05-27 +# Challenge 218 MatrixScore.py ( Ruby ) +#--------------------------------------------- + +$matrix = [ [0,0,1,1], + [1,0,1,0], + [1,1,0,0], ] + +def GetColVal(c) + testmat = [] + row = 0 + while row < 3 + testmat.push($matrix[row][c]) + row += 1 + end + colval = binary_to_decimal(testmat) + return colval +end + +def ToggleCol(c) + ov = GetColVal(c); + row = 0 + testmat = [] + testval = 0 + while row < 3 + $matrix[row][c] == 0 ? testmat.push(1) : testmat.push(0) + row += 1 + end + testval = binary_to_decimal(testmat); + if testval > ov + x = 0 + while x < 3 + $matrix[x][c] = testmat[x] + x += 1 + end + puts("Toggled column #{c+1}") + ShowMatrix() + end +end + +def ToggleRow(r, ov) + testmat = [] + col = 0 + while col <= 3 + x = $matrix[r][col] + if x == 0 + testmat.push(1) + else + testmat.push(0) + end + col += 1 + end + testval = binary_to_decimal(testmat) + if testval > ov + puts("\nToggled row #{r+1}") + $matrix[r] = testmat + ShowMatrix() + end +end + +def ShowMatrix() + total = 0 + cnt = 0 + while cnt < 3 + puts("#{$matrix[cnt]}") + total += binary_to_decimal($matrix[cnt]) + cnt += 1 + end + puts("Total = #{total}\n\n") +end + +def binary_to_decimal(binary_array) + decimal = 0 + power = binary_array.length() - 1 + binary_array.each do |digit| + decimal += digit * ( 2 ** power) + power -= 1 + end + return decimal +end + +ShowMatrix() +myrow = 0 +while myrow < 3 + bd = binary_to_decimal($matrix[myrow].to_a) + ToggleRow(myrow,bd) + myrow += 1 +end + +mycol = 0 +while mycol < 4 + ToggleCol(mycol) + mycol += 1 +end + +#--------------------------------------------- +# SAMPLE OUTPUT +# ruby .\MatrixScore.rb +# [0, 0, 1, 1] +# [1, 0, 1, 0] +# [1, 1, 0, 0] +# Total = 25 + +# Toggled row 1 +# [1, 1, 0, 0] +# [1, 0, 1, 0] +# [1, 1, 0, 0] +# Total = 34 + +# Toggled column 3 +# [1, 1, 1, 0] +# [1, 0, 0, 0] +# [1, 1, 1, 0] +# Total = 36 + +# Toggled column 4 +# [1, 1, 1, 1] +# [1, 0, 0, 1] +# [1, 1, 1, 1] +# Total = 39 +#--------------------------------------------- |
