added support for edge cases
This commit is contained in:
1 parent
72e5ece491
commit
ed5aa0347a
2 files changed
+30
-16
No files matched your search
+15
-16
@@ -5,12 +5,6 @@ type IsGreaterer interface {
|
|||||||
IsGreater(e IsGreaterer) bool
|
IsGreater(e IsGreaterer) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type alwaysBigger struct{}
|
|
||||||
|
|
||||||
func (a *alwaysBigger) IsGreater(e IsGreaterer) bool {
|
|
||||||
return e != nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Stream chan IsGreaterer
|
type Stream chan IsGreaterer
|
||||||
|
|
||||||
type streamOutput struct {
|
type streamOutput struct {
|
||||||
@@ -76,7 +70,11 @@ func prependValueToEachStream(streams []Stream, values []IsGreaterer) []Stream {
|
|||||||
func getSmallestElement(arr []IsGreaterer) (IsGreaterer, int) {
|
func getSmallestElement(arr []IsGreaterer) (IsGreaterer, int) {
|
||||||
smallest, idx := arr[0], 0
|
smallest, idx := arr[0], 0
|
||||||
for i, elm := range arr {
|
for i, elm := range arr {
|
||||||
if smallest.IsGreater(elm) {
|
if smallest == nil {
|
||||||
|
smallest = elm
|
||||||
|
idx = i
|
||||||
|
}
|
||||||
|
if elm != nil && smallest.IsGreater(elm) {
|
||||||
smallest = elm
|
smallest = elm
|
||||||
idx = i
|
idx = i
|
||||||
}
|
}
|
||||||
@@ -100,26 +98,27 @@ func getSmallerStreamWithSecondSmallestElementWithAllTheOthersGreaterStreams(str
|
|||||||
_, smallestStreamIndex := getSmallestElement(firstElements)
|
_, smallestStreamIndex := getSmallestElement(firstElements)
|
||||||
secondSmallestItem, _ := getSecondSmallestElement(firstElements)
|
secondSmallestItem, _ := getSecondSmallestElement(firstElements)
|
||||||
|
|
||||||
if secondSmallestItem == nil {
|
|
||||||
return streams[smallestStreamIndex], nil, []Stream{}
|
|
||||||
}
|
|
||||||
return streams[smallestStreamIndex], secondSmallestItem, allStreamsButOne(streams, smallestStreamIndex)
|
return streams[smallestStreamIndex], secondSmallestItem, allStreamsButOne(streams, smallestStreamIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func outputToStream(input, output Stream) {
|
||||||
|
for e := range input {
|
||||||
|
output <- e
|
||||||
|
}
|
||||||
|
close(output)
|
||||||
|
}
|
||||||
|
|
||||||
func sortStreamInChannel(o Stream, streams []Stream) {
|
func sortStreamInChannel(o Stream, streams []Stream) {
|
||||||
if len(streams) == 1 {
|
if len(streams) == 1 {
|
||||||
for e := range streams[0] {
|
outputToStream(streams[0], o)
|
||||||
o <- e
|
|
||||||
}
|
|
||||||
close(o)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
smallestStream, secondSmallestItem, greaterStreams := getSmallerStreamWithSecondSmallestElementWithAllTheOthersGreaterStreams(streams)
|
smallestStream, secondSmallestItem, greaterStreams := getSmallerStreamWithSecondSmallestElementWithAllTheOthersGreaterStreams(streams)
|
||||||
if secondSmallestItem == nil {
|
if secondSmallestItem == nil {
|
||||||
sortStreamInChannel(o, append(greaterStreams, smallestStream))
|
sortStreamInChannel(o, []Stream{smallestStream})
|
||||||
return
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for elm := range smallestStream {
|
for elm := range smallestStream {
|
||||||
if !elm.IsGreater(secondSmallestItem) {
|
if !elm.IsGreater(secondSmallestItem) {
|
||||||
o <- elm
|
o <- elm
|
||||||
|
|||||||
@@ -153,8 +153,23 @@ func TestSortStreams(t *testing.T) {
|
|||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s := SortStreams(CreateStream()); !streamEqual(s) {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
|
||||||
if s := SortStreams(CreateStream(1, 4), CreateStream(0, 5)); !streamEqual(s, 0, 1, 4, 5) {
|
if s := SortStreams(CreateStream(1, 4), CreateStream(0, 5)); !streamEqual(s, 0, 1, 4, 5) {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s := SortStreams(CreateStream(1, 2), CreateStream(3, 4), CreateStream(5, 6)); !streamEqual(s, 1, 2, 3, 4, 5, 6) {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
if s := SortStreams(CreateStream(1, 2), CreateStream()); !streamEqual(s, 1, 2) {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
if s := SortStreams(CreateStream(), CreateStream(1, 2)); !streamEqual(s, 1, 2) {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user