From 9c1d5f0f607b7ce2a1647ee6ab5a59cd0d7fba03 Mon Sep 17 00:00:00 2001 From: IlyaGrushevsky <96526822+IlyaGrushevsky@users.noreply.github.com> Date: Thu, 2 Jun 2022 15:29:48 +0300 Subject: [PATCH 1/8] Add files via upload --- go.mod | 3 +++ task01-arrays.go | 8 ++++++-- task02-slice.go | 9 +++++++-- task03-map.go | 13 +++++++++++-- 4 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 go.mod diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..cab0831 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module golang-united-school-homework-3- + +go 1.17 diff --git a/task01-arrays.go b/task01-arrays.go index b9cd9e9..f98e6e0 100644 --- a/task01-arrays.go +++ b/task01-arrays.go @@ -1,6 +1,10 @@ package homework func average(input [15]float32) (result float32) { - //Place your code here - return + for _, v := range input { + var sum float32 + sum += v + result = sum / 4 + } + return result } diff --git a/task02-slice.go b/task02-slice.go index 1db4004..f7551b4 100644 --- a/task02-slice.go +++ b/task02-slice.go @@ -1,6 +1,11 @@ package homework +import "sort" + func reverse(input []int64) (result []int64) { - //Place your code here - return + sort.IntSlice + for i := range input { + result = append(result, input[len(input)-1-i]) + } + return result } diff --git a/task03-map.go b/task03-map.go index 903e118..64ffb86 100644 --- a/task03-map.go +++ b/task03-map.go @@ -1,6 +1,15 @@ package homework +import "sort" + func sortMapValues(input map[int]string) (result []string) { - //Place your code here - return + var preresult []int + for i := range input { + preresult = append(preresult, i) + } + preresult = sort.IntSlice(preresult) + for v := range preresult { + result = append(result, input[v]) + } + return result } From 9286cbfe670e5e3a8ba2f56faaca2b47d5478c0f Mon Sep 17 00:00:00 2001 From: IlyaGrushevsky <96526822+IlyaGrushevsky@users.noreply.github.com> Date: Tue, 7 Jun 2022 13:51:52 +0300 Subject: [PATCH 2/8] Add files via upload --- task02-slice.go | 19 +++++++++++++++---- task03-map.go | 4 ++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/task02-slice.go b/task02-slice.go index f7551b4..d125411 100644 --- a/task02-slice.go +++ b/task02-slice.go @@ -2,10 +2,21 @@ package homework import "sort" +type Inputing []int64 + +func (a Inputing) Len() int { + return len(a) +} +func (a Inputing) Swap(i, j int) { + a[i], a[j] = a[j], a[i] +} +func (a Inputing) Less(i, j int) bool { + return a[i] > a[j] +} + func reverse(input []int64) (result []int64) { - sort.IntSlice - for i := range input { - result = append(result, input[len(input)-1-i]) - } + copy_input := make([]int64, 5) + copy(copy_input, input) + sort.Sort(Inputing(copy_input)) return result } diff --git a/task03-map.go b/task03-map.go index 64ffb86..cc38bfa 100644 --- a/task03-map.go +++ b/task03-map.go @@ -3,11 +3,11 @@ package homework import "sort" func sortMapValues(input map[int]string) (result []string) { - var preresult []int + var preresult []string for i := range input { preresult = append(preresult, i) } - preresult = sort.IntSlice(preresult) + sort.Strings(preresult) for v := range preresult { result = append(result, input[v]) } From c0bf7c886de0751bf5972e1f4caa6b807b1e9d82 Mon Sep 17 00:00:00 2001 From: IlyaGrushevsky <96526822+IlyaGrushevsky@users.noreply.github.com> Date: Wed, 8 Jun 2022 11:14:08 +0300 Subject: [PATCH 3/8] Add files via upload --- task03-map.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/task03-map.go b/task03-map.go index cc38bfa..548e423 100644 --- a/task03-map.go +++ b/task03-map.go @@ -3,11 +3,11 @@ package homework import "sort" func sortMapValues(input map[int]string) (result []string) { - var preresult []string + var preresult []int for i := range input { preresult = append(preresult, i) } - sort.Strings(preresult) + sort.Ints(preresult) for v := range preresult { result = append(result, input[v]) } From 6d34b0c2c198e6712c7d25934b5b3beafd04fdac Mon Sep 17 00:00:00 2001 From: IlyaGrushevsky <96526822+IlyaGrushevsky@users.noreply.github.com> Date: Wed, 8 Jun 2022 11:51:25 +0300 Subject: [PATCH 4/8] Add files via upload --- task01-arrays.go | 12 +++++++++--- task02-slice.go | 8 ++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/task01-arrays.go b/task01-arrays.go index f98e6e0..b2aa8dc 100644 --- a/task01-arrays.go +++ b/task01-arrays.go @@ -1,10 +1,16 @@ package homework func average(input [15]float32) (result float32) { + var sum float32 + var N float32 for _, v := range input { - var sum float32 - sum += v - result = sum / 4 + if v > 0 { + sum += v + N++ + } else { + break + } } + result = sum / N return result } diff --git a/task02-slice.go b/task02-slice.go index d125411..1efd162 100644 --- a/task02-slice.go +++ b/task02-slice.go @@ -11,12 +11,12 @@ func (a Inputing) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a Inputing) Less(i, j int) bool { - return a[i] > a[j] + return a[i] < a[j] } func reverse(input []int64) (result []int64) { - copy_input := make([]int64, 5) - copy(copy_input, input) - sort.Sort(Inputing(copy_input)) + copyinput := make([]int64, 5) + copy(copyinput, input) + sort.Sort(sort.Reverse(Inputing(copyinput))) return result } From 0b82dd6c37bd01f1af9a94b1a443403a01e29510 Mon Sep 17 00:00:00 2001 From: IlyaGrushevsky <96526822+IlyaGrushevsky@users.noreply.github.com> Date: Wed, 8 Jun 2022 15:58:37 +0300 Subject: [PATCH 5/8] Add files via upload --- task03-map.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task03-map.go b/task03-map.go index 548e423..8add42b 100644 --- a/task03-map.go +++ b/task03-map.go @@ -8,7 +8,7 @@ func sortMapValues(input map[int]string) (result []string) { preresult = append(preresult, i) } sort.Ints(preresult) - for v := range preresult { + for _, v := range preresult { result = append(result, input[v]) } return result From e752636f26ed54a2f481ca10e424c30a7746a92a Mon Sep 17 00:00:00 2001 From: IlyaGrushevsky <96526822+IlyaGrushevsky@users.noreply.github.com> Date: Wed, 8 Jun 2022 16:33:03 +0300 Subject: [PATCH 6/8] Add files via upload --- task02-slice.go | 1 + 1 file changed, 1 insertion(+) diff --git a/task02-slice.go b/task02-slice.go index 1efd162..3973fb4 100644 --- a/task02-slice.go +++ b/task02-slice.go @@ -18,5 +18,6 @@ func reverse(input []int64) (result []int64) { copyinput := make([]int64, 5) copy(copyinput, input) sort.Sort(sort.Reverse(Inputing(copyinput))) + result = copyinput return result } From c164f5577abdc87f4f91986b370dd67e0e4fde14 Mon Sep 17 00:00:00 2001 From: IlyaGrushevsky <96526822+IlyaGrushevsky@users.noreply.github.com> Date: Fri, 17 Jun 2022 15:43:46 +0300 Subject: [PATCH 7/8] Add files via upload --- task02-slice.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/task02-slice.go b/task02-slice.go index 3973fb4..6668ef9 100644 --- a/task02-slice.go +++ b/task02-slice.go @@ -11,11 +11,11 @@ func (a Inputing) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a Inputing) Less(i, j int) bool { - return a[i] < a[j] + return i < j } func reverse(input []int64) (result []int64) { - copyinput := make([]int64, 5) + copyinput := make([]int64, len(input)) copy(copyinput, input) sort.Sort(sort.Reverse(Inputing(copyinput))) result = copyinput From 1c1e95cd9ac24bb1c1b016567966752cab715491 Mon Sep 17 00:00:00 2001 From: IlyaGrushevsky <96526822+IlyaGrushevsky@users.noreply.github.com> Date: Wed, 22 Jun 2022 14:32:04 +0300 Subject: [PATCH 8/8] Add files via upload --- task02-slice.go | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/task02-slice.go b/task02-slice.go index 6668ef9..e3b962f 100644 --- a/task02-slice.go +++ b/task02-slice.go @@ -2,22 +2,10 @@ package homework import "sort" -type Inputing []int64 - -func (a Inputing) Len() int { - return len(a) -} -func (a Inputing) Swap(i, j int) { - a[i], a[j] = a[j], a[i] -} -func (a Inputing) Less(i, j int) bool { - return i < j -} - func reverse(input []int64) (result []int64) { - copyinput := make([]int64, len(input)) - copy(copyinput, input) - sort.Sort(sort.Reverse(Inputing(copyinput))) - result = copyinput + sort.Slice(input, func(i, j int) bool { + return input[i] > input[j] + }) + result = input return result }