add forgoten tests
This commit is contained in:
parent
5374dd7375
commit
2a6f023fa6
|
@ -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
|
||||
}
|
|
@ -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,
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
}
|
Binary file not shown.
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
6
main.go
6
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))
|
||||
|
||||
}
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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,
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
package solutions
|
|
@ -0,0 +1,4 @@
|
|||
package solutions
|
||||
|
||||
type Task struct {
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
package solutions
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue