aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-11-15 15:01:29 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-11-15 15:01:29 +0000
commit71081ca1fbcd2a5c3c1330d732f9dec368010a41 (patch)
treee409f43832799c3dd0f31d4f578252ef1a5e1b44
parent3aad7c8ad8d5fbbe42d3f110e5e7d7a073b8f497 (diff)
downloadperlweeklychallenge-club-71081ca1fbcd2a5c3c1330d732f9dec368010a41.tar.gz
perlweeklychallenge-club-71081ca1fbcd2a5c3c1330d732f9dec368010a41.tar.bz2
perlweeklychallenge-club-71081ca1fbcd2a5c3c1330d732f9dec368010a41.zip
Add Python solution to challenge 139
-rw-r--r--challenge-139/paulo-custodio/python/ch-1.py30
-rw-r--r--challenge-139/paulo-custodio/python/ch-2.py70
2 files changed, 100 insertions, 0 deletions
diff --git a/challenge-139/paulo-custodio/python/ch-1.py b/challenge-139/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..a5404eede9
--- /dev/null
+++ b/challenge-139/paulo-custodio/python/ch-1.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+# TASK #1 > JortSort
+# Submitted by: Mohammad S Anwar
+# You are given a list of numbers.
+#
+# Write a script to implement JortSort. It should return true/false depending
+# if the given list of numbers are already sorted.
+#
+# Example 1:
+# Input: @n = (1,2,3,4,5)
+# Output: 1
+#
+# Since the array is sorted, it prints 1.
+# Example 2:
+# Input: @n = (1,3,2,4,5)
+# Output: 0
+#
+# Since the array is NOT sorted, it prints 0.
+
+import sys
+
+def jortsort(a):
+ sa = sorted(a)
+ for i in range(len(a)):
+ if a[i]!=sa[i]:
+ return False
+ return True
+
+print(1 if jortsort([int(x) for x in sys.argv[1:]]) else 0)
diff --git a/challenge-139/paulo-custodio/python/ch-2.py b/challenge-139/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..37fa045f25
--- /dev/null
+++ b/challenge-139/paulo-custodio/python/ch-2.py
@@ -0,0 +1,70 @@
+#!/usr/bin/env python3
+
+# TASK #2 > Long Primes
+# Submitted by: Mohammad S Anwar
+# Write a script to generate first 5 Long Primes.
+#
+# A prime number (p) is called Long Prime if (1/p) has an infinite decimal
+# expansion repeating every (p-1) digits.
+#
+# Example
+# 7 is a long prime since 1/7 = 0.142857142857...
+# The repeating part (142857) size is 6 i.e. one less than the prime number 7.
+#
+# Also 17 is a long prime since 1/17 = 0.05882352941176470588235294117647...
+# The repeating part (0588235294117647) size is 16 i.e. one less than the
+# prime number 17.
+#
+# Another example, 2 is not a long prime as 1/2 = 0.5.
+# There is no repeating part in this case.
+
+import sys
+import re
+from decimal import *
+
+getcontext().prec = 1000
+getcontext().rounding = ROUND_DOWN
+
+def is_prime(n):
+ if n <= 1:
+ return 0
+ elif n <= 3:
+ return 1
+ elif n % 2 == 0 or n % 3 == 0:
+ return 0
+ else:
+ for i in range(5, n+1, 6):
+ if i*i>n:
+ break
+ if n % i == 0 or n % (i+2) == 0:
+ return 0
+ return 1
+
+def rept_sequence(n, max):
+ #print("rept_sequence", n, max)
+ for rept in range(1, max+1):
+ if re.search(r"\.(\d{"+str(rept)+r"})\1+", str(n)):
+ #print(rept)
+ return rept
+ #print(-1)
+ return -1
+
+def is_long_prime(p):
+ if not is_prime(p):
+ return False
+ inv = Decimal(1) / Decimal(p)
+ #print("check", inv, p, rept_sequence(inv, p-1))
+ if rept_sequence(inv, p-1)==p-1:
+ return True
+ else:
+ return False
+
+def print_long_primes(n):
+ p = 2
+ for i in range(n):
+ while not is_long_prime(p):
+ p += 1
+ print(p)
+ p += 1
+
+print_long_primes(int(sys.argv[1]))