diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-01-01 12:05:48 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-01 12:05:48 +0000 |
| commit | 0ff44b94d710ec989ae3a2ad0808c9e9cadb7c81 (patch) | |
| tree | b430a0ef9f54dc79940c6f9983e4a1fb8c447987 | |
| parent | dda1c3b46db7c18d268777c109ef629818de3ab6 (diff) | |
| parent | 57a03de81450974ac22657aa149c6427105e3b87 (diff) | |
| download | perlweeklychallenge-club-0ff44b94d710ec989ae3a2ad0808c9e9cadb7c81.tar.gz perlweeklychallenge-club-0ff44b94d710ec989ae3a2ad0808c9e9cadb7c81.tar.bz2 perlweeklychallenge-club-0ff44b94d710ec989ae3a2ad0808c9e9cadb7c81.zip | |
Merge pull request #7331 from simongreen-net/master
Simon's solution to challenge 197
| -rw-r--r-- | challenge-197/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-197/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-197/sgreen/perl/ch-1.pl | 19 | ||||
| -rwxr-xr-x | challenge-197/sgreen/perl/ch-2.pl | 31 | ||||
| -rwxr-xr-x | challenge-197/sgreen/python/ch-1.py | 19 | ||||
| -rwxr-xr-x | challenge-197/sgreen/python/ch-2.py | 30 |
6 files changed, 102 insertions, 2 deletions
diff --git a/challenge-197/sgreen/README.md b/challenge-197/sgreen/README.md index e446a70844..e88b63ed82 100644 --- a/challenge-197/sgreen/README.md +++ b/challenge-197/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 196 +# The Weekly Challenge 197 -Blog [Weekly Challenge 196](https://dev.to/simongreennet/weekly-challenge-196-41j1) +Blog [Sorting Lists](https://dev.to/simongreennet/sorting-lists-35fh) diff --git a/challenge-197/sgreen/blog.txt b/challenge-197/sgreen/blog.txt new file mode 100644 index 0000000000..016b52b604 --- /dev/null +++ b/challenge-197/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/sorting-lists-35fh
\ No newline at end of file diff --git a/challenge-197/sgreen/perl/ch-1.pl b/challenge-197/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..526fba3ed8 --- /dev/null +++ b/challenge-197/sgreen/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main(@list) { + my @solution = ( + # The solution is all the non-zero values ... + (grep { $_ != 0 } @list), + # ... followed by the zero values + (grep { $_ == 0 } @list), + ); + + say join ', ', @solution; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-197/sgreen/perl/ch-2.pl b/challenge-197/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..cb24ef3b66 --- /dev/null +++ b/challenge-197/sgreen/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util qw(any mesh); + +sub main (@list) { + # Sort the list, find the mid point + @list = sort { $b <=> $a } @list; + my $mid_point = int( scalar(@list) / 2 ); + + # Split the list into big_n and small_n + my @big_list = @list[ 0 .. $mid_point - 1 ]; + my @small_list = @list[ $mid_point .. $#list ]; + + # Merge the lists together + my @solution = grep { defined($_) } mesh( \@small_list, \@big_list ); + + # There is no solution if two subsequent numbers are equal + if ( any { $solution[ $_ - 1 ] == $solution[$_] } ( 1 .. $#solution ) ) { + say 'No solution possible!'; + } + else { + say join ', ', @solution; + } +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-197/sgreen/python/ch-1.py b/challenge-197/sgreen/python/ch-1.py new file mode 100755 index 0000000000..f4a252ee4b --- /dev/null +++ b/challenge-197/sgreen/python/ch-1.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 + +import sys + + +def main(n): + # The solution is all the non-zero values ... + solution = [i for i in n if i != 0] + + # ... followed by the zero values + solution.extend([i for i in n if i == 0]) + + print(*solution, sep=', ') + + +if __name__ == '__main__': + # Turn the strings into integers + n = [int(i) for i in sys.argv[1:]] + main(n) diff --git a/challenge-197/sgreen/python/ch-2.py b/challenge-197/sgreen/python/ch-2.py new file mode 100755 index 0000000000..099fa0c9be --- /dev/null +++ b/challenge-197/sgreen/python/ch-2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +from itertools import chain, zip_longest +import sys + + +def main(n): + # Sort the list, find the mid point + n = sorted(n, reverse=True) + mid_point = len(n) // 2 + + # Split the list into big_n and small_n + big_n = n[:mid_point] + small_n = n[mid_point:] + + # Merge the lists together + solution = [i for i in chain( + *zip_longest(small_n, big_n)) if i is not None] + + # There is no solution if two subsequent numbers are equal + if any(solution[i-1] == solution[i] for i in range(1, len(solution))): + print('No solution possible!') + else: + print(*solution, sep=', ') + + +if __name__ == '__main__': + # Turn the strings into integers + n = [int(i) for i in sys.argv[1:]] + main(n) |
