diff options
| -rw-r--r-- | challenge-332/mahnkong/perl/ch-1.pl | 15 | ||||
| -rw-r--r-- | challenge-332/mahnkong/perl/ch-2.pl | 18 | ||||
| -rw-r--r-- | challenge-333/arne-sommer/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-333/arne-sommer/raku/ch-1.raku | 24 | ||||
| -rwxr-xr-x | challenge-333/arne-sommer/raku/ch-2.raku | 5 | ||||
| -rwxr-xr-x | challenge-333/arne-sommer/raku/duplicate-zeroes | 5 | ||||
| -rwxr-xr-x | challenge-333/arne-sommer/raku/straight-line | 24 | ||||
| -rw-r--r-- | challenge-333/mahnkong/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-333/mahnkong/perl/ch-2.pl | 22 | ||||
| -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 | ||||
| -rw-r--r-- | challenge-333/wambash/raku/ch-1.raku | 29 | ||||
| -rw-r--r-- | challenge-333/wambash/raku/ch-2.raku | 21 |
18 files changed, 357 insertions, 2 deletions
diff --git a/challenge-332/mahnkong/perl/ch-1.pl b/challenge-332/mahnkong/perl/ch-1.pl new file mode 100644 index 0000000000..9862eb6b68 --- /dev/null +++ b/challenge-332/mahnkong/perl/ch-1.pl @@ -0,0 +1,15 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run($str) { + if ($str =~ /(\d{4})-(\d{2})-(\d{2})/) { + return sprintf("%b-%b-%b", $1, $2, $3); + } + return undef; +} + +is(run('2025-07-26'), '11111101001-111-11010', "Example 1"); +is(run('2000-02-02'), '11111010000-10-10', "Example 2"); +is(run('2024-12-31'), '11111101000-1100-11111', "Example 3"); diff --git a/challenge-332/mahnkong/perl/ch-2.pl b/challenge-332/mahnkong/perl/ch-2.pl new file mode 100644 index 0000000000..dfaa7c6ff1 --- /dev/null +++ b/challenge-332/mahnkong/perl/ch-2.pl @@ -0,0 +1,18 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run($string) { + return undef unless $string; + my %occurences; + for my $c (split //, $string) { + $occurences{$c} += 1; + } + my @sorted = sort { $b <=> $a } values(%occurences); + return $sorted[0] == 1 ? 1 : 0; +} + +is(run("weekly"), 0, "Example 1"); +is(run("perl"), 1, "Example 2"); +is(run("challenge"), 0, "Example 3"); diff --git a/challenge-333/arne-sommer/blog.txt b/challenge-333/arne-sommer/blog.txt new file mode 100644 index 0000000000..37ac339a2c --- /dev/null +++ b/challenge-333/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/straight-zeros.html
\ No newline at end of file diff --git a/challenge-333/arne-sommer/raku/ch-1.raku b/challenge-333/arne-sommer/raku/ch-1.raku new file mode 100755 index 0000000000..99b2cceb2a --- /dev/null +++ b/challenge-333/arne-sommer/raku/ch-1.raku @@ -0,0 +1,24 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@list where @list.elems > 2 && all(@list) ~~ /^\d+\,\d+$/, + :v(:$verbose)); + +my ($Ax, $Ay) = @list.shift.split(",")>>.Int; +my ($Bx, $By) = @list.shift.split(",")>>.Int; + +while @list +{ + my ($Cx, $Cy) = @list.shift.split(",")>>.Int; + + say ": Checking if ($Ax,$Ay), ($Bx,$By), ($Cx,$Cy) forms a triangle" if $verbose; + + if $Ax * ($By - $Cy) + $Bx * ($Cy - $Ay) + $Cx * ($Ay - $By) + { + say ": trangle detected" if $verbose; + say False; + exit + } +} + +say True; + diff --git a/challenge-333/arne-sommer/raku/ch-2.raku b/challenge-333/arne-sommer/raku/ch-2.raku new file mode 100755 index 0000000000..b63febec22 --- /dev/null +++ b/challenge-333/arne-sommer/raku/ch-2.raku @@ -0,0 +1,5 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@ints where @ints.elems > 0 && all(@ints) ~~ Int); + +say @ints.map({ $_ ?? $_ !! (0,0) })[*;*][0 .. @ints.end]; diff --git a/challenge-333/arne-sommer/raku/duplicate-zeroes b/challenge-333/arne-sommer/raku/duplicate-zeroes new file mode 100755 index 0000000000..b63febec22 --- /dev/null +++ b/challenge-333/arne-sommer/raku/duplicate-zeroes @@ -0,0 +1,5 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@ints where @ints.elems > 0 && all(@ints) ~~ Int); + +say @ints.map({ $_ ?? $_ !! (0,0) })[*;*][0 .. @ints.end]; diff --git a/challenge-333/arne-sommer/raku/straight-line b/challenge-333/arne-sommer/raku/straight-line new file mode 100755 index 0000000000..99b2cceb2a --- /dev/null +++ b/challenge-333/arne-sommer/raku/straight-line @@ -0,0 +1,24 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@list where @list.elems > 2 && all(@list) ~~ /^\d+\,\d+$/, + :v(:$verbose)); + +my ($Ax, $Ay) = @list.shift.split(",")>>.Int; +my ($Bx, $By) = @list.shift.split(",")>>.Int; + +while @list +{ + my ($Cx, $Cy) = @list.shift.split(",")>>.Int; + + say ": Checking if ($Ax,$Ay), ($Bx,$By), ($Cx,$Cy) forms a triangle" if $verbose; + + if $Ax * ($By - $Cy) + $Bx * ($Cy - $Ay) + $Cx * ($Ay - $By) + { + say ": trangle detected" if $verbose; + say False; + exit + } +} + +say True; + diff --git a/challenge-333/mahnkong/perl/ch-1.pl b/challenge-333/mahnkong/perl/ch-1.pl new file mode 100644 index 0000000000..72c77eff46 --- /dev/null +++ b/challenge-333/mahnkong/perl/ch-1.pl @@ -0,0 +1,27 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run(@coordinates) { + my $diff_x; + my $diff_y; + + return 1 if scalar(@coordinates) < 2; + + my ($x0, $y0) = @{$coordinates[0]}; + my ($x1, $y1) = @{$coordinates[1]}; + + for (my $i = 2; $i < scalar(@coordinates); $i++) { + my ($x, $y) = @{$coordinates[$i]}; + + return 0 if ($y1-$y0)*($x-$x0) != ($y-$y0)*($x1-$x0); + } + return 1; +} + +is(run([2, 1], [2, 3], [2, 5]), 1, "Example 1"); +is(run([1, 4], [3, 4], [10, 4]), 1, "Example 2"); +is(run([0, 0], [1, 1], [2, 3]), 0, "Example 3"); +is(run([1, 1], [1, 1], [1, 1]), 1, "Example 4"); +is(run([1000000, 1000000], [2000000, 2000000], [3000000, 3000000]), 1, "Example 5"); diff --git a/challenge-333/mahnkong/perl/ch-2.pl b/challenge-333/mahnkong/perl/ch-2.pl new file mode 100644 index 0000000000..cd30b46d4a --- /dev/null +++ b/challenge-333/mahnkong/perl/ch-2.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run(@ints) { + for (my $i = 0; $i <= $#ints; $i++) { + if ($ints[$i] == 0 && exists($ints[$i + 1])) { + for (my $j = $#ints; $j > $i; $j--) { + $ints[$j] = $ints[$j-1]; + } + $i+=1; + } + } + return \@ints; +} + +is_deeply(run(1, 0, 2, 3, 0, 4, 5, 0), [1, 0, 0, 2, 3, 0, 0, 4], "Example 1"); +is_deeply(run(1, 2, 3), [1, 2, 3], "Example 2"); +is_deeply(run(1, 2, 3, 0), [1, 2, 3, 0], "Example 3"); +is_deeply(run(0, 0, 1, 2), [0, 0, 0, 0], "Example 4"); +is_deeply(run(1, 2, 0, 3, 4), [1, 2, 0, 0, 3], "Example 5"); 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() diff --git a/challenge-333/wambash/raku/ch-1.raku b/challenge-333/wambash/raku/ch-1.raku new file mode 100644 index 0000000000..38641156f3 --- /dev/null +++ b/challenge-333/wambash/raku/ch-1.raku @@ -0,0 +1,29 @@ +#!/usr/bin/env raku + +sub straight-line (+@list) { + my ($M, $N) = @list.squish( with => &[eqv] ); + return True without $N; + + my $a=$M[0]-$N[0]; + my $b=$M[1]-$N[1]; + + @list + andthen .map: {$b * .[0] - $a * .[1]}\ + andthen [==] $_ +} + +multi MAIN (Bool :test($)!) { + use Test; + is straight-line((2,1),(2,3),(2,5)), True; + is straight-line((1,4),(3,4),(10,4)), True; + is straight-line((0,0),(1,1),(2,3)), False; + is straight-line((1,1),(2,2),(3,3)), True; + is straight-line((1,1),(1,1),(1,1)), True; + is straight-line((1,1),(1,1),(2,2),(3,4)), False; + is straight-line((1_000_000,1_000_000),(2_000_000,2_000_000),(3_000_000,3_000_000)), True; + done-testing; +} + +multi MAIN (+list) { + say straight-line list.map: *.split(',').cache; +} diff --git a/challenge-333/wambash/raku/ch-2.raku b/challenge-333/wambash/raku/ch-2.raku new file mode 100644 index 0000000000..10110f03ff --- /dev/null +++ b/challenge-333/wambash/raku/ch-2.raku @@ -0,0 +1,21 @@ +#!/usr/bin/env raku + +sub duplicate-zeros (+ints) { + ints + andthen .map: { $_ == 0 ?? slip 0,0 !! $_ }\ + andthen .head: ints.elems +} + +multi MAIN (Bool :test($)!) { + use Test; + is duplicate-zeros(1, 0, 2, 3, 0, 4, 5, 0), (1, 0, 0, 2, 3, 0, 0, 4); + is duplicate-zeros(1, 2, 3), (1, 2, 3,); + is duplicate-zeros(1, 2, 3, 0), (1, 2, 3,0); + is duplicate-zeros(0,0,1, 2), (0,0,0,0); + is duplicate-zeros(1, 2, 0, 3,4), (1, 2, 0,0,3); + done-testing; +} + +multi MAIN (+ints) { + put duplicate-zeros ints; +} |
