aboutsummaryrefslogtreecommitdiff
path: root/challenge-038
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2022-04-27 18:30:36 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2022-04-27 18:30:36 +0100
commitc6bbd320bc50a97da17f80a9b4e3cf26590a3574 (patch)
treefb2ac70197b5f020851e10cd18b0dc3894e5bfee /challenge-038
parent830204e938a5421f4d6d91d6cefcc48481da9dd2 (diff)
downloadperlweeklychallenge-club-c6bbd320bc50a97da17f80a9b4e3cf26590a3574.tar.gz
perlweeklychallenge-club-c6bbd320bc50a97da17f80a9b4e3cf26590a3574.tar.bz2
perlweeklychallenge-club-c6bbd320bc50a97da17f80a9b4e3cf26590a3574.zip
Add Python solution to challenge 038
Diffstat (limited to 'challenge-038')
-rw-r--r--challenge-038/paulo-custodio/python/ch-1.py50
-rw-r--r--challenge-038/paulo-custodio/python/ch-2.py79
2 files changed, 129 insertions, 0 deletions
diff --git a/challenge-038/paulo-custodio/python/ch-1.py b/challenge-038/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..d90e25f8bb
--- /dev/null
+++ b/challenge-038/paulo-custodio/python/ch-1.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python3
+
+# Challenge 038
+#
+# TASK #1
+# Date Finder
+# Create a script to accept a 7 digits number, where the first number can only
+# be 1 or 2. The second and third digits can be anything 0-9. The fourth and
+# fifth digits corresponds to the month i.e. 01,02,03...,11,12. And the last
+# 2 digits respresents the days in the month i.e. 01,02,03...29,30,31. Your
+# script should validate if the given number is valid as per the rule and then
+# convert into human readable format date.
+#
+# RULES
+# If 1st digit is 1, then prepend 20 otherwise 19 to the 2nd and 3rd digits to
+# make it 4-digits year.
+#
+# The 4th and 5th digits together should be a valid month.
+#
+# The 6th and 7th digits together should be a valid day for the above month.
+#
+# For example, the given number is 2230120, it should print 1923-01-20.
+
+from datetime import date
+from calendar import monthrange
+import re
+import sys
+
+input = sys.argv[1]
+if not (m := re.match(r"^([12])(\d\d)([01]\d)([0-3]\d)$", input)):
+ print("malformed input")
+else:
+ year_msb = int(m.group(1))
+ year_lsb = int(m.group(2))
+ month = int(m.group(3))
+ day = int(m.group(4))
+
+ if year_msb == 1:
+ year = 2000+year_lsb
+ else:
+ year = 1900+year_lsb
+
+ if not 1 <= month <= 12:
+ print("malformed month")
+ else:
+ if not 1 <= day <= monthrange(year, month)[1]:
+ print("malformed day")
+ else:
+ dt = date(year=year, month=month, day=day)
+ print(dt.strftime("%Y-%m-%d"))
diff --git a/challenge-038/paulo-custodio/python/ch-2.py b/challenge-038/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..9d7feca634
--- /dev/null
+++ b/challenge-038/paulo-custodio/python/ch-2.py
@@ -0,0 +1,79 @@
+#!/usr/bin/env python3
+
+# Challenge 038
+#
+# TASK #2
+# Word Game
+# Lets assume we have tiles as listed below, with an alphabet (A..Z) printed
+# on them. Each tile has a value, e.g. A (1 point), B (4 points) etc. You are
+# allowed to draw 7 tiles from the lot randomly. Then try to form a word using
+# the 7 tiles with maximum points altogether. You don't have to use all the
+# 7 tiles to make a word. You should try to use as many tiles as possible to
+# get the maximum points.
+#
+# For example, A (x8) means there are 8 tiles with letter A.
+#
+# 1 point
+# A (x8), G (x3), I (x5), S (x7), U (x5), X (x2), Z (x5)
+#
+# 2 points
+# E (x9), J (x3), L (x3), R (x3), V (x3), Y (x5)
+#
+# 3 points
+# F (x3), D (x3), P (x5), W (x5)
+#
+# 4 points
+# B (x5), N (x4)
+#
+# 5 points
+# T (x5), O (x3), H (x3), M (x4), C (x4)
+#
+# 10 points
+# K (x2), Q (x2)
+
+import sys
+import re
+
+g_score = {'a':1,'g':1,'i':1,'s':1,'u':1,'x':1,'z':1,
+ 'e':2,'j':2,'l':2,'r':2,'v':2,'y':2,
+ 'f':3,'d':3,'p':3,'w':3,
+ 'b':4,'n':4,
+ 't':5,'o':5,'h':5,'m':5,'c':5,
+ 'k':10,'q':10}
+g_count = {'a':8,'g':3,'i':5,'s':7,'u':5,'x':2,'z':5,
+ 'e':9,'j':3,'l':3,'r':3,'v':3,'y':5,
+ 'f':3,'d':3,'p':5,'w':5,
+ 'b':5,'n':4,
+ 't':5,'o':3,'h':3,'m':4,'c':4,
+ 'k':2,'q':2}
+
+def check_word(word):
+ # maximium 7 letters, only lower case letters
+ if not re.match(r"^[a-z]{1,7}$", word):
+ return -1
+
+ # less than count tiles for each letter
+ for letter in word:
+ if letter.count(letter) > g_count[letter]:
+ return -1
+
+ # compute score
+ score = 0
+ for letter in word:
+ score += g_score[letter]
+
+ return score
+
+
+dict = sys.argv[1]
+max_score = 0
+max_word = ""
+
+with open(dict) as f:
+ for line in f.readlines():
+ word = line.rstrip()
+ score = check_word(word)
+ if score > 0 and score > max_score:
+ max_score, max_word = score, word
+
+print(max_score, max_word)