diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-05 11:09:35 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-05 11:09:35 +0000 |
| commit | 1516641772cf7b58d9dc4c172d1a2a23c22ec1a6 (patch) | |
| tree | 92b065f719b3b87ba69595524b265d8c818b34ea | |
| parent | 7d9930c228b5efce194374deaf9ddb87cafc1603 (diff) | |
| download | perlweeklychallenge-club-1516641772cf7b58d9dc4c172d1a2a23c22ec1a6.tar.gz perlweeklychallenge-club-1516641772cf7b58d9dc4c172d1a2a23c22ec1a6.tar.bz2 perlweeklychallenge-club-1516641772cf7b58d9dc4c172d1a2a23c22ec1a6.zip | |
Add Python solution to challenge 109
| -rw-r--r-- | challenge-109/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-109/paulo-custodio/python/ch-1.py | 40 | ||||
| -rw-r--r-- | challenge-109/paulo-custodio/python/ch-2.py | 58 | ||||
| -rwxr-xr-x | challenge-109/paulo-custodio/test.pl | 4 |
4 files changed, 100 insertions, 4 deletions
diff --git a/challenge-109/paulo-custodio/Makefile b/challenge-109/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-109/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-109/paulo-custodio/python/ch-1.py b/challenge-109/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..064ce7ab4d --- /dev/null +++ b/challenge-109/paulo-custodio/python/ch-1.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +# Challenge 109 +# +# TASK #1 - Chowla Numbers +# Submitted by: Mohammad S Anwar +# Write a script to generate first 20 Chowla Numbers, named after, +# Sarvadaman D. S. Chowla, a London born Indian American mathematician. +# It is defined as: +# +# C(n) = sum of divisors of n except 1 and n +# NOTE: Updated the above definition as suggested by Abigail [2021/04/19 18:40]. +# Output: +# 0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21 + +import sys +import math + +def divisors(n): + div_low = [] + div_high = [] + for i in range(1, int(math.sqrt(n)+1)): + if n%i==0: + div_low.append(i) + if n/i!=i: + div_high.append(int(n/i)) + div_high = div_high[::-1] + return [*div_low, *div_high] + +def chowla(n): + terms = filter(lambda x: x!=1 and x!=n, divisors(n)) + return sum(terms) + +def first_chowla(num): + nums = [] + for i in range(1, num+1): + nums.append(chowla(i)) + return nums + +print(", ".join([str(x) for x in first_chowla(int(sys.argv[1]))])) diff --git a/challenge-109/paulo-custodio/python/ch-2.py b/challenge-109/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..5fdd001d37 --- /dev/null +++ b/challenge-109/paulo-custodio/python/ch-2.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +# Challenge 109 +# +# TASK #2 - Four Squares Puzzle +# Submitted by: Mohammad S Anwar +# You are given four squares as below with numbers named a,b,c,d,e,f,g. +# +# (1) (3) +# +--------------+ +--------------+ +# | | | | +# | a | | e | +# | | (2) | | (4) +# | +---+------+---+ +---+---------+ +# | | | | | | | | +# | | b | | d | | f | | +# | | | | | | | | +# | | | | | | | | +# +----------+---+ +---+------+---+ | +# | c | | g | +# | | | | +# | | | | +# +--------------+ +-------------+ +# Write a script to place the given unique numbers in the square box so that sum +# of numbers in each box is the same. +# +# Example +# Input: 1,2,3,4,5,6,7 +# +# Output: +# +# a = 6 +# b = 4 +# c = 1 +# d = 5 +# e = 2 +# f = 3 +# g = 7 +# +# Box 1: a + b = 6 + 4 = 10 +# Box 2: b + c + d = 4 + 1 + 5 = 10 +# Box 3: d + e + f = 5 + 2 + 3 = 10 +# Box 4: f + g = 3 + 7 = 10 + +import sys +from itertools import permutations + +# Note: return first solution found, not necessarily same as example +def place_numbers(nums): + for p in permutations(nums, 7): + a,b,c,d,e,f,g = p[0],p[1],p[2],p[3],p[4],p[5],p[6] + sum = a+b + if b + c + d == sum and d + e + f == sum and f + g == sum: + return p + +result = place_numbers([int(x) for x in sys.argv[1:]]) +for i in range(7): + print(chr(ord('a')+i)+" = "+str(result[i])) diff --git a/challenge-109/paulo-custodio/test.pl b/challenge-109/paulo-custodio/test.pl deleted file mode 100755 index ba6c37260b..0000000000 --- a/challenge-109/paulo-custodio/test.pl +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env perl -use Modern::Perl; -use Test::More; -require '../../challenge-001/paulo-custodio/test.pl'; |
