diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-05-28 12:35:02 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-05-28 12:35:02 +0200 |
| commit | 011d14fa29b85c7dcdc70a65af2483c0a81b65fe (patch) | |
| tree | d1dee35b808d40aa62d9eb2b3e280abd4d0c0d63 | |
| parent | 061ce8440f8259df938c87beb7be092711c259e0 (diff) | |
| download | perlweeklychallenge-club-011d14fa29b85c7dcdc70a65af2483c0a81b65fe.tar.gz perlweeklychallenge-club-011d14fa29b85c7dcdc70a65af2483c0a81b65fe.tar.bz2 perlweeklychallenge-club-011d14fa29b85c7dcdc70a65af2483c0a81b65fe.zip | |
feat(challenge-114/lubos-kolouch/perl,python/): Challenge 114 LK Perl Python
33 files changed, 3677 insertions, 2675 deletions
diff --git a/challenge-026/paulo-custodio/c/ch-1.c b/challenge-026/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..26e85e147a --- /dev/null +++ b/challenge-026/paulo-custodio/c/ch-1.c @@ -0,0 +1,33 @@ +/* +Challenge 026 + +Task #1 +Create a script that accepts two strings, let us call it, "stones" and +"jewels". It should print the count of "alphabet" from the string "stones" +found in the string "jewels". For example, if your stones is "chancellor" and +"jewels" is "chocolate", then the script should print "8". To keep it simple, +only A-Z,a-z characters are acceptable. Also make the comparison case +sensitive. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int count_alphabet(const char* stones, const char* jewels) { + int count = 0; + for (const char* p = stones; *p; p++) { + if (strchr(jewels, *p)) + count++; + } + return count; +} + +int main(int argc, char* argv[]) { + if (argc != 3) { + fputs("Usage: ch-1 stones jewels\n", stderr); + return EXIT_FAILURE; + } + + printf("%d\n", count_alphabet(argv[1], argv[2])); +} diff --git a/challenge-026/paulo-custodio/c/ch-2.c b/challenge-026/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..565926e544 --- /dev/null +++ b/challenge-026/paulo-custodio/c/ch-2.c @@ -0,0 +1,45 @@ +/* +Challenge 026 + +Task #2 +Create a script that prints mean angles of the given list of angles in degrees. +Please read wiki page that explains the formula in details with an example. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory\n", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +double mean_angle(double *angles, size_t size) { + double y = 0.0, x = 0.0; + for (size_t i = 0; i < size; i++) { + x += cos(angles[i] * M_PI / 180.0); + y += sin(angles[i] * M_PI / 180.0); + } + + return atan2(y / (double)size, x / (double)size) * 180.0 / M_PI; +} + +int main(int argc, char* argv[]) { + if (argc < 2) { + fputs("usage: ch-2 angles...\n", stderr); + return EXIT_FAILURE; + } + + size_t size = argc-1; + double* angles = check_mem(malloc(size * sizeof(double))); + for (size_t i = 0; i < size; i++) + angles[i] = atof(argv[i+1]); + + printf("%.1f", mean_angle(angles, size)); + + free(angles); +} diff --git a/challenge-114/lubos-kolouch/perl/ch-1.pl b/challenge-114/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..fb6e3649e5 --- /dev/null +++ b/challenge-114/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,10 @@ +sub next_palindrome { + my $n = shift; + while (1) { + $n += 1; + return $n if $n eq reverse $n; + } +} + +# Test +print next_palindrome(1234), "\n"; # Output: 1331 diff --git a/challenge-114/lubos-kolouch/perl/ch-2.pl b/challenge-114/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..349a330796 --- /dev/null +++ b/challenge-114/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,11 @@ +sub next_same_bits { + my $n = shift; + my $one_bits = unpack( "%32b*", pack( "N", $n ) ); + while (1) { + $n += 1; + return $n if unpack( "%32b*", pack( "N", $n ) ) == $one_bits; + } +} + +# Test +print next_same_bits(3), "\n"; # Output: 5 diff --git a/challenge-114/lubos-kolouch/python/ch-1.py b/challenge-114/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..e4d4da4dc0 --- /dev/null +++ b/challenge-114/lubos-kolouch/python/ch-1.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +def next_palindrome(n): + while True: + n += 1 + if str(n) == str(n)[::-1]: + return n + + +# Test +print(next_palindrome(1234)) # Output: 1331 diff --git a/challenge-114/lubos-kolouch/python/ch-2.py b/challenge-114/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..ebd0c29c85 --- /dev/null +++ b/challenge-114/lubos-kolouch/python/ch-2.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +def next_same_bits(n): + binary = bin(n)[2:] + one_bits = binary.count("1") + while True: + n += 1 + if bin(n)[2:].count("1") == one_bits: + return n + + +# Test +print(next_same_bits(3)) # Output: 5 diff --git a/challenge-218/e-choroba/perl/ch-1.pl b/challenge-218/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..6e03c400c4 --- /dev/null +++ b/challenge-218/e-choroba/perl/ch-1.pl @@ -0,0 +1,59 @@ +#! /usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +# Optimisation: Select the 3 least and 3 greatest numbers and build +# the maximum product from them by trying all the possibilities. The +# least numbers are needed as they might introduce negative numbers. +sub maximum_product (@list) { + return maximum_product_simple(@list) if @list <= 6; + return maximum_product_simple((sort { $a <=> $b } @list)[0, 1, 2, -3, -2, -1]) +} + +sub maximum_product_simple (@list) { + return product(@list) if 3 == @list; + + my $max_product = product(@list[0, 1, 2]); + for my $i (0 .. $#list - 2) { + for my $j ($i + 1 .. $#list - 1) { + my $p_ij = product(@list[$i, $j]); + for my $k ($j + 1 .. $#list) { + my $prod = $p_ij * $list[$k]; + $max_product = $prod if $prod > $max_product; + } + } + } + return $max_product +} + +sub product { + my $p = 1; + $p *= $_ for @_; + $p +} + +use Test::More tests => 2 * 4 + 1; + +for my $maximum_product (*maximum_product{CODE}, + *maximum_product_simple{CODE} +) { + is $maximum_product->(3, 1, 2), 6, 'Example 1'; + is $maximum_product->(4, 1, 3, 2), 24, 'Example 2'; + is $maximum_product->(-1, 0, 1, 3, 1), 3, 'Example 3'; + is $maximum_product->(-8, 2, -9, 0, -4, 3), 216, 'Example 4'; +} + +my @long = map int(rand 100) - 50, 1 .. 20; +is maximum_product(@long), maximum_product_simple(@long), "same @long"; + +use Benchmark qw{ cmpthese }; +cmpthese(-3, { + simple => sub { maximum_product_simple(@long) }, + preselect => sub { maximum_product(@long) }, +}); + +__END__ + Rate simple preselect +simple 8665/s -- -94% +preselect 140390/s 1520% -- diff --git a/challenge-218/e-choroba/perl/ch-2.pl b/challenge-218/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..20714112d9 --- /dev/null +++ b/challenge-218/e-choroba/perl/ch-2.pl @@ -0,0 +1,74 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub matrix_score($m) { + my $height = @$m; + my $width = @{ $m->[0] }; + my $ser = serialise($m); + my %agenda = my %done = ($ser => undef); + my $max = score($ser, $width); + + while (%agenda) { + my %next; + for my $s (keys %agenda) { + for my $i (0 .. ($width, $height)[$width < $height] - 1) { + my @flips; + push @flips, flip_col($s, $i, $width, $height) if $i < $width; + push @flips, flip_row($s, $i, $width, $height) if $i < $height; + for my $f (@flips) { + unless (exists $done{$f}) { + my $score = score($f, $width); + $max = $score if $score > $max; + undef $next{$f}; + undef $done{$f}; + } + } + } + } + %agenda = %next; + } + return $max +} + +sub serialise($m) { join "", map @$_, map @$_, @_ } + +sub flip_col($s, $x, $width, $height) { + for my $y (0 .. $height - 1) { + my $pos = $x + $y * $width; + substr $s, $pos, 1, substr($s, $pos, 1) ? 0 : 1; + } + return $s +} +sub flip_row($s, $y, $width, $height) { + my $pos = $y * $width; + for my $x (0 .. $width - 1) { + substr $s, $pos, 1, substr($s, $pos, 1) ? 0 : 1; + ++$pos; + } + return $s +} + +sub score($s, $width) { + my $score = 0; + my $pos = 0; + while (length(my $row = substr $s, $pos, $width)) { + $score += oct "0b$row"; + } continue { + $pos += $width; + } + return $score +} + +use Test::More tests => 3 + 2; + +is score('111110011111', 4), 39, 'score'; +is flip_col('001110', 1, 3, 2), '011100', 'flip column'; +is flip_row('001110', 1, 3, 2), '001001', 'flip row'; + +is matrix_score([ [0,0,1,1], + [1,0,1,0], + [1,1,0,0], ]), 39, 'Example 1'; + +is matrix_score([ [0] ]), 1, 'Example 2'; diff --git a/challenge-218/jo-37/perl/ch-1.pl b/challenge-218/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..acde924b6e --- /dev/null +++ b/challenge-218/jo-37/perl/ch-1.pl @@ -0,0 +1,69 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV >= 3; +usage: $0 [-examples] [-tests] [--] [N...] + +-examples + run the examples from the challenge + +-tests + run some tests + +N... + list of at least three numbers + +EOS + + +### Input and Output + +say max_prod(@ARGV); + + +### Implementation + +# There are only two candidates for the maximum product of three +# elements: +# - the product of the three largest elements +# - the product of the two smallest elements with the largest +sub max_prod { + # For the sake of convenience, sort the elements. + my @s = sort {$a <=> $b} @_; + my $left = $s[0] * $s[1] * $s[-1]; + my $right = $s[-3] * $s[-2] * $s[-1]; + + $left > $right ? $left : $right; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is max_prod(3, 1, 2), 6, 'example 1'; + is max_prod(4, 1, 3, 2), 24, 'example 2'; + is max_prod(-1, 0, 1, 3, 1), 3, 'example 3'; + is max_prod(-8, 2, -9, 0, -4, 3), 216, 'example 4'; + } + + SKIP: { + skip "tests" unless $tests; + + is max_prod(-1, -2, -3, -4, -5), -6, 'all negative'; + is max_prod(1, 2, 3, 4, -5), 24, 'one negative'; + is max_prod(1, 2, -3, -4, 4, 5), 60, 'left wins'; + is max_prod(1, -2, 3, -4, 4, 5), 60, 'right wins'; + } + + done_testing; + exit; +} diff --git a/challenge-218/jo-37/perl/ch-2.pl b/challenge-218/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..6c67fa4ac0 --- /dev/null +++ b/challenge-218/jo-37/perl/ch-2.pl @@ -0,0 +1,89 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; +use PDL::NiceSlice; +use Math::Prime::Util 'fromdigits'; +use experimental 'signatures'; + +our ($tests, $examples, $verbose); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [-verbose] [--] [...] + +-examples + run the examples from the challenge + +-tests + run some tests + +-verbose + enable trace output + +M + matrix from zeroes and ones in any form accepted by the PDL string + constructor, e.g. + "[ [0,0,1,1], [1,0,1,0], [1,1,0,0], ]" + +EOS + + +### Input and Output + +say matrix_score(byte "@ARGV"); + + +### Implementation + +# The matrix is maximal w.r.t. row toggles if the first column is +# all-ones. Any toggled row would decrease the overall sum. +# The matrix is maximal w.r.t. column toggles, if all columns have at +# least as many ones as zeroes. Toggling a column having an equal +# number of zeroes and ones would not change the overall sum whereas the +# overall sum would decrease otherwise. + +sub matrix_score ($b) { + say "inititial:", $b if $verbose; + + # Toggle all rows having a zero in the first column. + $b ^= !$b(0); + say "intermediate:", $b if $verbose; + + # Toggle all columns having less ones than zeroes. + $b ^= $b->xchg(0, 1)->sumover < $b->dim(1) / 2; + say "final:", $b if $verbose; + + # Fromdigits accepts digits of any size, thus there is no need to + # calculate the individual numbers. + fromdigits $b->xchg(0, 1)->sumover->unpdl, 2; +} + + +### Examples and tests + +sub run_tests { + local $verbose; + + SKIP: { + skip "examples" unless $examples; + + is matrix_score(byte [0,0,1,1], [1,0,1,0], [1,1,0,0]), 39, 'example 1'; + is matrix_score(byte [[0]]), 1, 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + + is matrix_score(byte [1, 1, 0, 0, 0, 1], [0, 0, 0, 1, 1, 1], + [1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 0, 1], [1, 0, 0, 0, 1, 1]), + 253, 'toggle all but first columns'; + # 32 * 5 + (16 + 8 + 4 + 2 + 1) * 3 + + } + + done_testing; + exit; +} diff --git a/challenge-218/paulo-custodio/Makefile b/challenge-218/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-218/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-218/paulo-custodio/perl/ch-1.pl b/challenge-218/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..2172a1675a --- /dev/null +++ b/challenge-218/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl + +# Challenge 218 +# +# Task 1: Maximum Product +# Submitted by: Mohammad S Anwar +# +# You are given a list of 3 or more integers. +# +# Write a script to find the 3 integers whose product is the maximum and return it. +# Example 1 +# +# Input: @list = (3, 1, 2) +# Output: 6 +# +# 1 x 2 x 3 => 6 +# +# Example 2 +# +# Input: @list = (4, 1, 3, 2) +# Output: 24 +# +# 2 x 3 x 4 => 24 +# +# Example 3 +# +# Input: @list = (-1, 0, 1, 3, 1) +# Output: 3 +# +# 1 x 1 x 3 => 3 +# +# Example 4 +# +# Input: @list = (-8, 2, -9, 0, -4, 3) +# Output: 216 +# +# -9 × -8 × 3 => 216 + +use Modern::Perl; + +sub max_product { + my(@N) = @_; + return 0 if @N < 3; + my $max = 0; + for my $i (0 .. $#N-2) { + for my $j ($i+1 .. $#N-1) { + for my $k ($j+1 .. $#N) { + my $prod = $N[$i]*$N[$j]*$N[$k]; + $max = $prod if $prod > $max; + } + } + } + return $max; +} + +say max_product(@ARGV); diff --git a/challenge-218/paulo-custodio/perl/ch-2.pl b/challenge-218/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..15cdefc7b0 --- /dev/null +++ b/challenge-218/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,94 @@ +#!/usr/bin/perl + +# Challenge 218 +# +# Task 2: Matrix Score +# Submitted by: Mohammad S Anwar +# +# You are given a m x n binary matrix i.e. having only 1 and 0. +# +# You are allowed to make as many moves as you want to get the highest score. +# +# A move can be either toggling each value in a row or column. +# +# To get the score, convert the each row binary to dec and return the sum. +# Example 1: +# +# Input: @matrix = [ [0,0,1,1], +# [1,0,1,0], +# [1,1,0,0], ] +# Output: 39 +# +# Move #1: convert row #1 => 1100 +# [ [1,1,0,0], +# [1,0,1,0], +# [1,1,0,0], ] +# +# Move #2: convert col #3 => 101 +# [ [1,1,1,0], +# [1,0,0,0], +# [1,1,1,0], ] +# +# Move #3: convert col #4 => 111 +# [ [1,1,1,1], +# [1,0,0,1], +# [1,1,1,1], ] +# +# Score: 0b1111 + 0b1001 + 0b1111 => 15 + 9 + 15 => 39 +# +# Example 2: +# +# Input: @matrix = [ [0] ] +# Output: 1 + +use Modern::Perl; +use List::Util 'sum'; + +sub compute_score { + my(@game) = @_; + return sum(map {oct("0b$_")} @game); +} + +sub invert_row { + my($row, @game) = @_; + $game[$row] =~ tr/01/10/; + return @game; +} + +sub invert_col { + my($col, @game) = @_; + for (@game) { + $_ = substr($_,0,$col).(1-substr($_,$col,1)).substr($_,$col+1); + } + return @game; +} + +sub maximize_score { + my(@game) = @_; + my $max = compute_score(@game); + my $changed; + do { + $changed = 0; + for my $row (0..$#game) { + my @new_game = invert_row($row, @game); + my $new_max = compute_score(@new_game); + if ($new_max > $max) { + @game = @new_game; + $max = $new_max; + $changed = 1; + } + } + for my $col (0..length($game[0])-1) { + my @new_game = invert_col($col, @game); + my $new_max = compute_score(@new_game); + if ($new_max > $max) { + @game = @new_game; + $max = $new_max; + $changed = 1; + } + } + } while ($changed); + return @game; +} + +say compute_score(maximize_score(@ARGV)); diff --git a/challenge-218/paulo-custodio/t/test-1.yaml b/challenge-218/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..538bbd6a57 --- /dev/null +++ b/challenge-218/paulo-custodio/t/test-1.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: 3 1 2 + input: + output: 6 +- setup: + cleanup: + args: 4 1 3 2 + input: + output: 24 +- setup: + cleanup: + args: -1 0 1 3 1 + input: + output: 3 +- setup: + cleanup: + args: -8 2 -9 0 -4 3 + input: + output: 216 diff --git a/challenge-218/paulo-custodio/t/test-2.yaml b/challenge-218/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..d22f9cb2fb --- /dev/null +++ b/challenge-218/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 0011 1010 1100 + input: + output: 39 +- setup: + cleanup: + args: 0 + input: + output: 1 diff --git a/challenge-218/robert-dicicco/julia/ch-2.jl b/challenge-218/robert-dicicco/julia/ch-2.jl new file mode 100644 index 0000000000..25003b78cb --- /dev/null +++ b/challenge-218/robert-dicicco/julia/ch-2.jl @@ -0,0 +1,127 @@ +#!/usr/bin/env julia +#--------------------------------------------- +# AUTHOR: Robert DiCicco +# DATE : 2023-05-27 +# Challenge 218 MatrixScore.py ( Julia ) +#--------------------------------------------- +using Printf + +matrix = [ [0,0,1,1], + [1,0,1,0], + [1,1,0,0], ] + +function ToggleRow(r, ov) + testmat = [] + col = 1 + while col < 5 + x = matrix[r][col] + if x == 0 + push!(testmat,1) + else + push!(testmat,0) + end + col += 1 + end + testval = binary_to_decimal(testmat) + if testval > ov + @printf("\nToggled row %d\n",r) + matrix[r] = testmat + ShowMatrix() + end +end + +function ToggleCol(c) + ov = GetColVal(c); + row = 1; + testmat = []; + testval = 0; + while row <= 3 + matrix[row][c] == 0 ? push!(testmat,1) : push!(testmat,0) + row += 1 + end + testval = binary_to_decimal(testmat); + if testval > ov + for x in (1:3) + matrix[x][c] = testmat[x] + end + @printf("Toggled column %d\n",c); + ShowMatrix(); + end +end + + +function binary_to_decimal(binary_array) + decimal = 0 + power = length(binary_array) - 1 + for digit in binary_array + decimal += digit * ( 2 ^ power) + power -= 1 + end + return decimal +end + +function ShowMatrix() + total = 0 + cnt = 1 + while cnt <= 3 + @printf("%s\n",matrix[cnt]) + total += binary_to_decimal(matrix[cnt]) + cnt += 1 + end + @printf("Total = %d\n\n",total) +end + +function GetColVal(c) + testmat = [] + row = 1 + while row <= 3 + push!(testmat,matrix[row][c]) + row += 1 + end + colval = binary_to_decimal(testmat) + return colval +end + +ShowMatrix() +for myrow in (1:3) + bd = binary_to_decimal(matrix[myrow]) + ToggleRow(myrow,bd) +end + + +for mycol in (1:4) + ToggleCol(mycol) +end + +#= +#--------------------------------------------- +SAMPLE OUTPUT +julia .\MatrixScore.jl + +[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 + +#--------------------------------------------- +=# + + 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 |
