diff options
| -rw-r--r-- | challenge-337/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-337/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-337/sgreen/perl/ch-1.pl | 21 | ||||
| -rwxr-xr-x | challenge-337/sgreen/perl/ch-2.pl | 46 | ||||
| -rwxr-xr-x | challenge-337/sgreen/python/ch-1.py | 34 | ||||
| -rwxr-xr-x | challenge-337/sgreen/python/ch-2.py | 63 | ||||
| -rwxr-xr-x | challenge-337/sgreen/python/test.py | 25 |
7 files changed, 192 insertions, 2 deletions
diff --git a/challenge-337/sgreen/README.md b/challenge-337/sgreen/README.md index 77aa3b753d..a207f68abf 100644 --- a/challenge-337/sgreen/README.md +++ b/challenge-337/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 336 +# The Weekly Challenge 337 -Blog: [Equalling the score](https://dev.to/simongreennet/weekly-challenge-equalling-the-score-5f2n) +Blog: [Oddly small](https://dev.to/simongreennet/weekly-challenge-oddly-small-1g9o) diff --git a/challenge-337/sgreen/blog.txt b/challenge-337/sgreen/blog.txt new file mode 100644 index 0000000000..0ab89ed8c4 --- /dev/null +++ b/challenge-337/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-oddly-small-1g9o
\ No newline at end of file diff --git a/challenge-337/sgreen/perl/ch-1.pl b/challenge-337/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..403110c537 --- /dev/null +++ b/challenge-337/sgreen/perl/ch-1.pl @@ -0,0 +1,21 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@numbers) { + my @solution = (); + + foreach my $number (@numbers) { + # Count how many numbers are smaller or equal than the current number. + # Subtract one to exclude the current number itself. + my $count = grep { $_ <= $number } @numbers; + push @solution, $count - 1; + } + + say '(' . join(', ', @solution) . ')'; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-337/sgreen/perl/ch-2.pl b/challenge-337/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..72252f72cb --- /dev/null +++ b/challenge-337/sgreen/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub point_score ( $row, $col, $point ) { + if ( $row == $point->[0] && $col == $point->[1] ) { + return 2; + } + if ( $row == $point->[0] || $col == $point->[1] ) { + return 1; + } + return 0; +} + +sub main (@ints) { + my $rows = shift @ints; + my $cols = shift @ints; + my @points = (); + for ( my $i = 0 ; $i < $#ints ; $i += 2 ) { + push @points, [ $ints[$i], $ints[ $i + 1 ] ]; + } + + my $odd_cells = 0; + + foreach my $row ( 0 .. $rows - 1 ) { + foreach my $col ( 0 .. $cols - 1 ) { + # Calculate the score for this cell + my $score = 0; + foreach my $point (@points) { + $score += point_score( $row, $col, $point ); + } + + # If the score is odd, increment the count of odd cells + if ( $score % 2 == 1 ) { + $odd_cells++; + } + } + } + + say $odd_cells; +} + +main(@ARGV); diff --git a/challenge-337/sgreen/python/ch-1.py b/challenge-337/sgreen/python/ch-1.py new file mode 100755 index 0000000000..565e8bfd00 --- /dev/null +++ b/challenge-337/sgreen/python/ch-1.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +import sys + + +def smaller_than_current(numbers: list) -> list: + """For each number in the list, count how many numbers are smaller than it. + + Args: + numbers: List of numbers. + + Returns: + A list of counts, where each count corresponds to how many numbers in the + input list are smaller than or equal to the number at that index. + """ + solution = [] + + for number in numbers: + # Count how many numbers are smaller or equal than the current number. + # Subtract one to exclude the current number itself. + solution.append(sum(1 for n in numbers if n <= number) - 1) + + return solution + + +def main(): + # Convert input into numbers + array = [float(n) for n in sys.argv[1:]] + result = smaller_than_current(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-337/sgreen/python/ch-2.py b/challenge-337/sgreen/python/ch-2.py new file mode 100755 index 0000000000..a6fb4e597c --- /dev/null +++ b/challenge-337/sgreen/python/ch-2.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 + +from dataclasses import dataclass +import sys + + +@dataclass +class Point: + """A point in the matrix.""" + x: int + y: int + + def score(self, row:int, col:int) -> int: + """Calculate the score for this point at the given row and column.""" + if row == self.x and col == self.y: + return 2 + if row == self.x or col == self.y: + return 1 + + return 0 + +def odd_matrix(rows:int, cols:int, points_list: list) -> int: + """Calculate the number of cells in the matrix with an odd score. + + Args: + rows: Number of rows in the matrix. + col: Number of columns in the matrix. + points_list: List of (x, y) tuples representing points in the matrix. + + Returns: + The number of cells in the matrix with an odd score. + """ + # Convert the list of points into Point objects + points = [Point(x, y) for x, y in points_list] + odd_cells = 0 + + for row in range(rows): + for col in range(cols): + # Calculate the score for this cell + score = 0 + for point in points: + score += point.score(row, col) + + # If the score is odd, increment the count of odd cells + if score % 2 == 1: + odd_cells += 1 + + return odd_cells + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + row = array.pop(0) + col = array.pop(0) + points = [(array[i], array[i + 1]) for i in range(0, len(array), 2)] + + result = odd_matrix(row, col, points) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-337/sgreen/python/test.py b/challenge-337/sgreen/python/test.py new file mode 100755 index 0000000000..8223307e20 --- /dev/null +++ b/challenge-337/sgreen/python/test.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +import unittest +ch_1 = __import__('ch-1') +ch_2 = __import__('ch-2') + + +class TestClass(unittest.TestCase): + def test_ch_1(self): + self.assertEqual(ch_1.smaller_than_current([6, 5, 4, 8]), [2, 1, 0, 3]) + self.assertEqual(ch_1.smaller_than_current([7, 7, 7, 7]), [3, 3, 3, 3]) + self.assertEqual(ch_1.smaller_than_current([5, 4, 3, 2, 1]), [4, 3, 2, 1, 0]) + self.assertEqual(ch_1.smaller_than_current([-1, 0, 3, -2, 1]), [1, 2, 4, 0, 3]) + self.assertEqual(ch_1.smaller_than_current([0, 1, 1, 2, 0]), [1, 3, 3, 4, 1]) + + def test_ch_2(self): + self.assertEqual(ch_2.odd_matrix(2, 3, [[0,1],[1,1]]), 6) + self.assertEqual(ch_2.odd_matrix(2, 2, [[1,1],[0,0]]), 0) + self.assertEqual(ch_2.odd_matrix(3, 3, [[0,0],[1,2],[2,1]]), 0) + self.assertEqual(ch_2.odd_matrix(1, 5, [[0,2],[0,4]]), 2) + self.assertEqual(ch_2.odd_matrix(4, 2, [[1,0],[3,1],[2,0],[0,1]]), 8) + + +if __name__ == '__main__': + unittest.main() |
