diff options
| author | Michael Manring <michael@manring> | 2024-03-05 02:15:10 +1100 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2024-03-11 03:21:22 +1100 |
| commit | 13f22d5c15b0eae547b4190fe1afab64143969c0 (patch) | |
| tree | a8749fdf3b5070c93e3edc8bebbc234b68c0344e | |
| parent | ad381c1123e47114d3d98f1749f9f354eebafcdd (diff) | |
| download | perlweeklychallenge-club-13f22d5c15b0eae547b4190fe1afab64143969c0.tar.gz perlweeklychallenge-club-13f22d5c15b0eae547b4190fe1afab64143969c0.tar.bz2 perlweeklychallenge-club-13f22d5c15b0eae547b4190fe1afab64143969c0.zip | |
pwc259 solution in python
| -rw-r--r-- | challenge-259/pokgopun/python/ch-1.py | 86 | ||||
| -rw-r--r-- | challenge-259/pokgopun/python/ch-2.py | 119 |
2 files changed, 205 insertions, 0 deletions
diff --git a/challenge-259/pokgopun/python/ch-1.py b/challenge-259/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..ab2c06878e --- /dev/null +++ b/challenge-259/pokgopun/python/ch-1.py @@ -0,0 +1,86 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-259/ +""" + +Task 1: Banking Day Offset + +Submitted by: [42]Lee Johnson + __________________________________________________________________ + + You are given a start date and offset counter. Optionally you also get + bank holiday date list. + + Given a number (of days) and a start date, return the number (of days) + adjusted to take into account non-banking days. In other words: convert + a banking day offset to a calendar day offset. + + Non-banking days are: +a) Weekends +b) Bank holidays + +Example 1 + +Input: $start_date = '2018-06-28', $offset = 3, $bank_holidays = ['2018-07-03'] +Output: '2018-07-04' + +Thursday bumped to Wednesday (3 day offset, with Monday a bank holiday) + +Example 2 + +Input: $start_date = '2018-06-28', $offset = 3 +Output: '2018-07-03' + +Task 2: Line Parser +""" +### solution by pokgopun@gmail.com + +from datetime import date,datetime,timedelta + +def str2date(datestr: str): ### convert date string to date object + return datetime.strptime(datestr,"%Y-%m-%d").date() + +def isWeekday(d: date): ### check if date object is a weekday, that is less than Saturday (i.e. 5 in python) + return d.weekday() < 5 + +def BDO(start: str, ofst: int, hols: tuple = ()): + dstart = str2date(start) + if isWeekday(dstart) == False: ### if startdate is a weekend day, move back to last friday + dstart -= timedelta(days = dstart.weekday() - 4) ### move back number of day from friday + + ### convert weekday offset (ofst) to everyday offset (dur), that is 7 * number of week (i.e. ofset // 5) and the remaing partial-weekday offset (ofst % 5) + dur = timedelta(days = 7*(ofst // 5) + ofst % 5) + ### now we need to account for partial-weekday offset in the everyday offset + if dstart.weekday() + ofst % 5 > 4: ### if partial-weekday offset move startdate over the current weekday, add weekend offset to everyday offset + dur += timedelta(days=2) + dend = dstart + dur ### calculate enddate + + ### now account for holidays one by one, need to start from the earliest holiday first so we sort holidays before processing + for hol in sorted(set(hols)): + dhol = str2date(hol) + if dhol > dstart and dhol <= dend and isWeekday(dhol): ### only offset holiday if it is a weekday and is later than startdate but not later than current enddate + dend += timedelta(days=1) + if isWeekday(dend) == False: ### if a holiday offset move enddate to a weekend day, add weekend offset + dend += timedelta(days=2) ### this is ok as we account for holiday offset one by one + return dend.strftime("%Y-%m-%d") + +import unittest + +class TestBDO(unittest.TestCase): + def test(self): + for output, (start_date, offset, bank_holidays) in { + "2018-07-04": ("2018-06-28", 3, ("2018-07-03",)), + "2018-07-03": ("2018-06-28", 3, ()), + "2018-07-05": ("2018-06-28", 3, ("2018-07-02", "2018-07-03")), + "2018-07-05": ("2018-06-28", 3, ("2018-07-06", "2018-07-03", "2018-07-02")), + "2018-07-06": ("2018-06-28", 3, ("2018-07-02", "2018-07-03", "2018-07-05")), + "2018-07-06": ("2018-06-28", 3, ("2018-07-02", "2018-07-03", "2018-07-05","2024-03-05")), + "2018-07-06": ("2018-06-28", 3, ("2018-07-02", "2018-07-03", "2018-07-05","1979-07-09")), + "2018-07-13": ("2018-06-28", 8, ("2018-07-02", "2018-07-03", "2018-07-05","1979-07-09")), + "2018-07-16": ("2018-06-28", 9, ("2018-07-02", "2018-07-03", "2018-07-05","1979-07-09")), + "2018-07-23": ("2018-06-28", 14, ("2018-07-02", "2018-07-03", "2018-07-05","1979-07-09")), + "2018-07-25": ("2018-06-28", 14, ("2018-07-02", "2018-07-03", "2018-07-05","2018-07-11","2018-07-18")), + "2018-07-05": ("2018-06-29", 3, ("2018-07-03",)), + "2018-07-05": ("2018-06-30", 3, ("2018-07-03",)), + }.items(): + self.assertEqual(output, BDO(start_date, offset, bank_holidays)) + +unittest.main() diff --git a/challenge-259/pokgopun/python/ch-2.py b/challenge-259/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..ac56114e24 --- /dev/null +++ b/challenge-259/pokgopun/python/ch-2.py @@ -0,0 +1,119 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-259/ +""" + +Task 2: Line Parser + +Submitted by: [43]Gabor Szabo + __________________________________________________________________ + + You are given a line like below: +{% id field1="value1" field2="value2" field3=42 %} + + Where +a) "id" can be \w+. +b) There can be 0 or more field-value pairs. +c) The name of the fields are \w+. +b) The values are either number in which case we don't need parentheses or strin +g in + which case we need parentheses around them. + + The line parser should return structure like below: +{ + name => id, + fields => { + field1 => value1, + field2 => value2, + field3 => value3, + } +} + + It should be able to parse the following edge cases too: +{% youtube title="Title \"quoted\" done" %} + + and +{% youtube title="Title with escaped backslash \\" %} + + BONUS: Extend it to be able to handle multiline tags: +{% id filed1="value1" ... %} +LINES +{% endid %} + + You should expect the following structure from your line parser: +{ + name => id, + fields => { + field1 => value1, + field2 => value2, + field3 => value3, + } + text => LINES +} + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 10th March + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +import re + +rgxLine = re.compile(r'{%\s(?P<name>\w+)(?P<kv>(?:\s\w+=(?:\d+|"(?:\\(?:"|\\)|[^"\\])+?"))+)?\s%}(?:\n(?P<tag>[\d\D]+?)\n\{%\sendid\s%})?') +rgxKV = re.compile(r'\s(?P<key>\w+)=(?P<value>\d+|"(?:\\(?:"|\\)|[^"\\])+?")') + +def lineParser(msg: str): + res = rgxLine.match(msg) + parsed = { "name": res.group("name") } + resKV = res.group("kv") + if resKV is not None: + parsed["fields"] = {} + for resKV in rgxKV.finditer(res.group("kv")): + k = resKV.group("key") + v = resKV.group("value") + if v.isnumeric(): + v = int(v) + else: + v = v[1:-1].encode("utf-8").decode('unicode_escape') + parsed["fields"][k] = v + tag = res.group("tag") + if tag is not None: + parsed["text"] = tag + return parsed + +import unittest + +class TestLineParser(unittest.TestCase): + def test(self): + for inpt, otpt in { + r'{% youtube id=1234 title="Title \"quoted\" done" %}': { + 'name': 'youtube', + 'fields': { + 'id': 1234, + 'title': 'Title "quoted" done', + }, + }, + r'{% youtube title="Title with escaped backslash \\" id=4321 %}': { + 'name': 'youtube', + 'fields': { + 'id': 4321, + 'title': 'Title with escaped backslash \\', + }, + }, + r'''{% id field1="value1" field2=1324 %} +LINE1 +LINE2 +{% endid %}''': { + 'name': 'id', + 'fields': { + 'field2': 1324, + 'field1': 'value1', + }, + 'text': '''LINE1 +LINE2''', +}, +}.items(): + self.assertEqual(lineParser(inpt),otpt) + +unittest.main() |
