From a37eba572ee70eb703ed9f79bb4cb733dddb3c0d Mon Sep 17 00:00:00 2001 From: Aliaksei Burau Date: Mon, 7 Jun 2021 16:19:56 +0200 Subject: [PATCH 1/6] add task and test --- module-2/README.md | 4 +++ module-2/task1/README.md | 20 ++++++++++++ module-2/task1/fibonacci/fibonacci_test.go | 36 ++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 module-2/README.md create mode 100644 module-2/task1/README.md create mode 100644 module-2/task1/fibonacci/fibonacci_test.go diff --git a/module-2/README.md b/module-2/README.md new file mode 100644 index 0000000..c96bdd6 --- /dev/null +++ b/module-2/README.md @@ -0,0 +1,4 @@ +# Tasks: + +* Fibonacci calculator [task description](task1/README.md) + diff --git a/module-2/task1/README.md b/module-2/task1/README.md new file mode 100644 index 0000000..77aa790 --- /dev/null +++ b/module-2/task1/README.md @@ -0,0 +1,20 @@ +# Task + +* Create go module `computator` +* Create package `fibonacci` +* Create type specific function for uint, int, float64 +* Create general function that accept and return an empty interface{} [What is the empty interfaces?](https://tour.golang.org/methods/14) +* In the general function using type switch call proper function depending on the type of the function parameter [What is the type switch?](https://tour.golang.org/methods/16) +* In computator module create file `main.go`, implement `func main() {...}` to run application from console [How to read number from the terminal](https://golang.org/pkg/fmt/#Scanf) +* In the main function read from the console user input, if the input not a number - return an error, if it is the number find most specific type from the list (uint, int, float64), convert input to the specific type and call `fibonacci` function + +Functions signature should be equals to functions presented below +``` go +func fibonacci(n interface{}) (interface{}) {...} + +func fibonnaciUint(n uint) (uint) {...} + +func fibonnaciInt(n int) (int) {...} + +func fibonnaciFloat(n float64) {...} +``` diff --git a/module-2/task1/fibonacci/fibonacci_test.go b/module-2/task1/fibonacci/fibonacci_test.go new file mode 100644 index 0000000..d539f9a --- /dev/null +++ b/module-2/task1/fibonacci/fibonacci_test.go @@ -0,0 +1,36 @@ +package fibonacci + +import ( + "reflect" + "testing" +) + +func TestFibonacci(t *testing.T) { + var nUint uint = 3 + rInterface := fibonacci(nUint) + + var expectedUint uint = 2 + if rUint, ok := rInterface.(uint); !ok || rUint != expectedUint { + t.Errorf("unexpected result for uint case, expected type - %s, got type - %s, expected result - %d, got result - %d", + reflect.TypeOf(expectedUint), reflect.TypeOf(rInterface), expectedUint, rUint) + } + + var nInt int = 5 + rInterface = fibonacci(nInt) + + var expectedInt int = 5 + if rInt, ok := rInterface.(int); !ok || rInt != expectedInt { + t.Errorf("unexpected result for int case, expected type - %s, got type - %s, expected result - %d, got result - %d", + reflect.TypeOf(expectedInt), reflect.TypeOf(rInterface), expectedInt, rInt) + } + + var nFloat64 float64 = 7.0 + rInterface = fibonacci(nFloat64) + + var expectedFloat64 float64 = 13.0 + if rFloat64, ok := rInterface.(float64); !ok || rFloat64 != expectedFloat64 { + t.Errorf("unexpected result for float64 case, expected type - %s, got type - %s, expected result - %f, got result - %f", + reflect.TypeOf(expectedFloat64), reflect.TypeOf(rInterface), expectedFloat64, rFloat64) + } + +} From dea4ddd7ee30f44c19f58bc6d67b932ca34541f7 Mon Sep 17 00:00:00 2001 From: Aliaksei Burau Date: Thu, 10 Jun 2021 11:04:34 +0200 Subject: [PATCH 2/6] make function public --- module-2/task1/README.md | 2 +- module-2/task1/fibonacci/fibonacci_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/module-2/task1/README.md b/module-2/task1/README.md index 77aa790..912213e 100644 --- a/module-2/task1/README.md +++ b/module-2/task1/README.md @@ -10,7 +10,7 @@ Functions signature should be equals to functions presented below ``` go -func fibonacci(n interface{}) (interface{}) {...} +func Fibonacci(n interface{}) (interface{}) {...} func fibonnaciUint(n uint) (uint) {...} diff --git a/module-2/task1/fibonacci/fibonacci_test.go b/module-2/task1/fibonacci/fibonacci_test.go index d539f9a..7d3197c 100644 --- a/module-2/task1/fibonacci/fibonacci_test.go +++ b/module-2/task1/fibonacci/fibonacci_test.go @@ -7,7 +7,7 @@ import ( func TestFibonacci(t *testing.T) { var nUint uint = 3 - rInterface := fibonacci(nUint) + rInterface := Fibonacci(nUint) var expectedUint uint = 2 if rUint, ok := rInterface.(uint); !ok || rUint != expectedUint { @@ -16,7 +16,7 @@ func TestFibonacci(t *testing.T) { } var nInt int = 5 - rInterface = fibonacci(nInt) + rInterface = Fibonacci(nInt) var expectedInt int = 5 if rInt, ok := rInterface.(int); !ok || rInt != expectedInt { @@ -25,7 +25,7 @@ func TestFibonacci(t *testing.T) { } var nFloat64 float64 = 7.0 - rInterface = fibonacci(nFloat64) + rInterface = Fibonacci(nFloat64) var expectedFloat64 float64 = 13.0 if rFloat64, ok := rInterface.(float64); !ok || rFloat64 != expectedFloat64 { From 057e0fe594ca57b0070f688c80baf5271f1d93b7 Mon Sep 17 00:00:00 2001 From: Michal Kupisinski Date: Wed, 14 Jul 2021 14:15:30 +0200 Subject: [PATCH 3/6] Task 1 for module 3 --- module-3/task1/go.mod | 3 +++ module-3/task1/main.go | 9 +++++++++ module-3/task1/pointers/pointers_test.go | 4 ++-- 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 module-3/task1/go.mod create mode 100644 module-3/task1/main.go diff --git a/module-3/task1/go.mod b/module-3/task1/go.mod new file mode 100644 index 0000000..45c1c10 --- /dev/null +++ b/module-3/task1/go.mod @@ -0,0 +1,3 @@ +module honestit/module3/task1 + +go 1.16 diff --git a/module-3/task1/main.go b/module-3/task1/main.go new file mode 100644 index 0000000..a868fca --- /dev/null +++ b/module-3/task1/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("Hello module-3") +} diff --git a/module-3/task1/pointers/pointers_test.go b/module-3/task1/pointers/pointers_test.go index d9e1c78..5a799bf 100644 --- a/module-3/task1/pointers/pointers_test.go +++ b/module-3/task1/pointers/pointers_test.go @@ -14,9 +14,9 @@ func Test_doubleValues(t *testing.T) { } for i, tt := range tests { - out, expected := *tt.in, *tt.expected + out, expected := tt.in, tt.expected if DoubleValues(tt.in); expected == out { - t.Errorf("Unexpected result in Test #%d, expected %+v, got %+v", i, tt.expected, *tt.in) + t.Errorf("Unexpected result in Test #%d, expected %+v, got %+v", i, tt.expected, tt.in) } } } From f81783de170542704eebcc8252d44c3847c23919 Mon Sep 17 00:00:00 2001 From: Michal Kupisinski Date: Wed, 14 Jul 2021 14:38:57 +0200 Subject: [PATCH 4/6] Task 2 for module 3 --- module-3/task2/go.mod | 3 +++ module-3/task2/main.go | 9 +++++++++ module-3/task2/slices/slice.go | 32 +++++++++++++++++++++++++++++--- 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 module-3/task2/go.mod create mode 100644 module-3/task2/main.go diff --git a/module-3/task2/go.mod b/module-3/task2/go.mod new file mode 100644 index 0000000..69e43d5 --- /dev/null +++ b/module-3/task2/go.mod @@ -0,0 +1,3 @@ +module honestit/module3/task2 + +go 1.16 diff --git a/module-3/task2/main.go b/module-3/task2/main.go new file mode 100644 index 0000000..c49ab7b --- /dev/null +++ b/module-3/task2/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("Hello task 2 of module 3") +} diff --git a/module-3/task2/slices/slice.go b/module-3/task2/slices/slice.go index 3e593be..25e7a38 100644 --- a/module-3/task2/slices/slice.go +++ b/module-3/task2/slices/slice.go @@ -1,10 +1,17 @@ package slices +import ( + "sort" +) + func SortStringsInPlace(s []string) { + sort.Strings(s) } func SortStringsPure(s []string) []string { - + copy := s + sort.Strings(copy) + return copy } type User struct { @@ -12,10 +19,29 @@ type User struct { LastName string } +type UserList []User + +func (u UserList) Len() int { + return len(u) +} + +func (u UserList) Less(i, j int) bool { + return u[i].FirstName < u[j].FirstName || (u[i].FirstName == u[j].FirstName && u[i].LastName < u[j].LastName) + +} + +func (u UserList) Swap(i, j int) { + u[i], u[j] = u[j], u[i] +} + func SortUsersPure(s []User) []User { - return r + var userList UserList = s + sort.Sort(userList) + return userList } func SortUsersPureDesc(s []User) []User { - return r + var userList UserList = s + sort.Sort(sort.Reverse(userList)) + return userList } From eb0e251f91b9896ddc3843e482ca0a8d43cc3212 Mon Sep 17 00:00:00 2001 From: Michal Kupisinski Date: Wed, 14 Jul 2021 14:54:58 +0200 Subject: [PATCH 5/6] Task 3 for module 3 --- module-3/task3/go.mod | 3 +++ module-3/task3/main.go | 9 +++++++++ module-3/task3/maps/maps.go | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 module-3/task3/go.mod create mode 100644 module-3/task3/main.go diff --git a/module-3/task3/go.mod b/module-3/task3/go.mod new file mode 100644 index 0000000..ce28f3d --- /dev/null +++ b/module-3/task3/go.mod @@ -0,0 +1,3 @@ +module honestit/module3/task3 + +go 1.16 diff --git a/module-3/task3/main.go b/module-3/task3/main.go new file mode 100644 index 0000000..826b99b --- /dev/null +++ b/module-3/task3/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("Hello from task 3 of module 3") +} diff --git a/module-3/task3/maps/maps.go b/module-3/task3/maps/maps.go index 9d577ed..1cc5af1 100644 --- a/module-3/task3/maps/maps.go +++ b/module-3/task3/maps/maps.go @@ -1,4 +1,23 @@ package maps +import ( + "fmt" + "sort" + "strings" +) + func SortedValues(m map[int]string) string { + var keys []int + for key := range m { + keys = append(keys, key) + } + sort.Ints(keys) + fmt.Printf("Keys: %v\n", keys) + + var sb strings.Builder + for _, key := range keys { + val := m[key] + sb.WriteString(val) + } + return sb.String() } From 791340a33651c63a8e6ecfe0d8c562cff358abb9 Mon Sep 17 00:00:00 2001 From: Michal Kupisinski Date: Wed, 14 Jul 2021 15:10:42 +0200 Subject: [PATCH 6/6] Some cleaning --- module-2/README.md | 4 --- module-2/task1/README.md | 20 ------------ module-2/task1/fibonacci/fibonacci_test.go | 36 ---------------------- 3 files changed, 60 deletions(-) delete mode 100644 module-2/README.md delete mode 100644 module-2/task1/README.md delete mode 100644 module-2/task1/fibonacci/fibonacci_test.go diff --git a/module-2/README.md b/module-2/README.md deleted file mode 100644 index c96bdd6..0000000 --- a/module-2/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# Tasks: - -* Fibonacci calculator [task description](task1/README.md) - diff --git a/module-2/task1/README.md b/module-2/task1/README.md deleted file mode 100644 index 912213e..0000000 --- a/module-2/task1/README.md +++ /dev/null @@ -1,20 +0,0 @@ -# Task - -* Create go module `computator` -* Create package `fibonacci` -* Create type specific function for uint, int, float64 -* Create general function that accept and return an empty interface{} [What is the empty interfaces?](https://tour.golang.org/methods/14) -* In the general function using type switch call proper function depending on the type of the function parameter [What is the type switch?](https://tour.golang.org/methods/16) -* In computator module create file `main.go`, implement `func main() {...}` to run application from console [How to read number from the terminal](https://golang.org/pkg/fmt/#Scanf) -* In the main function read from the console user input, if the input not a number - return an error, if it is the number find most specific type from the list (uint, int, float64), convert input to the specific type and call `fibonacci` function - -Functions signature should be equals to functions presented below -``` go -func Fibonacci(n interface{}) (interface{}) {...} - -func fibonnaciUint(n uint) (uint) {...} - -func fibonnaciInt(n int) (int) {...} - -func fibonnaciFloat(n float64) {...} -``` diff --git a/module-2/task1/fibonacci/fibonacci_test.go b/module-2/task1/fibonacci/fibonacci_test.go deleted file mode 100644 index 7d3197c..0000000 --- a/module-2/task1/fibonacci/fibonacci_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package fibonacci - -import ( - "reflect" - "testing" -) - -func TestFibonacci(t *testing.T) { - var nUint uint = 3 - rInterface := Fibonacci(nUint) - - var expectedUint uint = 2 - if rUint, ok := rInterface.(uint); !ok || rUint != expectedUint { - t.Errorf("unexpected result for uint case, expected type - %s, got type - %s, expected result - %d, got result - %d", - reflect.TypeOf(expectedUint), reflect.TypeOf(rInterface), expectedUint, rUint) - } - - var nInt int = 5 - rInterface = Fibonacci(nInt) - - var expectedInt int = 5 - if rInt, ok := rInterface.(int); !ok || rInt != expectedInt { - t.Errorf("unexpected result for int case, expected type - %s, got type - %s, expected result - %d, got result - %d", - reflect.TypeOf(expectedInt), reflect.TypeOf(rInterface), expectedInt, rInt) - } - - var nFloat64 float64 = 7.0 - rInterface = Fibonacci(nFloat64) - - var expectedFloat64 float64 = 13.0 - if rFloat64, ok := rInterface.(float64); !ok || rFloat64 != expectedFloat64 { - t.Errorf("unexpected result for float64 case, expected type - %s, got type - %s, expected result - %f, got result - %f", - reflect.TypeOf(expectedFloat64), reflect.TypeOf(rInterface), expectedFloat64, rFloat64) - } - -}