aboutsummaryrefslogtreecommitdiff
path: root/challenge-259/eric-cheung/python
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2024-03-05 12:32:31 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2024-03-05 12:32:31 +0000
commit9afcaea2974dfbb52662e6fc3802bef0c64e65be (patch)
tree03a0bcb9ade10a975577e5003f2f98914f59ec13 /challenge-259/eric-cheung/python
parent7a8545402b45caae224420ec9811914daa1a06ec (diff)
downloadperlweeklychallenge-club-9afcaea2974dfbb52662e6fc3802bef0c64e65be.tar.gz
perlweeklychallenge-club-9afcaea2974dfbb52662e6fc3802bef0c64e65be.tar.bz2
perlweeklychallenge-club-9afcaea2974dfbb52662e6fc3802bef0c64e65be.zip
- Added solutions by Peter Meszaros.
- Added solutions by Dave Jacoby. - Added solutions by Mark Anderson. - Added solutions by Roger Bell_West. - Added solutions by Mariano Spadaccini.
Diffstat (limited to 'challenge-259/eric-cheung/python')
-rwxr-xr-xchallenge-259/eric-cheung/python/ch-1.py23
-rwxr-xr-xchallenge-259/eric-cheung/python/ch-2.py57
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-259/eric-cheung/python/ch-1.py b/challenge-259/eric-cheung/python/ch-1.py
new file mode 100755
index 0000000000..794b79e029
--- /dev/null
+++ b/challenge-259/eric-cheung/python/ch-1.py
@@ -0,0 +1,23 @@
+
+from datetime import datetime, timedelta
+
+strDateFormat = "%Y-%m-%d"
+
+## Example 1
+## strStartDate = "2018-06-28"
+## nOffset = 3
+## arrBankHoliday = ["2018-07-03"]
+
+## Example 2
+strStartDate = "2018-06-28"
+nOffset = 3
+arrBankHoliday = []
+
+objOutputDate = datetime.strptime(strStartDate, strDateFormat)
+
+while nOffset > 0:
+ objOutputDate = objOutputDate + timedelta(days = 1)
+ if objOutputDate.weekday() < 5 and not objOutputDate.strftime(strDateFormat) in arrBankHoliday:
+ nOffset = nOffset - 1
+
+print (objOutputDate.strftime(strDateFormat))
diff --git a/challenge-259/eric-cheung/python/ch-2.py b/challenge-259/eric-cheung/python/ch-2.py
new file mode 100755
index 0000000000..271db58f8a
--- /dev/null
+++ b/challenge-259/eric-cheung/python/ch-2.py
@@ -0,0 +1,57 @@
+
+## Remarks
+## https://stackoverflow.com/questions/74569246/replace-space-in-between-double-quote-to-underscore
+
+import json
+import re
+
+def GetDoubleQuote (strInput):
+ if strInput[0] == "\"":
+ return strInput
+ return "\"" + strInput + "\""
+
+def repl(strInput):
+ return strInput[0].replace(' ', '^')
+
+
+## strLineInput = '{% id field1="value1" field2="value2" field3=42 %}' ## Example 1
+## strLineInput = '{% youtube title="Title \"quoted\" done" %}' ## Example 2
+## strLineInput = '{% youtube title="Title quoted done" %}' ## Example 3
+## strLineInput = '{% youtube title="Title with escaped backslash \\" %}' ## Example 4
+## strLineInput = '{% id %}' ## Example 5
+strLineInput = '''
+{% id field1="value1" field2="value2" field3=42 %}
+LINES
+{% endid %}
+''' ## Example 6
+
+bMultipleLine = False
+arrMultipleLineSplit = strLineInput.split("\n")
+
+if len(arrMultipleLineSplit) > 1:
+ bMultipleLine = True
+ strLineInput = arrMultipleLineSplit[1]
+
+strReplacePattern = re.compile(r'\"[^\"]+\"')
+strLineInput = re.sub(strReplacePattern, repl, strLineInput)
+
+arrLineSplit = strLineInput.replace("%", "").split()
+arrLineSplit = [elemLoop.replace('^', ' ') for elemLoop in arrLineSplit]
+
+arrLineSplit[1] = GetDoubleQuote("name") + " : " + GetDoubleQuote(arrLineSplit[1])
+for nIndx in range(2, len(arrLineSplit) - 1):
+ arrLineSplit[nIndx] = " : ".join([GetDoubleQuote(elemLoop) if nIndx == 0 else elemLoop for nIndx, elemLoop in enumerate(arrLineSplit[nIndx].split("="))])
+
+strJsonOutput = "{" + arrLineSplit[1]
+if len(arrLineSplit) > 3:
+ strJsonOutput = strJsonOutput + ", " + GetDoubleQuote("fields") + " : " + "{" + ", ".join(arrLineSplit[2:-1]) + "}"
+
+if bMultipleLine:
+ strJsonOutput = strJsonOutput + ", " + GetDoubleQuote("text") + " : " + GetDoubleQuote(arrMultipleLineSplit[2])
+
+strJsonOutput = strJsonOutput + "}"
+
+## print (arrLineSplit)
+## print (strJsonOutput)
+print (json.loads(strJsonOutput))
+## print (json.loads(strJsonOutput)["name"])