diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-08-11 00:22:04 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-11 00:22:04 +0100 |
| commit | 843ff8693b58afca1e467fb1163c91e413a5ec9b (patch) | |
| tree | 917a3dca3337826e8a72a15e28e3e50a119b7db3 | |
| parent | 82321caa0fa61341410bbafe4f86882c67e922ef (diff) | |
| parent | 576c0a2b89712a98601c11f5233e65f5917ba34e (diff) | |
| download | perlweeklychallenge-club-843ff8693b58afca1e467fb1163c91e413a5ec9b.tar.gz perlweeklychallenge-club-843ff8693b58afca1e467fb1163c91e413a5ec9b.tar.bz2 perlweeklychallenge-club-843ff8693b58afca1e467fb1163c91e413a5ec9b.zip | |
Merge pull request #12490 from simongreen-net/master
sgreen solutions to challenge 333
| -rw-r--r-- | challenge-333/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-333/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-333/sgreen/perl/ch-1.pl | 44 | ||||
| -rwxr-xr-x | challenge-333/sgreen/perl/ch-2.pl | 20 | ||||
| -rwxr-xr-x | challenge-333/sgreen/python/ch-1.py | 42 | ||||
| -rwxr-xr-x | challenge-333/sgreen/python/ch-2.py | 25 | ||||
| -rwxr-xr-x | challenge-333/sgreen/python/test.py | 32 |
7 files changed, 166 insertions, 2 deletions
diff --git a/challenge-333/sgreen/README.md b/challenge-333/sgreen/README.md index 55f79903ab..8c45461c8c 100644 --- a/challenge-333/sgreen/README.md +++ b/challenge-333/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 332 +# The Weekly Challenge 333 -Blog: [I sent my date a letter](https://dev.to/simongreennet/weekly-challenge-i-sent-my-date-a-letter-1ppg) +Blog: [Duplicate Lines](https://dev.to/simongreennet/weekly-challenge-duplicate-lines-6pd) diff --git a/challenge-333/sgreen/blog.txt b/challenge-333/sgreen/blog.txt new file mode 100644 index 0000000000..fbb34fc573 --- /dev/null +++ b/challenge-333/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-duplicate-lines-6pd
\ No newline at end of file diff --git a/challenge-333/sgreen/perl/ch-1.pl b/challenge-333/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..6fc56f90a2 --- /dev/null +++ b/challenge-333/sgreen/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util qw(all any uniq); + +sub is_straight_line($points) { + # Check for a flat line (to avoid division by zero error) + if ( all { $points->[0][1] == $points->[$_][1] } ( 1 .. $#{$points} ) ) { + return 'true'; + } + + if ( any { $points->[0][1] == $points->[$_][1] } ( 1 .. $#{$points} ) ) { + return 'false'; + } + + my @degrees = ( + map { + abs( ( $points->[0][0] - $points->[$_][0] ) / + ( $points->[0][1] - $points->[$_][1] ) ) + } ( 1 .. $#$points ) + ); + + if ( scalar( uniq @degrees ) == 1 ) { + # All points are a straight line + return 'true'; + } + + return 'false'; +} + +sub main (@ints) { + my @points = (); + for ( my $i = 0 ; $i <= $#ints ; $i += 2 ) { + push @points, [ $ints[$i], $ints[ $i + 1 ] ]; + } + my $result = is_straight_line( \@points ); + say $result; +} + +main(@ARGV); diff --git a/challenge-333/sgreen/perl/ch-2.pl b/challenge-333/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..442346be46 --- /dev/null +++ b/challenge-333/sgreen/perl/ch-2.pl @@ -0,0 +1,20 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@ints) { + my @solution = (); + for my $i (@ints) { + if ($i == 0) { + # Duplicate the zero + push @solution, 0; + } + push @solution, $i; + } + say join(', ', @solution[ 0 .. $#ints ]); +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-333/sgreen/python/ch-1.py b/challenge-333/sgreen/python/ch-1.py new file mode 100755 index 0000000000..fa37695004 --- /dev/null +++ b/challenge-333/sgreen/python/ch-1.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +import sys + + +def straight_line(points: list[list[int]]) -> bool: + """ + Check if all points lie on a straight line. + + Args: + points (list[list[int]]): A list of points, where each point is represented as a list of two integers [x, y]. + + Returns: + bool: True if all points lie on a straight line, False otherwise. + """ + + # Check for a flat line (to avoid division by zero error) + if all(points[0][1] == points[i][1] for i in range(1, len(points))): + return True + + # Check for only some points being a flat line + if any(points[0][1] == points[i][1] for i in range(1, len(points))): + return False + + degrees = set( + abs((points[0][0] - points[i][0]) / (points[0][1] - points[i][1])) + for i in range(1, len(points)) + ) + + return True if len(degrees) == 1 else False + + +def main(): + # Convert input into integers, and then into pairs of points + array = [int(n) for n in sys.argv[1:]] + points = [[array[i], array[i + 1]] for i in range(0, len(array), 2)] + result = straight_line(points) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-333/sgreen/python/ch-2.py b/challenge-333/sgreen/python/ch-2.py new file mode 100755 index 0000000000..f0662513ed --- /dev/null +++ b/challenge-333/sgreen/python/ch-2.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +import sys + + +def duplicate_zeros(ints: list) -> list: + solution = [] + for i in ints: + if i == 0: + # Duplicate the zero + solution.append(0) + solution.append(i) + + return solution[:len(ints)] + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = duplicate_zeros(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-333/sgreen/python/test.py b/challenge-333/sgreen/python/test.py new file mode 100755 index 0000000000..be4233ee2b --- /dev/null +++ b/challenge-333/sgreen/python/test.py @@ -0,0 +1,32 @@ +#!/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.assertTrue(ch_1.straight_line([[2, 1], [2, 3], [2, 5]])) + self.assertTrue(ch_1.straight_line([[1, 4], [3, 4], [10, 4]])) + self.assertFalse(ch_1.straight_line([[0, 0], [1, 1], [2, 3]])) + self.assertTrue(ch_1.straight_line([[1, 1], [1, 1], [1, 1]])) + self.assertTrue( + ch_1.straight_line( + [[1000000, 1000000], [2000000, 2000000], [3000000, 3000000]] + ) + ) + + def test_ch_2(self): + self.assertEqual( + ch_2.duplicate_zeros([1, 0, 2, 3, 0, 4, 5, 0]), + [1, 0, 0, 2, 3, 0, 0, 4] + ) + self.assertEqual(ch_2.duplicate_zeros([1, 2, 3]), [1, 2, 3]) + self.assertEqual(ch_2.duplicate_zeros([1, 2, 3, 0]), [1, 2, 3, 0]) + self.assertEqual(ch_2.duplicate_zeros([0, 0, 1, 2]), [0, 0, 0, 0]) + self.assertEqual(ch_2.duplicate_zeros([1, 2, 0, 3, 4]), [1, 2, 0, 0, 3]) + + +if __name__ == '__main__': + unittest.main() |
