diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2022-12-27 14:03:32 -0600 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2022-12-27 14:03:32 -0600 |
| commit | d15252dde06495df359ebd2e176aae5a87803823 (patch) | |
| tree | 49b01aa30abcf4a59af3faf71e8c67fe8cafc638 | |
| parent | 1e7c6d15f46ca6982ed95154212e62f7b910f9ad (diff) | |
| parent | 9720dd7a3a5d6c22d6cc034c363d1d09e309e745 (diff) | |
| download | perlweeklychallenge-club-d15252dde06495df359ebd2e176aae5a87803823.tar.gz perlweeklychallenge-club-d15252dde06495df359ebd2e176aae5a87803823.tar.bz2 perlweeklychallenge-club-d15252dde06495df359ebd2e176aae5a87803823.zip | |
Merge branch 'master' of github.com:manwar/perlweeklychallenge-club into challenges
78 files changed, 4834 insertions, 2117 deletions
diff --git a/challenge-196/mohammad-anwar/perl/ch-2.pl b/challenge-196/mohammad-anwar/perl/ch-2.pl new file mode 100644 index 0000000000..3b0b339e73 --- /dev/null +++ b/challenge-196/mohammad-anwar/perl/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl + +=head1 + +Week 196: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-196 + +Task #2: Range List + + You are given a sorted unique integer array, @array. + + Write a script to find all possible Number Range i.e [x, y] + represent range all integers from x and y (both inclusive). + +=cut + +use v5.36; +use Test2::V0; + +is range_list(1,3,4,5,7), [[3,5]], 'Example 1'; +is range_list(1,2,3,6,7,9), [[1,3],[6,7]], 'Example 2'; +is range_list(0,1,2,4,5,6,8,9), [[0,2],[4,6],[8,9]], 'Example 3'; + +done_testing; + +# +# +# METHOD + +sub range_list(@list) { + my $start = shift @list; + my $curr = $start; + my @range = (); + + foreach my $next (@list) { + next if ($next == $curr); + if ($next == $curr + 1) { + $curr = $next; + } + else { + push @range, [$start, $curr] + if ($curr > $start); + $curr = $start = $next; + } + } + + push @range, [$start, $curr] + if ($curr > $start); + + return \@range; +} diff --git a/challenge-196/wambash/raku/ch-1.raku b/challenge-196/wambash/raku/ch-1.raku new file mode 100644 index 0000000000..cd867a9c2c --- /dev/null +++ b/challenge-196/wambash/raku/ch-1.raku @@ -0,0 +1,20 @@ +#!/usr/bin/env raku + +sub pattern132 (+@list) { + @list + andthen .combinations: 3 + andthen .first: -> ($i, $j, $k) { $i < $k < $j }\ +} + +multi MAIN (Bool :test($)!) { + use Test; + is pattern132(3,1,4,2),(1,4,2); + is pattern132(1,2,3,4), Nil; + is pattern132(1, 3, 2, 4, 6, 5), (1,3,2); + is pattern132(1, 3, 4, 2), (1,3,2); + done-testing; +} + +multi MAIN (*@list) { + say pattern132 @list +} diff --git a/challenge-196/wambash/raku/ch-2.raku b/challenge-196/wambash/raku/ch-2.raku new file mode 100644 index 0000000000..d4efe9f08e --- /dev/null +++ b/challenge-196/wambash/raku/ch-2.raku @@ -0,0 +1,29 @@ +#!/usr/bin/env raku + +sub range-list-reducer (Capture $a, Int $b) { + $a.tail.tail == $b.pred + ?? \( |$a.head(*-1), (|$a.tail,$b,) ) + !! \( |$a, $b ) +} + +sub range-list (+@list) { + @list + andthen \(.head,), |.skip + andthen .reduce: &range-list-reducer + andthen .grep: Positional + andthen .map: {.head .. .tail} +} + +multi MAIN (Bool :test($)!) { + use Test; + is-deeply range-list-reducer( \(1,(3,4)), 5 ), \(1,(3,4, 5)); + is-deeply range-list-reducer( \(1,(3,4)), 6 ), \(1,(3,4),6 ); + is-deeply range-list(1,3,4,5,7), (3..5,); + is-deeply range-list(1,2,3,6,7,9), (1..3,6..7); + is-deeply range-list(0,1,2,4,5,6,8,9), (0..2, 4..6, 8..9); + done-testing; +} + +multi MAIN (*@list) { + say range-list( @list».Int ) +} diff --git a/challenge-197/bob-lied/README b/challenge-197/bob-lied/README index 1eebe9fbd4..270b68a64d 100644 --- a/challenge-197/bob-lied/README +++ b/challenge-197/bob-lied/README @@ -1,3 +1,3 @@ -Solutions to weekly challenge 196 by Bob Lied +Solutions to weekly challenge 197 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-196/ +https://perlweeklychallenge.org/blog/perl-weekly-challenge-197/ diff --git a/challenge-197/bob-lied/perl/ch-1.pl b/challenge-197/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..18153180a6 --- /dev/null +++ b/challenge-197/bob-lied/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl +#============================================================================= +# Copyright (c) 2022, Bob Lied +#============================================================================= +# Perl Weekly Challenge Week 197, Task 1 Move Zero +# +# You are given a list of integers, @list. +# Write a script to move all zero, if exists, to the end while maintaining +# the relative order of non-zero elements. +# Example 1 Input: @list = (1, 0, 3, 0, 0, 5) +# Output: (1, 3, 5, 0, 0, 0) +# Example 2 Input: @list = (1, 6, 4) +# Output: (1, 6, 4) +# Example 3 Input: @list = (0, 1, 0, 2, 0 +# Output: (1, 2, 0, 0, 0) +# +#============================================================================= + +use v5.36; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub moveZero(@list) +{ + my @noZero = grep { $_ != 0 } @list; + return [ @noZero, (0) x ( scalar(@list)-scalar(@noZero) ) ]; +} + +sub runTest +{ + use Test::More; + + is_deeply( moveZero(1,0,3,0,0,5), [1,3,5,0,0,0], "Example 1"); + is_deeply( moveZero(1,6,4 ), [1,6,4 ], "Example 2"); + is_deeply( moveZero(0,1,0,2,0 ), [1,2,0,0,0 ], "Example 3"); + is_deeply( moveZero(0 ), [0 ], "Example 3"); + + done_testing; +} + diff --git a/challenge-197/bob-lied/perl/ch-2.pl b/challenge-197/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..9660429516 --- /dev/null +++ b/challenge-197/bob-lied/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl Perl Weekly Challenge Week 197 Task 2 Wiggle Sort +#============================================================================= +# Copyright (c) 2022, Bob Lied +#============================================================================= +# You are given a list of integers, @list. +# Write a script to perform Wiggle Sort on the given list. +# Wiggle sort would be such as list[0] < list[1] > list[2] < list[3]…. +# +# Example 1 Input: @list = (1,5,1,1,6,4) +# Output: (1,6,1,5,1,4) +# +# Example 2 Input: @list = (1,3,2,2,3,1) +# Output: (2,3,1,3,1,2) + + +# +#============================================================================= + +use v5.36; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub wiggleSort(@list) +{ + my @sl = sort { $a <=> $b } @list; + + # Subtract one for 0-based arrays + my $midpoint = int( scalar(@sl) / 2 ) - 1; + my @bottom = reverse @sl[0..$midpoint]; + my @top = reverse splice(@sl, $midpoint+1); + + my @result; + while ( @bottom && @top ) + { + push @result, (shift @bottom), (shift @top); + } + push @result, @top if @top; # Odd-sized lists + return \@result; +} + +sub runTest +{ + use Test::More; + + is_deeply( wiggleSort(1,5,1,1,6,4), [1,6,1,5,1,4], "Example 1"); + is_deeply( wiggleSort(1,3,2,2,3,1), [2,3,1,3,1,2], "Example 2"); + is_deeply( wiggleSort(1,3,2,2,3,1,4), [2,4,1,3,1,3,2], "Odd"); + + done_testing; +} + diff --git a/challenge-197/carlos-oliveira/perl/ch-1.pl b/challenge-197/carlos-oliveira/perl/ch-1.pl new file mode 100644 index 0000000000..61e4473855 --- /dev/null +++ b/challenge-197/carlos-oliveira/perl/ch-1.pl @@ -0,0 +1,14 @@ +use strict; +use warnings; + +use Data::Dump; + +sub moveZero { + my @listWithoutZeros = grep { $_ != 0 } @_; + return @listWithoutZeros, (0) x (@_ - @listWithoutZeros); +} + +dd moveZero 1, 0, 3, 0, 0, 5; +dd moveZero 1, 6, 4; +dd moveZero 0, 1, 0, 2, 0; + diff --git a/challenge-197/carlos-oliveira/perl/ch-2.pl b/challenge-197/carlos-oliveira/perl/ch-2.pl new file mode 100644 index 0000000000..6219c64d9c --- /dev/null +++ b/challenge-197/carlos-oliveira/perl/ch-2.pl @@ -0,0 +1,27 @@ +use strict; +use warnings; +use 5.36.0; +use Data::Dump; + + +sub wiggle; + + +dd wiggle 1,5,1,1,6,4; +dd wiggle 1,3,2,2,3,1; +dd wiggle 9,8,7,6,5,4,3,2,1; + + +sub wiggle { + my $originalLength = @_; + @_ = sort @_; + my $offset = int @_ / 2; + my @pivots = splice @_, -$offset, $offset; + + for my $i ( reverse 1 .. $#_ + ($originalLength % 2 == 0) ) { + my $pivot = pop @pivots; + splice @_, $i, 0, $pivot; + } + + return @_; +} diff --git a/challenge-197/feng-chang/raku/ch-1.raku b/challenge-197/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..8bab342a05 --- /dev/null +++ b/challenge-197/feng-chang/raku/ch-1.raku @@ -0,0 +1,8 @@ +#!/bin/env raku + +unit sub MAIN(*@N); + +for +@N ^... 0 -> \j { + @N.push(@N.splice(j, 1)) if @N[j] == 0; +} +put @N.join(', '); diff --git a/challenge-197/feng-chang/raku/ch-2.raku b/challenge-197/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..8eb42b696b --- /dev/null +++ b/challenge-197/feng-chang/raku/ch-2.raku @@ -0,0 +1,12 @@ +#!/bin/env raku + +unit sub MAIN(*@N); + +@N .= sort; +my @a = @N.splice((+@N + 1) div 2); + +loop (my $i = 1; +@a > 0; $i += 2) { + @N.splice($i, 0, @a.shift); +} + +put @N.join(', '); diff --git a/challenge-197/jeanluc2020/blog-1.txt b/challenge-197/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..07829acb5a --- /dev/null +++ b/challenge-197/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-197-1.html diff --git a/challenge-197/jeanluc2020/blog-2.txt b/challenge-197/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..0f3cc2503b --- /dev/null +++ b/challenge-197/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-197-2.html diff --git a/challenge-197/jeanluc2020/perl/ch-1.pl b/challenge-197/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..8197b5dd64 --- /dev/null +++ b/challenge-197/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-197/#TASK1 +# +# You are given a list of integers, @list. +# +# Write a script to move all zero, if exists, to the end while maintaining the relative order of non-zero elements. + +use strict; +use warnings; + +# sample input values +my $inputs = [ + [1, 0, 3, 0, 0, 5], + [1, 6, 4], + [0, 1, 0, 2, 0] +]; + +# handle all input arrays from sample list above +foreach my $input (@$inputs) { + print "(" . join(", ", @$input) . ") returns (" . join(", ", move_zero(@$input)) . ")\n"; +} + +# given a list of integers, return the same list with all zeros moved to the end +sub move_zero { + my @values = @_; + my @return = (); + my @tmp = (); + # collect all non-zero values into @return, all zero values into @tmp + foreach my $elem (@values) { + if($elem == 0) { + push @tmp, $elem; + } else { + push @return, $elem; + } + } + # add all zero values to the end of @return before returning + push @return, @tmp; + return @return; +} diff --git a/challenge-197/jeanluc2020/perl/ch-2.pl b/challenge-197/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..b5e0634d5f --- /dev/null +++ b/challenge-197/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-197/#TASK2 +# +# You are given a list of integers, @list. +# +# Write a script to perform Wiggle Sort on the given list. +# +# # Wiggle sort would be such as list[0] < list[1] > list[2] < list[3]... + +use strict; +use warnings; + +# sample input values +my $inputs = [ + [1,5,1,1,6,4], + [1,3,2,2,3,1], + [1,2,3,4,5], + [1,1,1,2,2,2,3,3,4,4], +]; + +# handle all input arrays from sample list above +foreach my $input (@$inputs) { + print "(" . join(", ", @$input) . ") returns (" . join(", ", wiggle_sort(@$input)) . ")\n"; +} + +# wiggle sort has to jump up and down every time +sub wiggle_sort { + # let's start by sorting all values + my @values = sort {$a <=> $b} @_; + my @result = (); + # by starting in the middle o |
