aboutsummaryrefslogtreecommitdiff
path: root/challenge-109/colin-crain/python
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-04-26 09:15:20 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-04-26 09:15:20 +0100
commit03f28cae3ddea3b08a671dd3f20f3d32777aa4db (patch)
tree7aea1f86e706cf8233d89d704ac2342a0c63d059 /challenge-109/colin-crain/python
parent46b8aecc9397c6211a1e97a7f0638833726294a2 (diff)
parent1ff197d81f941c3dd35d77bec8a0326807e8d2b1 (diff)
downloadperlweeklychallenge-club-03f28cae3ddea3b08a671dd3f20f3d32777aa4db.tar.gz
perlweeklychallenge-club-03f28cae3ddea3b08a671dd3f20f3d32777aa4db.tar.bz2
perlweeklychallenge-club-03f28cae3ddea3b08a671dd3f20f3d32777aa4db.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-109/colin-crain/python')
-rw-r--r--challenge-109/colin-crain/python/ch-1.py36
-rw-r--r--challenge-109/colin-crain/python/ch-2.py92
2 files changed, 128 insertions, 0 deletions
diff --git a/challenge-109/colin-crain/python/ch-1.py b/challenge-109/colin-crain/python/ch-1.py
new file mode 100644
index 0000000000..554f5b5518
--- /dev/null
+++ b/challenge-109/colin-crain/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python3
+#
+#
+# chowla.py
+#
+# 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
+#
+# Output:
+# 0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21
+#
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+import math
+
+def sum_divisors (n):
+ out = 0
+ for d in range( 2, int(math.sqrt(n))+1 ):
+ id = divmod(n,d)
+ if id[1] == 0:
+ out += d
+ if id[0] != d:
+ out += id[0]
+ return out
+
+
+for n in range(1,21):
+ print(f"C({n}) = ", sum_divisors(n))
+
+
diff --git a/challenge-109/colin-crain/python/ch-2.py b/challenge-109/colin-crain/python/ch-2.py
new file mode 100644
index 0000000000..3d45b655a7
--- /dev/null
+++ b/challenge-109/colin-crain/python/ch-2.py
@@ -0,0 +1,92 @@
+#!/usr/bin/env python3
+#
+#
+# four-sq-permute.py
+#
+# 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
+#
+#
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+from itertools import permutations
+
+
+
+def find_solutions ( lst ):
+ out = []
+ for candidate in list( permutations(lst) ):
+ (v, n) = validate( candidate )
+ if v:
+ out.append( [candidate, n] )
+ return out
+
+def validate ( lst ):
+ n = lst[0] + lst[1]
+ return ( (n == lst[1] + lst[2] + lst[3]
+ == lst[3] + lst[4] + lst[5]
+ == lst[5] + lst[6]), n )
+
+def report ( tup ):
+ (list, n) = tup
+
+ print(f'''
+ ===============================
+ solution
+ sum is {n}
+ values:
+ ''')
+
+ letter_values = zip( ['a','b','c','d','e','f','g'] , list )
+ for lv in letter_values:
+ print(f"\t\t{lv[0]} = {lv[1]}")
+
+ print(f'''
+ ===============================
+ ''')
+
+input = [1,2,3,4,5,6,7]
+solution = find_solutions(input)
+if len(solution) > 0:
+ for sol in solution:
+ report(sol)