diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-03-27 12:10:32 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-27 12:10:32 +0100 |
| commit | 2b80f568952a64ce2a6247a23f99b5e5881176ec (patch) | |
| tree | 124ebdf3f006bcfaaf08174efdf6040828c9eeb1 /challenge-157 | |
| parent | cf25159f5b7beaf1bfb26bcb42179d185a95a7fa (diff) | |
| parent | 5d0404f5d1b944e2070a02b641f69d937c3f4472 (diff) | |
| download | perlweeklychallenge-club-2b80f568952a64ce2a6247a23f99b5e5881176ec.tar.gz perlweeklychallenge-club-2b80f568952a64ce2a6247a23f99b5e5881176ec.tar.bz2 perlweeklychallenge-club-2b80f568952a64ce2a6247a23f99b5e5881176ec.zip | |
Merge pull request #5839 from simongreen-net/master
sgreen solutions to challenge 157
Diffstat (limited to 'challenge-157')
| -rw-r--r-- | challenge-157/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-157/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-157/sgreen/perl/ch-1.pl | 21 | ||||
| -rwxr-xr-x | challenge-157/sgreen/perl/ch-2.pl | 48 | ||||
| -rwxr-xr-x | challenge-157/sgreen/python/ch-1.py | 24 | ||||
| -rwxr-xr-x | challenge-157/sgreen/python/ch-2.py | 41 |
6 files changed, 137 insertions, 2 deletions
diff --git a/challenge-157/sgreen/README.md b/challenge-157/sgreen/README.md index e8deece95f..57c0021858 100644 --- a/challenge-157/sgreen/README.md +++ b/challenge-157/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 152 +# The Weekly Challenge 157 -Blog: [Factorials everywhere!](https://dev.to/simongreennet/factorials-everywhere-5gga) +Blog: [Three means and big bases](https://dev.to/simongreennet/three-means-and-big-bases-3o2f)
\ No newline at end of file diff --git a/challenge-157/sgreen/blog.txt b/challenge-157/sgreen/blog.txt new file mode 100644 index 0000000000..7bc77415f7 --- /dev/null +++ b/challenge-157/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/three-means-and-big-bases-3o2f
\ No newline at end of file diff --git a/challenge-157/sgreen/perl/ch-1.pl b/challenge-157/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..771173ea6b --- /dev/null +++ b/challenge-157/sgreen/perl/ch-1.pl @@ -0,0 +1,21 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use List::Util qw(product reduce sum); + +sub main { + # Convert strings to integers, and get count of items + my @n = @_; + my $c = scalar(@n); + + # Calculate the different means + my $am = sum(@n) / $c; + my $gm = product(@n)**( 1 / $c ); + my $hm = $c / reduce { $a + ( $b == 0 ? 0 : 1 / $b ) } 0, @n; + + # Display results + printf "AM = %.1f, GM = %.1f, HM = %.1f\n", $am, $gm, $hm; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-157/sgreen/perl/ch-2.pl b/challenge-157/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..2b239268d0 --- /dev/null +++ b/challenge-157/sgreen/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub same_digits { + my ( $x, $b ) = @_; + + # Record the digit + my $d = undef; + + while ( $x != 0 ) { + my $this_digit = $x % $b; + if ( not defined $d ) { + # First (right most) digit + $d = $this_digit; + } + elsif ( $d != $this_digit ) { + # Check this digit matches the other one + return 0; + } + + # Divide x by the b allows us to calculate the next right most digit + $x = int( $x / $b ); + } + + return 1; +} + +sub main { + # Convert string to integer + my $n = shift; + + # Calculate for bases 2 to n-2 + foreach my $b ( 2 .. $n - 2 ) { + # Check for n in base b is all the same digits + if ( same_digits( $n, $b ) ) { + say "1 (base $b)"; + return; + } + } + + # Not a brazilian number + say 0; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-157/sgreen/python/ch-1.py b/challenge-157/sgreen/python/ch-1.py new file mode 100755 index 0000000000..1f1108582a --- /dev/null +++ b/challenge-157/sgreen/python/ch-1.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 + +from functools import reduce +from operator import mul +import math +import sys + + +def main(s): + # Convert strings to integers, and get count of items + n = [int(x) for x in s] + c = len(n) + + # Calculate the different means + am = sum(n) / c + gm = reduce(mul, n, 1) ** (1 / c) + hm = c / reduce(lambda x, y: x + (1/y if y != 0 else 0), list([0] + n)) + + # Display results + print(f"AM = { round(am, 1) }, GM = { round(gm, 1) }, HM = { round(hm, 1) }") + + +if __name__ == '__main__': + main(sys.argv[1:]) diff --git a/challenge-157/sgreen/python/ch-2.py b/challenge-157/sgreen/python/ch-2.py new file mode 100755 index 0000000000..c6fd81ceba --- /dev/null +++ b/challenge-157/sgreen/python/ch-2.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +import sys + + +def same_digits(x, b): + # Record the digit + d = None + + while x != 0: + this_digit = x % b + if d is None: + # First (right most) digit + d = this_digit + elif d != this_digit: + # Check this digit matches the other one + return False + + # Divide x by the b allows us to calculate the next right most digit + x = x // b + + return True + + +def main(s): + # Convert string to integer + n = int(s) + + # Calculate for bases 2 to n-2 + for b in range(2, n-1): + # Check for n in base b is all the same digits + if same_digits(n, b): + print(f"1 (base {b})") + return + + # Not a brazilian number + print(0) + + +if __name__ == '__main__': + main(sys.argv[1]) |
