From bcbb8e543ac9ad74cf70fb749a5f7f917e78abb2 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sat, 28 Sep 2024 19:57:44 +0100 Subject: Add Python solution to challenge 157 --- challenge-157/paulo-custodio/perl/ch-1.pl | 2 +- challenge-157/paulo-custodio/perl/ch-2.pl | 2 +- challenge-157/paulo-custodio/python/ch-1.py | 46 +++++++++++++++++++++++++ challenge-157/paulo-custodio/python/ch-2.py | 53 +++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 challenge-157/paulo-custodio/python/ch-1.py create mode 100644 challenge-157/paulo-custodio/python/ch-2.py diff --git a/challenge-157/paulo-custodio/perl/ch-1.pl b/challenge-157/paulo-custodio/perl/ch-1.pl index b6b2cdd4e6..36199d209e 100644 --- a/challenge-157/paulo-custodio/perl/ch-1.pl +++ b/challenge-157/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 157 # -# TASK #1 › Pythagorean Means +# TASK #1 > Pythagorean Means # Submitted by: Mohammad S Anwar # You are given a set of integers. # diff --git a/challenge-157/paulo-custodio/perl/ch-2.pl b/challenge-157/paulo-custodio/perl/ch-2.pl index 2c298e11a7..48af9d0de7 100644 --- a/challenge-157/paulo-custodio/perl/ch-2.pl +++ b/challenge-157/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 157 # -# TASK #2 › Brazilian Number +# TASK #2 > Brazilian Number # Submitted by: Mohammad S Anwar # You are given a number $n > 3. # diff --git a/challenge-157/paulo-custodio/python/ch-1.py b/challenge-157/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..726edff089 --- /dev/null +++ b/challenge-157/paulo-custodio/python/ch-1.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +# Challenge 157 +# +# TASK #1 > Pythagorean Means +# Submitted by: Mohammad S Anwar +# You are given a set of integers. +# +# Write a script to compute all three Pythagorean Means i.e Arithmetic Mean, +# Geometric Mean and Harmonic Mean of the given set of integers. Please refer +# to wikipedia page for more informations. +# +# Example 1: +# Input: @n = (1,3,5,6,9) +# Output: AM = 4.8, GM = 3.8, HM = 2.8 +# CORRECTION [2022-03-21 16:35] GM = 3.9 (before) +# +# Example 2: +# Input: @n = (2,4,6,8,10) +# Output: AM = 6.0, GM = 5.2, HM = 4.4 +# Example 3: +# Input: @n = (1,2,3,4,5) +# Output: AM = 3.0, GM = 2.6, HM = 2.2 + +import sys +import numpy as np + +def am(n): + return np.sum(n) / len(n) + +def gm(n): + base = abs(np.prod(n)) + exp = 1 / len(n) + return base ** exp + +def hm(n): + invn = [1/x for x in n] + return len(invn) / np.sum(invn) + +def f(n): + return f"{n:.1f}" + +n = list(map(float, sys.argv[1:])) +if not n: + raise ValueError("Usage: ch-1.py n...") +print(f"AM = {f(am(n))}, GM = {f(gm(n))}, HM = {f(hm(n))}") diff --git a/challenge-157/paulo-custodio/python/ch-2.py b/challenge-157/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..9da6f097cb --- /dev/null +++ b/challenge-157/paulo-custodio/python/ch-2.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +# Challenge 157 +# +# TASK #2 > Brazilian Number +# Submitted by: Mohammad S Anwar +# You are given a number $n > 3. +# +# Write a script to find out if the given number is a Brazilian Number. +# +# A positive integer number N has at least one natural number B where +# 1 < B < N-1 where the representation of N in base B has same digits. +# +# +# Example 1: +# Input: $n = 7 +# Output: 1 +# +# Since 7 in base 2 is 111. +# Example 2: +# Input: $n = 6 +# Output: 0 +# +# Since 6 in base 2 is 110, +# 6 in base 3 is 20 and +# 6 in base 4 is 12. +# Example 3: +# Input: $n = 8 +# Output: 1 +# +# Since 8 in base 3 is 22. + +import sys +from math import log + +def cnv(n, base): + if n == 0: + return '0' + digits = [] + while n: + digits.append(int(n % base)) + n //= base + return ''.join(str(x) for x in digits[::-1]) + +def is_brazilian(n): + for b in range(2, n - 1): + cnv_value = cnv(n, b) + if all(c == cnv_value[0] for c in cnv_value): + return True + return False + +n = int(sys.argv[1]) +print(1 if is_brazilian(n) else 0) -- cgit