aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-10-03 21:45:02 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-10-03 21:45:02 +0100
commit14c600e5da23ba6cfc62cc668a85270cafaf8add (patch)
tree8cd7f99231ab339a0514d5fec5ff7d1d1f6db163
parent2d4a098d9d72c509ec2af2721d8a14ff5800fb08 (diff)
downloadperlweeklychallenge-club-14c600e5da23ba6cfc62cc668a85270cafaf8add.tar.gz
perlweeklychallenge-club-14c600e5da23ba6cfc62cc668a85270cafaf8add.tar.bz2
perlweeklychallenge-club-14c600e5da23ba6cfc62cc668a85270cafaf8add.zip
Add Python solution to challenge 174
-rw-r--r--challenge-174/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-174/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-174/paulo-custodio/python/ch-1.py36
-rw-r--r--challenge-174/paulo-custodio/python/ch-2.py66
4 files changed, 104 insertions, 2 deletions
diff --git a/challenge-174/paulo-custodio/perl/ch-1.pl b/challenge-174/paulo-custodio/perl/ch-1.pl
index 1b2ea1c24b..6b3d61e8d4 100644
--- a/challenge-174/paulo-custodio/perl/ch-1.pl
+++ b/challenge-174/paulo-custodio/perl/ch-1.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 174
#
diff --git a/challenge-174/paulo-custodio/perl/ch-2.pl b/challenge-174/paulo-custodio/perl/ch-2.pl
index 003d0c206b..c63f484ead 100644
--- a/challenge-174/paulo-custodio/perl/ch-2.pl
+++ b/challenge-174/paulo-custodio/perl/ch-2.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 174
#
diff --git a/challenge-174/paulo-custodio/python/ch-1.py b/challenge-174/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..26ccedb38c
--- /dev/null
+++ b/challenge-174/paulo-custodio/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python3
+
+# Challenge 174
+#
+# Task 1: Disarium Numbers
+# Submitted by: Mohammad S Anwar
+#
+# Write a script to generate first 19 Disarium Numbers.
+#
+# A disarium number is an integer where the sum of each digit raised to the
+# power of its position in the number, is equal to the number.
+#
+#
+# For example,
+#
+# 518 is a disarium number as (5 ** 1) + (1 ** 2) + (8 ** 3) => 5 + 1 + 512 => 518
+
+import sys
+from typing import List
+
+def is_disarium(n: int) -> bool:
+ digits = [int(d) for d in str(n)]
+ return sum(d ** (i + 1) for i, d in enumerate(digits)) == n
+
+def disarium_numbers(N: int) -> List[int]:
+ result = []
+ n = 1
+ while len(result) < N:
+ if is_disarium(n):
+ result.append(n)
+ n += 1
+ return result
+
+if len(sys.argv) != 2:
+ raise ValueError("usage: script.py n")
+print(", ".join(map(str, disarium_numbers(int(sys.argv[1])))))
diff --git a/challenge-174/paulo-custodio/python/ch-2.py b/challenge-174/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..0c89d3b1d9
--- /dev/null
+++ b/challenge-174/paulo-custodio/python/ch-2.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python3
+
+# Challenge 174
+#
+# Task 2: Permutation Ranking
+# Submitted by: Mohammad S Anwar
+#
+# You are given a list of integers with no duplicates, e.g. [0, 1, 2].
+#
+# Write two functions, permutation2rank() which will take the list and determine
+# its rank (starting at 0) in the set of possible permutations arranged in
+# lexicographic order, and rank2permutation() which will take the list and a rank
+# number and produce just that permutation.
+#
+# Please checkout this post for more informations and algorithm.
+#
+# Given the list [0, 1, 2] the ordered permutations are:
+#
+# 0: [0, 1, 2]
+# 1: [0, 2, 1]
+# 2: [1, 0, 2]
+# 3: [1, 2, 0]
+# 4: [2, 0, 1]
+# 5: [2, 1, 0]
+#
+# and therefore:
+#
+# permutation2rank([1, 0, 2]) = 2
+#
+# rank2permutation([0, 1, 2], 1) = [0, 2, 1]
+
+from math import factorial
+
+def fact(n):
+ return factorial(n)
+
+def permutation2rank(p):
+ n = len(p)
+ fact_n_minus_1 = fact(n - 1) # (n-1)!
+ rank = 0
+ digits = list(range(n)) # all unused digits
+ for i in range(n - 1):
+ q = next(index for index, value in enumerate(digits) if value == p[i])
+ rank += fact_n_minus_1 * q
+ digits.pop(q) # remove digit p[i]
+ fact_n_minus_1 //= (n - 1 - i) # weight of next digit
+ return rank
+
+def rank2permutation(p, rank):
+ n = len(p)
+ fact_n_minus_1 = fact(n - 1) # (n-1)!
+ digits = list(range(n)) # all unused digits
+ result = []
+ for i in range(n):
+ q = rank // fact_n_minus_1 # by decomposing rank = q * fact + rest
+ rank %= fact_n_minus_1
+ result.append(digits[q])
+ digits.pop(q) # remove digit at position q
+ if i != n - 1:
+ fact_n_minus_1 //= (n - 1 - i) # weight of next digit
+ return result
+
+# Testing
+print("ok 1" if permutation2rank([1, 0, 2]) == 2 else "nok 1")
+print("ok 2" if rank2permutation([0, 1, 2], 1) == [0, 2, 1] else "nok 2")
+print("1..2")