add some exaples, rotation this is for go course,
twonums this is a problem from leetcode
This commit is contained in:
parent
e01c18dc10
commit
6e5b860955
|
@ -0,0 +1,35 @@
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,55 @@
|
||||||
|
package rotation_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"gotest/rotation"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Структура тестовых данных, input - входной массив,
|
||||||
|
// iteration - колличество повторений,
|
||||||
|
// output - ожидаеммый вывод
|
||||||
|
|
||||||
|
type testdata struct {
|
||||||
|
input []int
|
||||||
|
iteration int
|
||||||
|
output []int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Входной, повторения, ожидания
|
||||||
|
|
||||||
|
var test = []testdata{
|
||||||
|
{[]int{3, 8, 9, 7, 6}, 3, []int{9, 7, 6, 3, 8}},
|
||||||
|
{[]int{1, 2, 3, 4}, 4, []int{1, 2, 3, 4}},
|
||||||
|
{[]int{1, 2, 3, -1000}, 1, []int{-1000, 1, 2, 3}},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRotation(t *testing.T) {
|
||||||
|
|
||||||
|
t.Run(
|
||||||
|
"insert slices", func(t *testing.T) {
|
||||||
|
for _, data := range test {
|
||||||
|
// Передаём массив и повторения в функцию и записываем результат в переменную
|
||||||
|
v := rotation.Rotation(data.input, data.iteration)
|
||||||
|
// Сравниваем вывод с ожиданием
|
||||||
|
if reflect.DeepEqual(v, data.output) != true {
|
||||||
|
t.Error(
|
||||||
|
"For: ", data.input,
|
||||||
|
"Expected: ", data.output,
|
||||||
|
"Got: ", v,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
fmt.Println(
|
||||||
|
"For: ", data.input,
|
||||||
|
"Iterations", data.iteration,
|
||||||
|
"Expected: ", data.output,
|
||||||
|
"Got: ", v,
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
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