diff options
| author | Michael Manring <michael@manring> | 2024-03-11 07:59:55 +1100 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2024-03-11 07:59:55 +1100 |
| commit | 821e8a01fd33c3409e370b4d54cd90733e2503c0 (patch) | |
| tree | 7cb3938baef05c67bd6f05b50bbc51da3d424065 | |
| parent | 13f22d5c15b0eae547b4190fe1afab64143969c0 (diff) | |
| download | perlweeklychallenge-club-821e8a01fd33c3409e370b4d54cd90733e2503c0.tar.gz perlweeklychallenge-club-821e8a01fd33c3409e370b4d54cd90733e2503c0.tar.bz2 perlweeklychallenge-club-821e8a01fd33c3409e370b4d54cd90733e2503c0.zip | |
pwc259 solution in go
| -rw-r--r-- | challenge-259/pokgopun/go/ch-1.go | 34 | ||||
| -rw-r--r-- | challenge-259/pokgopun/go/ch-2.go | 154 |
2 files changed, 188 insertions, 0 deletions
diff --git a/challenge-259/pokgopun/go/ch-1.go b/challenge-259/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..ceec77ce84 --- /dev/null +++ b/challenge-259/pokgopun/go/ch-1.go @@ -0,0 +1,34 @@ +//# 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 diff --git a/challenge-259/pokgopun/go/ch-2.go b/challenge-259/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..186a72442b --- /dev/null +++ b/challenge-259/pokgopun/go/ch-2.go @@ -0,0 +1,154 @@ +//# 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 + +package main + +import ( + "fmt" + "reflect" + "regexp" + "strconv" +) + +type kvpair map[string]string + +type parsed struct { + name, text string + fields kvpair +} + +type parser struct { + m []string + rgxLine, rgxKV *regexp.Regexp +} + +func newParser() parser { + return parser{ + rgxLine: regexp.MustCompile(`{%\s(?P<name>\w+)(?P<kv>(?:\s\w+=(?:\d+|"(?:\\(?:"|\\)|[^"\\])+?"))+)?\s%}(?:\n(?P<tag>[\d\D]+?)\n\{%\sendid\s%})?`), + rgxKV: regexp.MustCompile(`\s(?P<key>\w+)=(?P<value>\d+|"(?:\\(?:"|\\)|[^"\\])+?")`), + } +} + +func (ps parser) parse(msg string) parsed { + ps.m = ps.rgxLine.FindStringSubmatch(msg) + p := parsed{name: ps.m[1], text: ps.m[3], fields: make(kvpair)} + if ps.m[2] != "" { + for _, kv := range ps.rgxKV.FindAllStringSubmatch(ps.m[2], -1) { + _, err := strconv.Atoi(kv[2]) + if err != nil { + kv[2], _ = strconv.Unquote(kv[2]) + } + p.fields[kv[1]] = kv[2] + } + } + return p +} + +func main() { + p := newParser() + for _, data := range []struct { + input string + output parsed + }{ + { + `{% youtube id=1234 title="Title \"quoted\" done" %}`, + parsed{ + name: "youtube", + fields: kvpair{ + "id": "1234", + "title": `Title "quoted" done`, + }, + }, + }, + { + `{% youtube title="Title with escaped backslash \\" id=4321 %}`, + parsed{ + name: "youtube", + fields: kvpair{ + "id": "4321", + "title": `Title with escaped backslash \`, + }, + }, + }, + { + `{% id %}`, + parsed{name: "id", fields: kvpair{}}, + }, + { + `{% youtube id=12345 title="mejoo and cats" cats="\"monji\" \\ \"bongji\" \\ \"hyuji\" \\ \"yoji\" \\ \"audrey\"" %} +soo +mejoo +{% endid %}`, + parsed{ + name: "youtube", + fields: kvpair{ + "id": "12345", + "cats": `"monji" \ "bongji" \ "hyuji" \ "yoji" \ "audrey"`, + "title": "mejoo and cats", + }, + text: `soo +mejoo`, + }, + }, + } { + //fmt.Println(p.parse(data.input)) + fmt.Println(reflect.DeepEqual(p.parse(data.input), data.output)) + } +} |
