aboutsummaryrefslogtreecommitdiff
path: root/challenge-149/paulo-custodio/python/ch-2.py
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-09-27 11:31:34 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-09-27 11:31:34 +0100
commit7de6f2107b0b6852a3210340e7e4d31de11df7a6 (patch)
tree26bd978c15e4493e8496da66d2b450060b980b32 /challenge-149/paulo-custodio/python/ch-2.py
parentdfd896477be36c3a279b8a1134e6ef3113b0f5b2 (diff)
downloadperlweeklychallenge-club-7de6f2107b0b6852a3210340e7e4d31de11df7a6.tar.gz
perlweeklychallenge-club-7de6f2107b0b6852a3210340e7e4d31de11df7a6.tar.bz2
perlweeklychallenge-club-7de6f2107b0b6852a3210340e7e4d31de11df7a6.zip
Add Python solution to challenge 149
Diffstat (limited to 'challenge-149/paulo-custodio/python/ch-2.py')
-rw-r--r--challenge-149/paulo-custodio/python/ch-2.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/challenge-149/paulo-custodio/python/ch-2.py b/challenge-149/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..b5c900633d
--- /dev/null
+++ b/challenge-149/paulo-custodio/python/ch-2.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+
+# Challenge 149
+#
+# TASK #2 > Largest Square
+# Submitted by: Roger Bell_West
+# Given a number base, derive the largest perfect square with no repeated
+# digits and return it as a string. (For base>10, use 'A'..'Z'.)
+#
+# Example:
+# f(2)="1"
+# f(4)="3201"
+# f(10)="9814072356"
+# f(12)="B8750A649321"
+
+import sys
+import math
+from itertools import permutations
+
+def largest_perfect_square(base):
+ digits = [str(i) for i in range(10)] + [chr(i) for i in range(ord('A'), ord('A') + 26)]
+ digits = digits[:base]
+
+ max_num = 0
+ max_str = "0"
+
+ for permu in permutations(digits, base):
+ str_num = ''.join(permu)
+ str_num = str_num.lstrip('0') or '0'
+ num = int(str_num, base)
+
+ if math.isqrt(num) ** 2 == num: # is perfect square
+ if num > max_num:
+ max_num, max_str = num, str_num
+
+ return max_str
+
+base = int(sys.argv[1])
+print(largest_perfect_square(base))