diff --git a/.DS_Store b/.DS_Store index bff0db8..5d427e7 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/consecutive/cons.go b/consecutive/cons.go new file mode 100644 index 0000000..bddaca3 --- /dev/null +++ b/consecutive/cons.go @@ -0,0 +1,35 @@ +package consecutive + +func Consecutive(A []int) int { + + if len(A) != 0 { + + min, max := extremum(A) + + if len(A) == max-min+1 && len(A) > 1 { + + return 1 + + } + + } + + return 0 + +} + +func extremum(A []int) (int, int) { + min := A[0] + max := A[0] + + for i := 0; i < len(A); i++ { + if min > A[i] { + min = A[i] + } + if max < A[i] { + max = A[i] + } + + } + return min, max +} diff --git a/consecutive/cons_test.go b/consecutive/cons_test.go new file mode 100644 index 0000000..e657868 --- /dev/null +++ b/consecutive/cons_test.go @@ -0,0 +1,51 @@ +package consecutive_test + +import ( + "fmt" + "gotest/consecutive" + "testing" +) + +// Структура тестовых данных, input - входной массив, +// output - ожидаеммый вывод + +type testdata struct { + input []int + output int +} + +// Входной, повторения, ожидания + +var test = []testdata{ + {[]int{1, 2, 4, 3, 6, 5}, 1}, + {[]int{1, 2, 3, 5}, 0}, + {[]int{255, 256, 257, 258}, 1}, +} + +func TestCons(t *testing.T) { + + t.Run( + "insert slices", func(t *testing.T) { + for _, data := range test { + // Передаём массив и повторения в функцию и записываем результат в переменную + v := consecutive.Consecutive(data.input) + // Сравниваем вывод с ожиданием + if v != data.output { + t.Error( + "For: ", data.input, + "Expected: ", data.output, + "Got: ", v, + ) + } else { + fmt.Println( + "For: ", data.input, + "Expected: ", data.output, + "Got: ", v, + ) + + } + } + }, + ) + +} diff --git a/fizzbuzz/.DS_Store b/fizzbuzz/.DS_Store deleted file mode 100644 index c267298..0000000 Binary files a/fizzbuzz/.DS_Store and /dev/null differ diff --git a/fizzbuzz/FizzBuzz.go b/fizzbuzz/FizzBuzz.go deleted file mode 100644 index b1da893..0000000 --- a/fizzbuzz/FizzBuzz.go +++ /dev/null @@ -1,35 +0,0 @@ -package fizzbuzz - -import "fmt" - -func main() { - // for i := 1; i < 101; i++ { - // if i%15 == 0 { - // fmt.Print("FizzBuzz\n") - // } else if i%3 == 0 { - // fmt.Print("Fizz\n") - // } else if i%5 == 0 { - // fmt.Print("Buzz\n") - // } else { - // fmt.Println(i) - // } - // } - - switchFB() -} - -func switchFB() { - for i := 1; i < 101; i++ { - switch { - case i%15 == 0: - fmt.Println("FizzBuzz") - case i%3 == 0: - fmt.Println("Fizz") - case i%5 == 0: - fmt.Println("Buzz") - default: - fmt.Println(i) - } - } - -} diff --git a/main.go b/main.go index 2712b7b..7415f00 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,13 @@ package main import ( "fmt" - "gotest/missing" + "gotest/consecutive" ) func main() { - fmt.Print(missing.FindNumber([]int{1, 3, 5, 2, 6, 7, -9, 8, 4})) + array := []int{1, 2, 4, 3, 6, 5} + + fmt.Println(consecutive.Consecutive(array)) } diff --git a/missing/missing.go b/missing/missing.go index 31611b3..34b568e 100644 --- a/missing/missing.go +++ b/missing/missing.go @@ -9,7 +9,7 @@ func FindNumber(A []int) int { for i := 0; i < len(A); i++ { if A[i] < 0 || A[i] > 100000 { - fmt.Print("array has negative element or longer than expected") + fmt.Print("array has negative element or longer than expected \n") return A[i] } else { elements_sum = elements_sum + A[i] diff --git a/missing/missing_test.go b/missing/missing_test.go index 4142715..8c85807 100644 --- a/missing/missing_test.go +++ b/missing/missing_test.go @@ -1 +1,51 @@ package missing_test + +import ( + "fmt" + "gotest/missing" + "testing" +) + +// Структура тестовых данных, input - входной массив, +// output - ожидаеммый вывод + +type testdata struct { + input []int + output int +} + +// Входной, повторения, ожидания + +var test = []testdata{ + {[]int{1, 2, 4, 3, 6}, 5}, + {[]int{1, 2, 3, 5}, 4}, + {[]int{1, 2, 4, 3, -6}, -6}, +} + +func TestMissing(t *testing.T) { + + t.Run( + "insert slices", func(t *testing.T) { + for _, data := range test { + // Передаём массив и повторения в функцию и записываем результат в переменную + v := missing.FindNumber(data.input) + // Сравниваем вывод с ожиданием + if v != data.output { + t.Error( + "For: ", data.input, + "Expected: ", data.output, + "Got: ", v, + ) + } else { + fmt.Println( + "For: ", data.input, + "Expected: ", data.output, + "Got: ", v, + ) + + } + } + }, + ) + +} diff --git a/solutions/solutions.go b/solutions/solutions.go new file mode 100644 index 0000000..e1e0aea --- /dev/null +++ b/solutions/solutions.go @@ -0,0 +1 @@ +package solutions diff --git a/solutions/task.go b/solutions/task.go new file mode 100644 index 0000000..1d2ba72 --- /dev/null +++ b/solutions/task.go @@ -0,0 +1,4 @@ +package solutions + +type Task struct { +} diff --git a/solutions/task_store.go b/solutions/task_store.go new file mode 100644 index 0000000..e1e0aea --- /dev/null +++ b/solutions/task_store.go @@ -0,0 +1 @@ +package solutions diff --git a/twonums/twonums.go b/twonums/twonums.go deleted file mode 100644 index 7401fa0..0000000 --- a/twonums/twonums.go +++ /dev/null @@ -1,23 +0,0 @@ -package twonums - -import "fmt" - -func main() { - var test_nums []int - test_nums = append(test_nums, 2, 3, 5, 6, 9, 1) - fmt.Print(TwoSum(test_nums, 10)) -} - -func TwoSum(nums []int, target int) []int { - - var result []int - - for i := 0; i < len(nums); i++ { - for j := i + 1; j < len(nums); j++ { - if nums[i]+nums[j] == target { - result = append(result, i, j) - } - } - } - return result -}