aboutsummaryrefslogtreecommitdiff
path: root/challenge-159
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-09-28 21:10:20 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-09-28 21:10:20 +0100
commit502be42efd777d4125a2dff0fbc73d3b10af3db4 (patch)
treebc97169323751a462d8660b00c09b5b86705818b /challenge-159
parent45ae6460d987618b7ca6016b89f7722c848893fa (diff)
downloadperlweeklychallenge-club-502be42efd777d4125a2dff0fbc73d3b10af3db4.tar.gz
perlweeklychallenge-club-502be42efd777d4125a2dff0fbc73d3b10af3db4.tar.bz2
perlweeklychallenge-club-502be42efd777d4125a2dff0fbc73d3b10af3db4.zip
Add Python solution to challenge 159
Diffstat (limited to 'challenge-159')
-rw-r--r--challenge-159/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-159/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-159/paulo-custodio/python/ch-1.py38
-rw-r--r--challenge-159/paulo-custodio/python/ch-2.py26
4 files changed, 66 insertions, 2 deletions
diff --git a/challenge-159/paulo-custodio/perl/ch-1.pl b/challenge-159/paulo-custodio/perl/ch-1.pl
index de8cf8b75c..4497c797be 100644
--- a/challenge-159/paulo-custodio/perl/ch-1.pl
+++ b/challenge-159/paulo-custodio/perl/ch-1.pl
@@ -2,7 +2,7 @@
# Challenge 159
#
-# TASK #1 › Farey Sequence
+# TASK #1 > Farey Sequence
# Submitted by: Mohammad S Anwar
# You are given a positive number, $n.
#
diff --git a/challenge-159/paulo-custodio/perl/ch-2.pl b/challenge-159/paulo-custodio/perl/ch-2.pl
index 0b8df98a0b..52a8efa892 100644
--- a/challenge-159/paulo-custodio/perl/ch-2.pl
+++ b/challenge-159/paulo-custodio/perl/ch-2.pl
@@ -2,7 +2,7 @@
# Challenge 159
#
-# TASK #2 › Moebius Number
+# TASK #2 > Moebius Number
# Submitted by: Mohammad S Anwar
# You are given a positive number $n.
#
diff --git a/challenge-159/paulo-custodio/python/ch-1.py b/challenge-159/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..70c306c5fa
--- /dev/null
+++ b/challenge-159/paulo-custodio/python/ch-1.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python3
+
+# Challenge 159
+#
+# TASK #1 > Farey Sequence
+# Submitted by: Mohammad S Anwar
+# You are given a positive number, $n.
+#
+# Write a script to compute Farey Sequence of the order $n.
+#
+# Example 1:
+# Input: $n = 5
+# Output: 0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1.
+# Example 2:
+# Input: $n = 7
+# Output: 0/1, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 2/5, 3/7, 1/2, 4/7, 3/5, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 1/1.
+# Example 3:
+# Input: $n = 4
+# Output: 0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1.
+
+from math import gcd
+import sys
+
+def farey_sequence(n):
+ seq = [(0, 1), (1, 1)] # first and last terms
+
+ for i in range(1, n + 1):
+ for j in range(i + 1, n + 1):
+ if gcd(i, j) == 1:
+ seq.append((i, j))
+
+ seq.sort(key=lambda x: x[0] / x[1])
+ seq = [f"{num[0]}/{num[1]}" for num in seq]
+
+ return seq
+
+n = int(sys.argv[1])
+print(", ".join(farey_sequence(n)))
diff --git a/challenge-159/paulo-custodio/python/ch-2.py b/challenge-159/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..2ca571f2a1
--- /dev/null
+++ b/challenge-159/paulo-custodio/python/ch-2.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python3
+
+# Challenge 159
+#
+# TASK #2 > Moebius Number
+# Submitted by: Mohammad S Anwar
+# You are given a positive number $n.
+#
+# Write a script to generate the Moebius Number for the given number.
+# Please refer to wikipedia page for more informations.
+#
+# Example 1:
+# Input: $n = 5
+# Output: -1
+# Example 2:
+# Input: $n = 10
+# Output: 1
+# Example 3:
+# Input: $n = 20
+# Output: 0
+
+from sympy.functions.combinatorial.numbers import mobius
+import sys
+
+n = int(sys.argv[1])
+print(mobius(n))