Skip to content

Commit

Permalink
Merge 10d8dba into c4c7dfb
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Feb 8, 2020
2 parents c4c7dfb + 10d8dba commit f74e8cd
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ problems from
* [Day 437](https://github.com/vaskoz/dailycodingproblem-go/issues/875)
* [Day 438](https://github.com/vaskoz/dailycodingproblem-go/issues/873)
* [Day 439](https://github.com/vaskoz/dailycodingproblem-go/issues/886)
* [Day 441](https://github.com/vaskoz/dailycodingproblem-go/issues/896)
* [Day 442](https://github.com/vaskoz/dailycodingproblem-go/issues/894)
* [Day 443](https://github.com/vaskoz/dailycodingproblem-go/issues/892)
* [Day 444](https://github.com/vaskoz/dailycodingproblem-go/issues/890)
Expand Down
33 changes: 33 additions & 0 deletions day441/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package day441

import "sort"

// DNFPartitionBrute has values smaller than pivot first, followed by pivot values,
// followed by values larger than pivot last.
// Runs in O(N log N) because it just sorts.
func DNFPartitionBrute(lst []int, pivot int) {
sort.Ints(lst)
}

// DNFPartition has values smaller than pivot first, followed by pivot values,
// followed by values larger than pivot last.
// Runs in O(N) because it just sorts.
func DNFPartition(lst []int, pivot int) {
var i, j int

n := len(lst) - 1

for j <= n {
switch {
case lst[j] < pivot:
lst[i], lst[j] = lst[j], lst[i]
i++
j++
case lst[j] > pivot:
lst[j], lst[n] = lst[n], lst[j]
n--
default:
j++
}
}
}
74 changes: 74 additions & 0 deletions day441/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package day441

import "testing"

// nolint
var testcases = []struct {
lst []int
pivot int
}{
{[]int{9, 12, 3, 5, 14, 10, 10}, 10},
{[]int{9, 12, 3, 5, 14, 10, 10}, 20},
{[]int{9, 12, 3, 5, 14, 10, 10}, 1},
}

func checkInvariant(lst []int, pivot int) bool {
var i int
for i < len(lst) && lst[i] < pivot {
i++
}

for i < len(lst) && lst[i] == pivot {
i++
}

for i < len(lst) && lst[i] > pivot {
i++
}

return i == len(lst)
}

func TestDNFPartitionBrute(t *testing.T) {
t.Parallel()

for _, tc := range testcases {
copied := append([]int{}, tc.lst...)
DNFPartitionBrute(copied, tc.pivot)

if !checkInvariant(copied, tc.pivot) {
t.Errorf("Didn't partition correctly. pivot=%d. Given %v got %v", tc.pivot, tc.lst, copied)
}
}
}

func BenchmarkDNFPartitionBrute(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
copied := append([]int{}, tc.lst...)
DNFPartitionBrute(copied, tc.pivot)
}
}
}

func TestDNFPartition(t *testing.T) {
t.Parallel()

for _, tc := range testcases {
copied := append([]int{}, tc.lst...)
DNFPartition(copied, tc.pivot)

if !checkInvariant(copied, tc.pivot) {
t.Errorf("Didn't partition correctly. pivot=%d. Given %v got %v", tc.pivot, tc.lst, copied)
}
}
}

func BenchmarkDNFPartition(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
copied := append([]int{}, tc.lst...)
DNFPartition(copied, tc.pivot)
}
}
}

0 comments on commit f74e8cd

Please sign in to comment.