aboutsummaryrefslogtreecommitdiff
path: root/challenge-004/paulo-custodio/python
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-06-07 00:10:37 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2021-06-07 00:10:37 +0100
commit3c31e63adaad68da1414fefd38eeb8e15c7127f7 (patch)
tree450c7d5e3df68e83aad83c0dff93470ec0639bf2 /challenge-004/paulo-custodio/python
parent6c06cf4efe57d194e090dffed9f1d77d8c8bbf77 (diff)
downloadperlweeklychallenge-club-3c31e63adaad68da1414fefd38eeb8e15c7127f7.tar.gz
perlweeklychallenge-club-3c31e63adaad68da1414fefd38eeb8e15c7127f7.tar.bz2
perlweeklychallenge-club-3c31e63adaad68da1414fefd38eeb8e15c7127f7.zip
Add python solution to challenge 004
Diffstat (limited to 'challenge-004/paulo-custodio/python')
-rwxr-xr-xchallenge-004/paulo-custodio/python/ch-1.py13
-rwxr-xr-xchallenge-004/paulo-custodio/python/ch-2.py40
2 files changed, 53 insertions, 0 deletions
diff --git a/challenge-004/paulo-custodio/python/ch-1.py b/challenge-004/paulo-custodio/python/ch-1.py
new file mode 100755
index 0000000000..c1f19cec8d
--- /dev/null
+++ b/challenge-004/paulo-custodio/python/ch-1.py
@@ -0,0 +1,13 @@
+#!/usr/bin/env python3
+
+# Challenge 004
+#
+# Challenge #1
+# Write a script to output the same number of PI digits as the size of your script.
+# Say, if your script size is 10, it should print 3.141592653.
+
+import math_pi # pip install math-pi
+import os;
+
+size = os.path.getsize(__file__)
+print(math_pi.pi(b=size-1)) # -1 to account for "3."
diff --git a/challenge-004/paulo-custodio/python/ch-2.py b/challenge-004/paulo-custodio/python/ch-2.py
new file mode 100755
index 0000000000..8cadfda8c8
--- /dev/null
+++ b/challenge-004/paulo-custodio/python/ch-2.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python3
+
+# Challenge 004
+#
+# Challenge #2
+# You are given a file containing a list of words (case insensitive 1 word per
+# line) and a list of letters. Print each word from the file that can be made
+# using only letters from the list. You can use each letter only once (though
+# there can be duplicates and you can use each of them once), you don't have to
+# use all the letters.
+# (Disclaimer: The challenge was proposed by Scimon Proctor)
+
+import sys
+import re
+
+def isalpha(word):
+ if re.fullmatch(r"[a-zA-Z]+", word):
+ return True
+ else:
+ return False
+
+def matches_letters(word, letters):
+ for c in letters:
+ word = re.sub(c, "", word, 1)
+ if word == "":
+ return True
+ return False
+
+def print_matching(file, letters):
+ letters = letters.lower()
+ fp = open(file, 'r')
+ for line in fp.readlines():
+ word = line.strip()
+ if isalpha(word) and len(word) >= 2 and matches_letters(word, letters):
+ print(word)
+
+if len(sys.argv) != 2:
+ print("Usage: ch-2.py letters")
+else:
+ print_matching("words.txt", sys.argv[1])