diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-10-15 09:43:39 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-15 09:43:39 +0100 |
| commit | b0820a156498ed7818f1836123cfcb5014e2df7e (patch) | |
| tree | a53076956b8bb85c64e26d0a321f5c298e70b3c9 | |
| parent | 988fad6a7e38a53dccb0a5d76a01e9414418ff7e (diff) | |
| parent | c15fde33eb7752df67d677818ec5f878fb00e98b (diff) | |
| download | perlweeklychallenge-club-b0820a156498ed7818f1836123cfcb5014e2df7e.tar.gz perlweeklychallenge-club-b0820a156498ed7818f1836123cfcb5014e2df7e.tar.bz2 perlweeklychallenge-club-b0820a156498ed7818f1836123cfcb5014e2df7e.zip | |
Merge pull request #8869 from simongreen-net/master
Simon's solution to challenge 238
| -rw-r--r-- | challenge-238/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-238/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-238/sgreen/perl/ch-1.pl | 20 | ||||
| -rwxr-xr-x | challenge-238/sgreen/perl/ch-2.pl | 29 | ||||
| -rwxr-xr-x | challenge-238/sgreen/python/ch-1.py | 20 | ||||
| -rwxr-xr-x | challenge-238/sgreen/python/ch-2.py | 27 |
6 files changed, 99 insertions, 2 deletions
diff --git a/challenge-238/sgreen/README.md b/challenge-238/sgreen/README.md index 8aa6cc5481..aab15c5d06 100644 --- a/challenge-238/sgreen/README.md +++ b/challenge-238/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 237 +# The Weekly Challenge 238 -Blog: [Seize the greatness](https://dev.to/simongreennet/seize-the-greatness-38n0) +Blog: [Counting and sorting](https://dev.to/simongreennet/counting-and-sorting-4136) diff --git a/challenge-238/sgreen/blog.txt b/challenge-238/sgreen/blog.txt new file mode 100644 index 0000000000..36ba2e88e5 --- /dev/null +++ b/challenge-238/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/counting-and-sorting-4136
\ No newline at end of file diff --git a/challenge-238/sgreen/perl/ch-1.pl b/challenge-238/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..25619e139d --- /dev/null +++ b/challenge-238/sgreen/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@ints) { + my $total = 0; + my @solution = (); + + foreach my $i (@ints) { + $total += $i; + push @solution, $total; + } + + say '(', join( ', ', @solution ), ')'; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-238/sgreen/perl/ch-2.pl b/challenge-238/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..67b7779915 --- /dev/null +++ b/challenge-238/sgreen/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'product'; + +sub main (@ints) { + my %steps = (); + foreach my $i (@ints) { + # Calculate the steps required for each int to get a single digit + $steps{$i} = 0; + my $num = abs($i); + + while ( length($num) > 1 ) { + $num = product( split //, $num ); + $steps{$i}++; + } + } + + # Sort the integers in ascender order, and then by the steps required + my @sorted_ints = sort { $steps{$a} <=> $steps{$b} || $a <=> $b } @ints; + + say '(', join( ', ', @sorted_ints ), ')'; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-238/sgreen/python/ch-1.py b/challenge-238/sgreen/python/ch-1.py new file mode 100755 index 0000000000..09f05071b7 --- /dev/null +++ b/challenge-238/sgreen/python/ch-1.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 + +import sys + + +def main(ints): + total = 0 + solution = [] + + for i in ints: + total += i + solution.append(total) + + print('(' + ', '.join(map(str, solution)) + ')') + + +if __name__ == '__main__': + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + main(array) diff --git a/challenge-238/sgreen/python/ch-2.py b/challenge-238/sgreen/python/ch-2.py new file mode 100755 index 0000000000..1f5fbb6ff0 --- /dev/null +++ b/challenge-238/sgreen/python/ch-2.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +from math import prod +import sys + + +def main(ints): + steps = {} + for i in ints: + # Calculate the steps required for each int to get a single digit + steps[i] = 0 + num = abs(i) + while len(str(num)) > 1: + num = prod(map(int, str(num))) + steps[i] += 1 + + # Sort the integers in ascender order, and then by the steps required + sorted_ints = sorted(ints) + sorted_ints = sorted(sorted_ints, key=lambda i: steps[i]) + + print('(' + ', '.join(map(str, sorted_ints)) + ')') + + +if __name__ == '__main__': + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + main(array) |
