aboutsummaryrefslogtreecommitdiff
path: root/challenge-218
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2023-05-27 01:11:25 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2023-05-27 01:11:25 +0100
commite755f960534a82c604442bf3d6ac6c3b33dc07dc (patch)
tree16195a5fb8d8edb7b5ffa94dd02b2aaaeea06e75 /challenge-218
parent5e66b7dbc0a48ff40cf7fc56d2f1a5aac484dc42 (diff)
downloadperlweeklychallenge-club-e755f960534a82c604442bf3d6ac6c3b33dc07dc.tar.gz
perlweeklychallenge-club-e755f960534a82c604442bf3d6ac6c3b33dc07dc.tar.bz2
perlweeklychallenge-club-e755f960534a82c604442bf3d6ac6c3b33dc07dc.zip
- Added solutions by Robert DiCicco.
Diffstat (limited to 'challenge-218')
-rw-r--r--challenge-218/robert-dicicco/python/ch-2.py110
-rw-r--r--challenge-218/robert-dicicco/raku/ch-2.raku120
2 files changed, 230 insertions, 0 deletions
diff --git a/challenge-218/robert-dicicco/python/ch-2.py b/challenge-218/robert-dicicco/python/ch-2.py
new file mode 100644
index 0000000000..20be6643f2
--- /dev/null
+++ b/challenge-218/robert-dicicco/python/ch-2.py
@@ -0,0 +1,110 @@
+#!/usr/bin/env python
+#---------------------------------------------
+# AUTHOR: Robert DiCicco
+# DATE : 2023-05-26
+# Challenge 218 MatrixScore.py ( Python )
+#---------------------------------------------
+matrix = [ [0,0,1,1],
+ [1,0,1,0],
+ [1,1,0,0], ]
+row = 0
+col = 0
+
+def ShowMatrix():
+ total = 0
+ for x in range(3): #[0,1,2]:
+ print(matrix[x])
+ total += binary_to_decimal(matrix[x])
+ print("Total = ",total,"\n")
+
+def ToggleRow(r, ov):
+ cnt = 0
+ testmat = []
+ for x in matrix[r]:
+ if x == 0:
+ testmat.append(1)
+ else:
+ testmat.append(0)
+ cnt += 1
+ testval = binary_to_decimal(testmat)
+ cnt = 0
+ if testval > ov:
+ for x in matrix[r]:
+ if x == 0:
+ matrix[r][cnt] = 1
+ else:
+ matrix[r][cnt] = 0
+ cnt += 1
+ print("Toggled Row ",r)
+ ShowMatrix()
+
+
+def ToggleCol(c):
+ ov = GetColVal(c)
+ row = 0
+ testmat = []
+ while row < 3:
+ if matrix[row][c] == 0:
+ testmat.append(1)
+ else:
+ testmat.append(0)
+ row += 1
+ testval = binary_to_decimal(testmat)
+ if testval > ov:
+ for x in range(3): #[0,1,2]:
+ matrix[x][c] = testmat[x]
+ print("Toggled column ",c)
+ ShowMatrix()
+
+def binary_to_decimal(binary_array):
+ decimal = 0
+ power = len(binary_array) - 1
+ for digit in binary_array:
+ decimal += digit * (2 ** power)
+ power -= 1
+ return decimal
+
+def GetColVal(c):
+ testmat = []
+ row = 0
+ while row < 3:
+ testmat.append(matrix[row][c])
+ row += 1
+ colval = binary_to_decimal(testmat)
+ return colval
+
+############################################
+ShowMatrix()
+for myrow in range(3): #[0,1,2]:
+ bd = binary_to_decimal(matrix[myrow])
+ ToggleRow(myrow, bd)
+
+for mycol in range(4): #[0,1,2,3]:
+ ToggleCol(mycol)
+
+#---------------------------------------------
+# SAMPLE OUTPUT
+# python .\MatrixScore.py
+# [0, 0, 1, 1]
+# [1, 0, 1, 0]
+# [1, 1, 0, 0]
+# Total = 25
+
+# Toggled Row 0
+# [1, 1, 0, 0]
+# [1, 0, 1, 0]
+# [1, 1, 0, 0]
+# Total = 34
+
+# Toggled column 2
+# [1, 1, 1, 0]
+# [1, 0, 0, 0]
+# [1, 1, 1, 0]
+# Total = 36
+
+# Toggled column 3
+# [1, 1, 1, 1]
+# [1, 0, 0, 1]
+# [1, 1, 1, 1]
+# Total = 39
+#---------------------------------------------
diff --git a/challenge-218/robert-dicicco/raku/ch-2.raku b/challenge-218/robert-dicicco/raku/ch-2.raku
new file mode 100644
index 0000000000..b0b5f50d59
--- /dev/null
+++ b/challenge-218/robert-dicicco/raku/ch-2.raku
@@ -0,0 +1,120 @@
+#!/usr/bin/env raku
+#---------------------------------------------
+# AUTHOR: Robert DiCicco
+# DATE : 2023-05-26
+# Challenge 218 MatrixScore.py ( Raku )
+#---------------------------------------------
+use v6;
+
+my @matrix = [ [0,0,1,1],
+ [1,0,1,0],
+ [1,1,0,0], ];
+
+sub ShowMatrix() {
+ my $total = 0;
+ my $cnt = 0;
+ while $cnt <= 2 {
+ say (@matrix[$cnt]);
+ $total += binary_to_decimal(@matrix[$cnt]);
+ $cnt++;
+ }
+ say "Total = ",$total;
+ say "";
+}
+
+sub binary_to_decimal(@binary_array) {
+ my $decimal = 0;
+ my $power = @binary_array.elems - 1;
+ for (@binary_array) -> $digit {
+ $decimal += $digit * (2 ** $power);
+ $power -= 1;
+ }
+ return $decimal;
+}
+
+sub ToggleRow($r, $ov) {
+ my @testmat = [];
+ my $col = 0;
+ while $col <= 3 {
+ my $x = @matrix[$r][$col++];
+ $x == 0 ?? @testmat.push: 1 !! @testmat.push: 0;
+ }
+ my $testval = binary_to_decimal(@testmat);
+ if $testval > $ov {
+ say "Toggled row ",$r;
+ @matrix[$r] = @testmat;
+ ShowMatrix();
+ }
+}
+
+sub ToggleCol($c) {
+ my $ov = GetColVal($c);
+ my $row = 0;
+ my @testmat = [];
+ my $testval = 0;
+ while $row < 3 {
+ @matrix[$row][$c] == 0 ?? @testmat.push: 1 !! @testmat.push: 0;
+ $row++;
+ $testval = binary_to_decimal(@testmat);
+ }
+ if $testval > $ov {
+ for 0..3 -> $x {
+ @matrix[$x][$c] = @testmat[$x];
+ }
+ print("Toggled column ",$c,"\n");
+ ShowMatrix();
+ }
+}
+
+sub GetColVal($c) {
+ my @testmat = [];
+ my $row = 0;
+ while $row < 3 {
+ @testmat.push: @matrix[$row][$c];
+ $row++;
+ }
+ my $colval = binary_to_decimal(@testmat);
+ return $colval;
+}
+
+
+####################################################
+ShowMatrix();
+
+for 0..2 -> $myrow {
+ my $bd = binary_to_decimal(@matrix[$myrow]);
+ ToggleRow($myrow, $bd);
+}
+
+for (0..3) -> $myrow {
+ ToggleCol($myrow);
+}
+
+=begin comment
+#---------------------------------------------
+SAMPLE OUTPUT
+raku .\MatrixScore.rk
+[0 0 1 1]
+[1 0 1 0]
+[1 1 0 0]
+Total = 25
+
+Toggled row 0
+[1 1 0 0]
+[1 0 1 0]
+[1 1 0 0]
+Total = 34
+
+Toggled column 2
+[1 1 1 0]
+[1 0 0 0]
+[1 1 1 0]
+Total = 36
+
+Toggled column 3
+[1 1 1 1]
+[1 0 0 1]
+[1 1 1 1]
+Total = 39
+#---------------------------------------------
+=end comment