add forgoten tests

This commit is contained in:
Илья Смышляев 2022-09-30 23:35:21 +03:00
parent 5374dd7375
commit 2a6f023fa6
12 changed files with 147 additions and 61 deletions

BIN
.DS_Store vendored

Binary file not shown.

35
consecutive/cons.go Normal file
View File

@ -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
}

51
consecutive/cons_test.go Normal file
View File

@ -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,
)
}
}
},
)
}

BIN
fizzbuzz/.DS_Store vendored

Binary file not shown.

View File

@ -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)
}
}
}

View File

@ -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))
}

View File

@ -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]

View File

@ -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,
)
}
}
},
)
}

1
solutions/solutions.go Normal file
View File

@ -0,0 +1 @@
package solutions

4
solutions/task.go Normal file
View File

@ -0,0 +1,4 @@
package solutions
type Task struct {
}

1
solutions/task_store.go Normal file
View File

@ -0,0 +1 @@
package solutions

View File

@ -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
}