diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-09-24 18:17:45 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-24 18:17:45 +0100 |
| commit | a6ec10fbc389cb458109e32e50827f59566069d1 (patch) | |
| tree | c0b3a13d3059064d4093b544abfcaf5293f925fd | |
| parent | cf9e5773c3e26f372aaf139c3345fad483ee7323 (diff) | |
| parent | b91b39eb680fddcb1539c1df0a4f3e856d4be8b3 (diff) | |
| download | perlweeklychallenge-club-a6ec10fbc389cb458109e32e50827f59566069d1.tar.gz perlweeklychallenge-club-a6ec10fbc389cb458109e32e50827f59566069d1.tar.bz2 perlweeklychallenge-club-a6ec10fbc389cb458109e32e50827f59566069d1.zip | |
Merge pull request #8757 from simongreen-net/master
Simon's solution to challenge 235
| -rw-r--r-- | challenge-235/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-235/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-235/sgreen/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-235/sgreen/perl/ch-2.pl | 17 | ||||
| -rwxr-xr-x | challenge-235/sgreen/python/ch-1.py | 26 | ||||
| -rwxr-xr-x | challenge-235/sgreen/python/ch-2.py | 25 |
6 files changed, 102 insertions, 1 deletions
diff --git a/challenge-235/sgreen/README.md b/challenge-235/sgreen/README.md index 2444f94ca5..f06edcdb27 100644 --- a/challenge-235/sgreen/README.md +++ b/challenge-235/sgreen/README.md @@ -1 +1,3 @@ -# The Weekly Challenge 234 +# The Weekly Challenge 235 + +Blog: [Adding and removing integers](https://dev.to/simongreennet/adding-and-removing-integers-3i4g) diff --git a/challenge-235/sgreen/blog.txt b/challenge-235/sgreen/blog.txt new file mode 100644 index 0000000000..1ae72f6038 --- /dev/null +++ b/challenge-235/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/adding-and-removing-integers-3i4g
\ No newline at end of file diff --git a/challenge-235/sgreen/perl/ch-1.pl b/challenge-235/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..1c9a29a82e --- /dev/null +++ b/challenge-235/sgreen/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'all'; + +sub main (@ints) { + # Loop through each position of the list + foreach my $i ( 0 .. $#ints ) { + # Make a new list with the integer at that position remove + my @new_list = @ints; + splice( @new_list, $i, 1 ); + + # Check if the list is incremental + if ( all { $new_list[ $_ - 1 ] <= $new_list[$_] } ( 1 .. $#new_list ) ) + { + # It is, tell the user about it + say 'true'; + return; + } + } + + # Oh dear. No solution is possible + say 'false'; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-235/sgreen/perl/ch-2.pl b/challenge-235/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..53de7bd345 --- /dev/null +++ b/challenge-235/sgreen/perl/ch-2.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@ints) { + # For zeros in the list, double it + my @solution = map { $_ ? $_ : ( 0, 0 ) } @ints; + + # Truncate the list, and print it + splice( @solution, scalar(@ints) ); + say join ', ', @solution; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-235/sgreen/python/ch-1.py b/challenge-235/sgreen/python/ch-1.py new file mode 100755 index 0000000000..59c3e710c9 --- /dev/null +++ b/challenge-235/sgreen/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +import sys + + +def main(ints): + # Loop through each position of the list + for i in range(len(ints)): + # Make a new list with the integer at that position remove + new_list = ints.copy() + del new_list[i] + + # Check if the list is incremental + if all(new_list[j-1] <= new_list[j] for j in range(1, len(new_list))): + # It is, tell the user about it + print('true') + return + + # Oh dear. No solution is possible + print('false') + + +if __name__ == '__main__': + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + main(array) diff --git a/challenge-235/sgreen/python/ch-2.py b/challenge-235/sgreen/python/ch-2.py new file mode 100755 index 0000000000..068f507cf2 --- /dev/null +++ b/challenge-235/sgreen/python/ch-2.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +import sys + + +def main(ints): + solution = [] + + # Work through the list + for i in ints: + # Add the original number + solution.append(i) + if i == 0: + # Add it twice if it is zero + solution.append(i) + + # Truncate list, and print it + solution = solution[:len(ints)] + print(*solution, sep=', ') + + +if __name__ == '__main__': + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + main(array) |
