diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-12-24 08:40:55 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-24 08:40:55 +0000 |
| commit | 9af7a6fb5c30a5d3813c99d8c16f22ea0a10f673 (patch) | |
| tree | dedb149ca155f9edef7752a2b68c56a1611a139b /challenge-196 | |
| parent | 03a5b2e285225643226df71df1370033e195f9e0 (diff) | |
| parent | 1c0933f0c2594679a6396cd86c5455b81d455ac7 (diff) | |
| download | perlweeklychallenge-club-9af7a6fb5c30a5d3813c99d8c16f22ea0a10f673.tar.gz perlweeklychallenge-club-9af7a6fb5c30a5d3813c99d8c16f22ea0a10f673.tar.bz2 perlweeklychallenge-club-9af7a6fb5c30a5d3813c99d8c16f22ea0a10f673.zip | |
Merge pull request #7299 from simongreen-net/master
Simon's solution to challenge 195 and 196
Diffstat (limited to 'challenge-196')
| -rw-r--r-- | challenge-196/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-196/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-196/sgreen/perl/ch-1.pl | 25 | ||||
| -rwxr-xr-x | challenge-196/sgreen/perl/ch-2.pl | 35 | ||||
| -rwxr-xr-x | challenge-196/sgreen/python/ch-1.py | 20 | ||||
| -rwxr-xr-x | challenge-196/sgreen/python/ch-2.py | 31 |
6 files changed, 114 insertions, 2 deletions
diff --git a/challenge-196/sgreen/README.md b/challenge-196/sgreen/README.md index f3687cb8b9..e446a70844 100644 --- a/challenge-196/sgreen/README.md +++ b/challenge-196/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 194 +# The Weekly Challenge 196 -Blog [Digital frequency](https://dev.to/simongreennet/digital-frequency-l56) +Blog [Weekly Challenge 196](https://dev.to/simongreennet/weekly-challenge-196-41j1) diff --git a/challenge-196/sgreen/blog.txt b/challenge-196/sgreen/blog.txt new file mode 100644 index 0000000000..8058983946 --- /dev/null +++ b/challenge-196/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-196-41j1
\ No newline at end of file diff --git a/challenge-196/sgreen/perl/ch-1.pl b/challenge-196/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..d79ed9232b --- /dev/null +++ b/challenge-196/sgreen/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use Algorithm::Combinatorics 'combinations'; + +sub main (@n) { + # Work through all combinations of positions + my $iter = combinations( [ 0 .. $#n ], 3 ); + while ( my $x = $iter->next ) { + my ( $i, $j, $k ) = sort { $a <=> $b } @$x; + if ( $n[$i] < $n[$k] < $n[$j] ) { + say "($n[$i], $n[$j], $n[$k])"; + return; + } + } + + # No solution is found + say '()'; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-196/sgreen/perl/ch-2.pl b/challenge-196/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..1df0c4fc49 --- /dev/null +++ b/challenge-196/sgreen/perl/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@n) { + my @solutions = (); + + # Go through the list until it is exhausted + while ( scalar(@n) ) { + my $start = my $end = shift @n; + + # See if the next number is one more than the last value + while ( scalar(@n) > 0 and $n[0] == $end + 1 ) { + $end = shift @n; + } + + # We have found a range + if ( $start != $end ) { + push @solutions, "[$start,$end]"; + } + } + + # Print solution + if (@solutions) { + say join ', ', @solutions; + } + else { + say 'No solutions found!'; + } +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-196/sgreen/python/ch-1.py b/challenge-196/sgreen/python/ch-1.py new file mode 100755 index 0000000000..57b37a70da --- /dev/null +++ b/challenge-196/sgreen/python/ch-1.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 + +from itertools import combinations +import sys + +def main(n): + # Work through all combinations of positions + for x in combinations(range(len(n)), 3): + i, j, k = sorted(x) + if n[i] < n[k] < n[j]: + print(f'({n[i]}, {n[j]}, {n[k]})') + return + + # No solution is found + print('()') + +if __name__ == '__main__': + # Turn the strings into integers + n = [int(i) for i in sys.argv[1:]] + main(n) diff --git a/challenge-196/sgreen/python/ch-2.py b/challenge-196/sgreen/python/ch-2.py new file mode 100755 index 0000000000..ce0f95cd62 --- /dev/null +++ b/challenge-196/sgreen/python/ch-2.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +import sys + + +def main(n): + solutions = [] + + # Go through the list until it is exhausted + while len(n): + start = end = n.pop(0) + + # See if the next number is one more than the last value + while len(n) > 0 and n[0] == end+1: + end = n.pop(0) + + # We have found a range + if start != end: + solutions.append(f'[{start},{end}]') + + # Print solution + if solutions: + print(*solutions, sep=', ') + else: + print('No solutions found!') + + +if __name__ == '__main__': + # Turn the strings into integers + n = [int(i) for i in sys.argv[1:]] + main(n) |
