Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Перенёс задания А и Б в свои функции, добавил тесты программы при запуске main функции #152

Open
wants to merge 32 commits into
base: Chadov_Sergej_Vadimovich
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
cd08682
Переменные А и Б теперь в main, задания А и Б разделены по функциям
fagirton Nov 16, 2022
4339ac5
Merge branch 'Chadov_Sergej_Vadimovich' of https://github.com/fagirto…
fagirton Nov 16, 2022
d78dc16
Merge branch 'Chadov_Sergej_Vadimovich' of https://github.com/fagirto…
fagirton Nov 16, 2022
b6c0e76
Попытка в импровизированные тесты заданий А и Б
fagirton Nov 17, 2022
19b4f2f
Решение заданий с Codewars, также убрал повторяющиеся выводы в консоль
fagirton Nov 23, 2022
379dbb0
Перенес функции из main.go
fagirton Nov 23, 2022
58c9e06
Неужели нормальные тесты? + Нерабочая реализация Game of Life
fagirton Nov 28, 2022
024edad
Пока без gameOfLife потому что cognitive complexity 57 ((
fagirton Nov 28, 2022
08048ae
Снова исправления, чтобы порадовать линтер
fagirton Nov 28, 2022
c03f80c
Разделение тестов по заданиям
fagirton Nov 28, 2022
1343c2d
Проблемы с параллельностью?
fagirton Nov 28, 2022
af70f57
Тесты, тесты и снова доделываем тесты
fagirton Nov 30, 2022
877752e
Линтер, снова
fagirton Nov 30, 2022
38f0853
Гитигнор не хотел игнорить настройки VSC
fagirton Nov 30, 2022
efa2b1d
Добавил решения последних двух задач с codewars
fagirton Dec 1, 2022
18abfc8
Merge branch 'Chadov_Sergej_Vadimovich' of https://github.com/fagirto…
fagirton Dec 1, 2022
1a7e4aa
А вот и первое задание с codewars с польской коровой
fagirton Dec 1, 2022
b52c100
- Ненужные комментарии
fagirton Dec 2, 2022
2997edc
Исправил, но ещё не всё (есть код в комментах)
fagirton Dec 2, 2022
8a3b48b
Штуки с тестами
fagirton Dec 3, 2022
dd3fcea
Game Of Life (пока не готов - с проблемами)
fagirton Dec 3, 2022
71aa1a6
Linter moment
fagirton Dec 3, 2022
babd06e
Linter moment 2
fagirton Dec 3, 2022
52aa7f4
СКОБКИ. ВСЕМУ ВИНОЙ БЫЛИ СКОБКИ.
fagirton Dec 17, 2022
2384469
Опять не понял почему не работает
fagirton Dec 17, 2022
6806f27
GAME OF LIFE РАБОТАЕТ??? Не окончательная версия
fagirton Dec 24, 2022
1b07799
Линтер не хочет признавать мою победу
fagirton Dec 24, 2022
6a32963
Простая внутренняя проверка сетки для Game of Life
fagirton Dec 24, 2022
7a6bf05
Небольшие исправления в GoL
fagirton Dec 24, 2022
527741c
ОНО ПРАВДА РАБОТАЕТ, КАК НАДО (осталось переделать функцию проверки с…
fagirton Dec 28, 2022
fdb6a1f
Небольшой Clean-up в main, так и не сделал проверку сетки в GoL
fagirton Dec 28, 2022
5748713
Чайник с функцией Dead Inside
fagirton Jan 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang/.vscode/
golang/пускай подумает о своем поведении/
jskonst marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions golang/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module isuct.ru/informatics2022

go 1.16

require github.com/stretchr/testify v1.8.1 // indirect
17 changes: 17 additions & 0 deletions golang/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
87 changes: 87 additions & 0 deletions golang/internal/codewars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package internal

import (
"math"
"strings"
)

func EvenOrOdd(num int) string {
if num%2 == 0 {
return "Even"
} else {
return "Odd"
}
}

func CountingSheep(arr []bool) int {
var res int
var i int
for ; i < len(arr); i++ {
if arr[i] && (arr[i] || !arr[i]) {
res++
}
}
return res
}

func MonkeysCount(num int) []int {
var res []int
for n := 1; n <= num; n++ {
res = append(res, n)
}
return res
}

func PaperworkCount(n int, m int) int {
if n < 0 || m < 0 {
return 0
}
return n * m
}

func HeroWithGunShootsDragons(ammo int, dragons int) bool {
return ammo/2 >= dragons
}

//Codewars 2: Возвращение

func PolishCow(text string) string {
var symbols = [][]string{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔕 используйте словарь

{"ą", "a"},
{"ć", "c"},
{"ę", "e"},
{"ł", "l"},
{"ń", "n"},
{"ó", "o"},
{"ź", "z"},
{"ż", "z"},
}
for i := 0; i < len(symbols); i++ {
text = strings.Replace(text, symbols[i][0], symbols[i][1], -1)
}
return text
}

func FindAll(arr []string, search string) []int {
var res []int
for i := 0; i < len(arr); i++ {
if arr[i] == search {
res = append(res, i)
}
}
return res
}

func SumOfMin(arr [][]int, m int, n int) int {
var res int
for i := 0; i < m; i++ {
min := math.Inf(1)
for a := 0; a < n; a++ {
if float64(arr[i][a]) < min {
min = float64(arr[i][a])
}
}
res = res + int(min)
}
return res
}
76 changes: 76 additions & 0 deletions golang/internal/gameOfLife_true.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package internal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

На будущее - это бы в отдельный PR - только этот файл - потому что сейчас я не могу другие задачи завести не проверив эту :(


import "fmt"

func GameOfLife(s int, width int, height int, grid [][]byte) {
for i := 0; i < s; i++ {
grid = GameOfLifeStep(width, height, grid)
printGrid(width, height, grid, i+1)
}
}

func printGrid(width int, height int, grid [][]byte, step int) [][]byte {
fmt.Println("Step ", step)
for y := 0; y <= height+1; y++ {
for x := 0; x <= width+1; x++ {
switch grid[x][y] {
case byte(1):
fmt.Printf("O")
case byte(0):
fmt.Printf("#")
case byte(2):
fmt.Printf("+")
}
}
fmt.Println()
}
return grid
}

func GameOfLifeStep(width int, height int, grid [][]byte) [][]byte {
var PosX int = 0
var PosY int = 1
var nb int
var overwrite_pos [][]int
for i := 1; i <= width*height; i++ {
PosX++
if PosX > width {
PosY++
if PosY > height {
PosY = 0
}
PosX = 1
fmt.Println()
}
nb_grid := [][]int{
{-1, 1},
{-1, 0},
{-1, 1},
{0, -1},
{0, 1},
{1, -1},
{1, 0},
{1, 1},
}
for i := 0; i < len(nb_grid); i++ {
if grid[PosX+nb_grid[i][0]][PosY+nb_grid[i][1]] == byte(1) {
nb++
}
}
if grid[PosX][PosY] == byte(0) && nb == 3 {
overwrite_pos = append(overwrite_pos, []int{PosX, PosY, 1})
} else {
if grid[PosX][PosY] == byte(1) && nb == 3 || nb == 2 {
overwrite_pos = append(overwrite_pos, []int{PosX, PosY, 1})
} else {
overwrite_pos = append(overwrite_pos, []int{PosX, PosY, 0})
}
}
nb = 0
}

for oi := 0; oi < len(overwrite_pos); oi++ {
grid[overwrite_pos[oi][0]][overwrite_pos[oi][1]] = byte(overwrite_pos[oi][2])
}
return grid
}
5 changes: 0 additions & 5 deletions golang/internal/sample.go

This file was deleted.

23 changes: 23 additions & 0 deletions golang/internal/tasks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package internal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Принято - все ок


import "math"

func Formula(x float64, a float64, b float64) float64 {
var y float64 = ((a*a*a)*(math.Cbrt(x)) - b*(math.Log10(x)/math.Log10(5))) / (math.Pow(math.Log10(x-1), 3))
return y
}

func TaskA(a float64, b float64, xn float64, xk float64, xd float64) []float64 {
var res []float64
for i := xn; i < xk; i = i + xd {
res = append(res, Formula(i, a, b))
}
return res
}
func TaskB(a float64, b float64, x []float64) []float64 {
var res []float64
for i := 0; i < len(x); i++ {
res = append(res, Formula(x[i], a, b))
}
return res
}
64 changes: 47 additions & 17 deletions golang/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,56 @@ package main

import (
"fmt"
"math"
"strconv"

"isuct.ru/informatics2022/internal"
)

func main() {
fmt.Println("Задание А")
var i float64 = 1.5
for ; i < 3.5; i = i + 0.4 {
fmt.Println(formula(i))
const a float64 = 4.1
const b float64 = 2.7
const xn float64 = 1.5
const xk float64 = 3.5
const xd float64 = 0.4

fmt.Println(internal.TaskA(a, b, xn, xk, xd))
fmt.Println(internal.TaskB(a, b, []float64{1.9, 2.15, 2.34, 2.74, 3.16}))

fmt.Println("Задания с Codewars")
fmt.Println(internal.EvenOrOdd(16))
fmt.Println(internal.CountingSheep([]bool{true, false, true, true, true}))
fmt.Println(internal.MonkeysCount(19))
fmt.Println(internal.PaperworkCount(23, 2))
fmt.Println(internal.HeroWithGunShootsDragons(37, 20))

sample_grid := [][]byte{
{2, 2, 2, 2, 2, 2, 2},
{2, 0, 0, 0, 0, 0, 2},
{2, 0, 0, 1, 0, 0, 2},
{2, 1, 0, 1, 0, 0, 2},
{2, 0, 1, 1, 0, 0, 2},
{2, 0, 0, 0, 0, 0, 2},
{2, 2, 2, 2, 2, 2, 2},
}
fmt.Println("Задание В")
fmt.Println(formula(1.9))
fmt.Println(formula(2.15))
fmt.Println(formula(2.34))
fmt.Println(formula(2.74))
fmt.Println(formula(3.16))
}

func formula(x float64) float64 {
var a float64 = 4.1
var b float64 = 2.7
var y float64 = (a*(math.Cbrt(x)) - b*(math.Log10(x)/math.Log10(5))) / (math.Pow(math.Log10(x-1), 3))
return y
fmt.Println(internal.PolishCow("Gdzie jest biały węgorz?"))

//Code to convert []int to []string
nums := []int{6, 9, 3, 4, 3, 82, 11}
sNums := make([]string, len(nums))
for i, x := range nums {
sNums[i] = strconv.Itoa(x)
}

fmt.Println(internal.FindAll(sNums, fmt.Sprint(3)))

var exampleArr = [][]int{
{1, 2, 3, 4, 5},
{5, 6, 7, 8, 9},
{20, 21, 34, 56, 100},
}

fmt.Println(internal.SumOfMin(exampleArr, 3, 5))

internal.GameOfLife(1, 5, 5, sample_grid)
}
14 changes: 0 additions & 14 deletions golang/tests/sample_test.go

This file was deleted.

66 changes: 66 additions & 0 deletions golang/tests/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main_test

import (
"testing"

"github.com/stretchr/testify/assert"
"isuct.ru/informatics2022/internal"
)

func TestTaskA(t *testing.T) {
t.Parallel()

var resultA float64 = -2867.204

const a float64 = 4.1
const b float64 = 2.7
const xn float64 = 1.5
const xk float64 = 3.5
const xd float64 = 0.4

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добавьте отдельные тесты на формулу
В этих тестах проверьте возможные значения передаваемые в формулу, особенно критические

var testResA []float64 = internal.TaskA(a, b, xn, xk, xd)

t.Run("Check 1 value", func(t *testing.T) {
t.Parallel()
jskonst marked this conversation as resolved.
Show resolved Hide resolved
assert.InDelta(t, resultA, testResA[1], 0.001)
})
t.Run("Check xn, xk and xd > 0", func(t *testing.T) {
t.Parallel()
assert.Equal(t, true, (xn < xk && xd > 0) || (xn > xk && xd < 0))
})
t.Run("Check xd compared to xk and xn", func(t *testing.T) {
t.Parallel()
assert.Equal(t, true, (xd < xk-xn) || (xd > xn-xk))
})
t.Run("Check xd compared to xk and xn", func(t *testing.T) {
t.Parallel()
assert.InDelta(t, 6, len(testResA), 0)
})
}

func TestTaskB(t *testing.T) {
t.Parallel()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 - дассив длины >0
2 - пустой массив

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Немного не понял, тест должен проверять какой длинны входной массив? Или выходной массив?
И какой смысл проверять на пустоту массива, если первым уже проверили, что его длина больше нуля?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Выходной


var resultB float64 = -879771.242

const a float64 = 4.1
const b float64 = 2.7

var testResB []float64 = internal.TaskB(a, b, []float64{1.9, 2.15, 2.34, 2.74, 3.16})

assert.InDelta(t, resultB, testResB[1], 0.001)

assert.Equal(t, 6, len(testResB))
assert.Equal(t, []float64{}, internal.TaskB(a, b, []float64{}))
}

func TestFormula(t *testing.T) {
t.Parallel()

const a float64 = 4.1
const b float64 = 2.7
// Не уверен, что строка ниже вообще будет работать, но пусть будет
assert.Nil(t, internal.Formula(1, a, b))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вы правы - не надо это


assert.InDelta(t, 3576.30, internal.Formula(3, a, b), 0.01)
}