simplify code a bit
This commit is contained in:
1 parent
bf0b66c280
commit
72e5ece491
2 files changed
+52
-25
No files matched your search
+23
-16
@@ -1,9 +1,5 @@
|
|||||||
package sorting
|
package sorting
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
var _ = fmt.Print
|
|
||||||
|
|
||||||
type IsGreaterer interface {
|
type IsGreaterer interface {
|
||||||
// return true iff "this" is strictly greater than e
|
// return true iff "this" is strictly greater than e
|
||||||
IsGreater(e IsGreaterer) bool
|
IsGreater(e IsGreaterer) bool
|
||||||
@@ -77,23 +73,34 @@ func prependValueToEachStream(streams []Stream, values []IsGreaterer) []Stream {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getSmallestElement(arr []IsGreaterer) (IsGreaterer, int) {
|
||||||
|
smallest, idx := arr[0], 0
|
||||||
|
for i, elm := range arr {
|
||||||
|
if smallest.IsGreater(elm) {
|
||||||
|
smallest = elm
|
||||||
|
idx = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return smallest, idx
|
||||||
|
}
|
||||||
|
|
||||||
|
func getSecondSmallestElement(arr []IsGreaterer) (IsGreaterer, int) {
|
||||||
|
_, smallerIdx := getSmallestElement(arr)
|
||||||
|
secondSmallest, idx := getSmallestElement(append(arr[0:smallerIdx], arr[smallerIdx+1:len(arr)]...))
|
||||||
|
if idx > smallerIdx {
|
||||||
|
idx++
|
||||||
|
}
|
||||||
|
return secondSmallest, idx
|
||||||
|
}
|
||||||
|
|
||||||
func getSmallerStreamWithSecondSmallestElementWithAllTheOthersGreaterStreams(streams []Stream) (Stream, IsGreaterer, []Stream) {
|
func getSmallerStreamWithSecondSmallestElementWithAllTheOthersGreaterStreams(streams []Stream) (Stream, IsGreaterer, []Stream) {
|
||||||
firstElements := readFirstElementOfAllTheStreams(streams)
|
firstElements := readFirstElementOfAllTheStreams(streams)
|
||||||
streams = prependValueToEachStream(streams, firstElements)
|
streams = prependValueToEachStream(streams, firstElements)
|
||||||
|
|
||||||
var smallestStreamIndex int = -1
|
_, smallestStreamIndex := getSmallestElement(firstElements)
|
||||||
var smallestItem IsGreaterer = &alwaysBigger{}
|
secondSmallestItem, _ := getSecondSmallestElement(firstElements)
|
||||||
var secondSmallestItem IsGreaterer = &alwaysBigger{}
|
|
||||||
|
|
||||||
for i, e := range firstElements {
|
if secondSmallestItem == nil {
|
||||||
if smallestItem.IsGreater(e) {
|
|
||||||
smallestItem = e
|
|
||||||
smallestStreamIndex = i
|
|
||||||
} else if secondSmallestItem.IsGreater(e) {
|
|
||||||
secondSmallestItem = e
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if _, ok := secondSmallestItem.(*alwaysBigger); ok {
|
|
||||||
return streams[smallestStreamIndex], nil, []Stream{}
|
return streams[smallestStreamIndex], nil, []Stream{}
|
||||||
}
|
}
|
||||||
return streams[smallestStreamIndex], secondSmallestItem, allStreamsButOne(streams, smallestStreamIndex)
|
return streams[smallestStreamIndex], secondSmallestItem, allStreamsButOne(streams, smallestStreamIndex)
|
||||||
|
|||||||
+29
-9
@@ -74,6 +74,34 @@ func TestAllStreamsButOne(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetSmallestElement(t *testing.T) {
|
||||||
|
e, i := getSmallestElement([]IsGreaterer{MockIsGreaterer(9), MockIsGreaterer(10), MockIsGreaterer(1)})
|
||||||
|
if i != 2 {
|
||||||
|
t.Error("bad index")
|
||||||
|
}
|
||||||
|
if e != MockIsGreaterer(1) {
|
||||||
|
t.Error("bad value")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetSecondSmallestElement(t *testing.T) {
|
||||||
|
e, i := getSecondSmallestElement([]IsGreaterer{MockIsGreaterer(9), MockIsGreaterer(10), MockIsGreaterer(1)})
|
||||||
|
if i != 0 {
|
||||||
|
t.Error("bad index")
|
||||||
|
}
|
||||||
|
if e != MockIsGreaterer(9) {
|
||||||
|
t.Error("bad value")
|
||||||
|
}
|
||||||
|
|
||||||
|
e, i = getSecondSmallestElement([]IsGreaterer{MockIsGreaterer(0), MockIsGreaterer(10), MockIsGreaterer(1)})
|
||||||
|
if i != 2 {
|
||||||
|
t.Error("bad index")
|
||||||
|
}
|
||||||
|
if e != MockIsGreaterer(1) {
|
||||||
|
t.Error("bad value")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetSmallerStreamWithSecondSmallestElementWithAllTheOthersGreaterStreams(t *testing.T) {
|
func TestGetSmallerStreamWithSecondSmallestElementWithAllTheOthersGreaterStreams(t *testing.T) {
|
||||||
streams := []Stream{CreateStream(0, 1), CreateStream(3, 4), CreateStream(7, 8)}
|
streams := []Stream{CreateStream(0, 1), CreateStream(3, 4), CreateStream(7, 8)}
|
||||||
stream, e, streams := getSmallerStreamWithSecondSmallestElementWithAllTheOthersGreaterStreams(streams)
|
stream, e, streams := getSmallerStreamWithSecondSmallestElementWithAllTheOthersGreaterStreams(streams)
|
||||||
@@ -121,19 +149,11 @@ func TestSortStreams(t *testing.T) {
|
|||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
if s := SortStreams(CreateStream(), CreateStream(2, 7)); !streamEqual(s, 2, 7) {
|
|
||||||
t.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
if s := SortStreams(CreateStream(), CreateStream(2, 7), CreateStream(3, 8)); !streamEqual(s, 2, 3, 7, 8) {
|
|
||||||
t.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
if s := SortStreams(CreateStream()); !streamEqual(s) {
|
if s := SortStreams(CreateStream()); !streamEqual(s) {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
if s := SortStreams(CreateStream(1, 2), CreateStream(2, 3)); !streamEqual(s, 1, 2, 2, 3) {
|
if s := SortStreams(CreateStream(1, 4), CreateStream(0, 5)); !streamEqual(s, 0, 1, 4, 5) {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user