Skip to content

Commit

Permalink
Merge pull request #60 from vaskoz/day25
Browse files Browse the repository at this point in the history
Day25
  • Loading branch information
vaskoz committed Sep 16, 2018
2 parents f359fd9 + 4cb25ed commit 536543b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ problems from
* [Day 22](https://github.com/vaskoz/dailycodingproblem-go/issues/53)
* [Day 23](https://github.com/vaskoz/dailycodingproblem-go/issues/55)
* [Day 24](https://github.com/vaskoz/dailycodingproblem-go/issues/57)
* [Day 25](https://github.com/vaskoz/dailycodingproblem-go/issues/59)
2 changes: 1 addition & 1 deletion day21/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type lectureEvent struct {
starting bool
}

// MinimumRooms calculates the minimum number of rooms to accomodate
// MinimumRooms calculates the minimum number of rooms to accommodate
// overlapping schedules.
// Runtime is O(N log N) with O(2N) space
func MinimumRooms(lectures []Lecture) int {
Expand Down
30 changes: 30 additions & 0 deletions day25/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package day25

// Match returns if there is a regex match on the given input.
func Match(input, pattern string) bool {
var ii, pi, missed int
for ii < len(input) && pi < len(pattern) && missed < 2 {
if pattern[pi] == '*' {
prev := pattern[pi-1]
var next byte
if pi+1 < len(pattern) {
next = pattern[pi+1]
}
for ii < len(input) && input[ii] != next {
if prev != '.' && input[ii] != prev {
return false
}
ii++
}
if ii == len(input) && pi < len(pattern)-1 {
return false
}
ii--
} else if pattern[pi] != '.' && input[ii] != pattern[pi] {
missed++
}
ii++
pi++
}
return ii == len(input) && pi == len(pattern)
}
36 changes: 36 additions & 0 deletions day25/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package day25

import "testing"

var testcases = []struct {
input, pattern string
expected bool
}{
{"ray", "ra.", true},
{"raymond", "ra.", false},
{"chat", ".*at", true},
{"chats", ".*at", false},
{"chaaaa", "cha*", true},
{"raymond", "ra.*", true},
{"raymond", "ra.*d", true},
{"chaaab", "cha*", false},
{"chaaa", "cha*b", false},
{"abcd", "efg", false},
{"ab", "e", false},
}

func TestMatch(t *testing.T) {
for _, tc := range testcases {
if result := Match(tc.input, tc.pattern); result != tc.expected {
t.Errorf("Given (%v, %v), expected %v got %v", tc.input, tc.pattern, tc.expected, result)
}
}
}

func BenchmarkMatch(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
Match(tc.input, tc.pattern)
}
}
}

0 comments on commit 536543b

Please sign in to comment.