-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ozoncp/feature/task-2
Feature/task 2
- Loading branch information
Showing
6 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package utils | ||
|
||
func defaultList() []string { | ||
return []string{"1", "2", "3", "4", "5"} | ||
} | ||
|
||
func contain(source []string, val string) bool { | ||
for i := range source { | ||
if source[i] == val { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func Filter(source []string) []string { | ||
result := make([]string, 0, len(source)) | ||
for _, val := range source { | ||
if !contain(defaultList(), val) { | ||
result = append(result, val) | ||
} | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package utils | ||
|
||
import "testing" | ||
|
||
func TestFilter(t *testing.T) { | ||
source := []string{"0", "1", "2", "3", "6", "10"} | ||
expected := []string{"0", "6", "10"} | ||
|
||
result := Filter(source) | ||
|
||
if len(result) != len(expected) { | ||
t.Errorf("Expected %v, received %v", expected, result) | ||
} | ||
|
||
for i := range result { | ||
if expected[i] != result[i] { | ||
t.Errorf("Mismatched at index %v expected %v, received %v", i, expected[i], result[i]) | ||
} | ||
} | ||
} | ||
|
||
func TestFilterEmpty(t *testing.T) { | ||
result := Filter(nil) | ||
|
||
if len(result) != 0 { | ||
t.Errorf("Expected empty, received %v", result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package utils | ||
|
||
func Flip(source map[int]string) map[string]int { | ||
result := make(map[string]int, len(source)) | ||
|
||
for key, value := range source { | ||
result[value] = key | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package utils | ||
|
||
import "testing" | ||
|
||
func TestEmptyFlip(t *testing.T) { | ||
val := Flip(nil) | ||
if len(val) != 0 { | ||
t.Errorf("Expected empty, received %v", val) | ||
} | ||
} | ||
|
||
func TestFlip(t *testing.T) { | ||
length := 3 | ||
source := make(map[int]string, length) | ||
source[1] = "one" | ||
source[2] = "two" | ||
source[3] = "three" | ||
|
||
val := Flip(source) | ||
if len(val) != length { | ||
t.Errorf("Expected length %v , received %v", length, val) | ||
return | ||
} | ||
|
||
for key, value := range val { | ||
if source[value] != key { | ||
t.Errorf("Expected key %v, received %v", source[value], key) | ||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package utils | ||
|
||
import "errors" | ||
|
||
func Split(source []string, batchLen int) ([][]string, error) { | ||
if batchLen <= 0 { | ||
return nil, errors.New("batch length cannot be a zero or negative") | ||
} | ||
sourceLen := len(source) | ||
fullBatchesCount := sourceLen / batchLen | ||
batchesCount := fullBatchesCount | ||
if sourceLen % batchLen != 0 { | ||
batchesCount++ | ||
} | ||
result := make([][]string, batchesCount) | ||
|
||
for i := 0; i < fullBatchesCount; i++ { | ||
result[i] = append([]string{}, source[i * batchLen:(i + 1) * batchLen]...) | ||
} | ||
|
||
if fullBatchesCount != batchesCount { | ||
result[fullBatchesCount] = append([]string{}, source[fullBatchesCount * batchLen:sourceLen]...) | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package utils | ||
|
||
import "testing" | ||
|
||
func TestSplitNegativeBatchLen(t *testing.T) { | ||
val, err := Split(nil, -1) | ||
if err == nil { | ||
t.Errorf("Expected error, received %v", val) | ||
} | ||
} | ||
|
||
func TestSplitZeroBatchLen(t *testing.T) { | ||
val, err := Split(nil, 0) | ||
if err == nil { | ||
t.Errorf("Expected error, received %v", val) | ||
} | ||
} | ||
|
||
func TestSplitNilSource(t *testing.T) { | ||
val, err := Split(nil, 3) | ||
if err != nil { | ||
t.Errorf("Expected value, received error %v", err) | ||
} | ||
|
||
if len(val) != 0 { | ||
t.Errorf("Expected empty value, received %v", val) | ||
} | ||
} | ||
|
||
func compareSlices(left []string, right []string) bool { | ||
if len(left) != len(right) { | ||
return false | ||
} | ||
for i := 0; i < len(left); i++ { | ||
if left[i] != right[i] { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func TestSplitFullBatches(t *testing.T) { | ||
source := []string{"one", "two", "three", "four", "five", "six"} | ||
batchLen := 2 | ||
expectedCount := 3 | ||
val, err := Split(source, batchLen) | ||
if err != nil { | ||
t.Errorf("Expected value, received error %v", err) | ||
} | ||
|
||
if len(val) != expectedCount { | ||
t.Fatalf("Expected %v slices, received %v", batchLen, val) | ||
} | ||
|
||
for i := 0; i < expectedCount; i++ { | ||
expectedSlice := source[i * batchLen:(i+1) * batchLen] | ||
if !compareSlices(expectedSlice, val[i]) { | ||
t.Fatalf("Expected %v, received %v", expectedSlice, val[i]) | ||
} | ||
} | ||
} | ||
|
||
func TestSplitPartialBatches(t *testing.T) { | ||
source := []string{"one", "two", "three", "four", "five", "six"} | ||
sourceLen := len(source) | ||
batchLen := 4 | ||
expectedCount := 2 | ||
val, err := Split(source, batchLen) | ||
if err != nil { | ||
t.Errorf("Expected value, received error %v", err) | ||
} | ||
|
||
if len(val) != expectedCount { | ||
t.Fatalf("Expected %v slices, received %v", batchLen, val) | ||
} | ||
|
||
for i := 0; i < expectedCount; i++ { | ||
first := i * batchLen | ||
last := first + batchLen | ||
if last > sourceLen { | ||
last = sourceLen | ||
} | ||
expectedSlice := source[first:last] | ||
if !compareSlices(expectedSlice, val[i]) { | ||
t.Fatalf("Expected %v, received %v", expectedSlice, val[i]) | ||
} | ||
} | ||
} | ||
|
||
func TestSplitUnchanges(t *testing.T) { | ||
source := []string{"one", "two", "three", "four", "five", "six"} | ||
res, _ := Split(source, 3) | ||
baseVal := res[0][0] | ||
source[0] = "WOW!" | ||
newVal := res[0][0] | ||
if baseVal != newVal { | ||
t.Errorf("res[0][0] is <%v>, <%v> expected", newVal, baseVal) | ||
} | ||
} |