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..912213e --- /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.go b/module-2/task1/fibonacci/fibonacci.go new file mode 100644 index 0000000..9a88463 --- /dev/null +++ b/module-2/task1/fibonacci/fibonacci.go @@ -0,0 +1,49 @@ +package fibonacci + +import "fmt" + +// create type specific function for uint, int, float64 +func fibonnaciInt(i int) int { + if i == 0 { + return 0 + } + if i == 1 { + return 1 + } + return fibonnaciInt(i-1) + fibonnaciInt(i-2) +} + +func fibonnaciUint(u uint) uint { + if u == 0 { + return 0 + } + if u == 1 { + return 1 + } + return fibonnaciUint(u-1) + fibonnaciUint(u-2) +} + +func fibonnaciFloat(f float64) float64 { + if f == 0 { + return 0 + } + if f == 1 { + return 1 + } + return fibonnaciFloat(f-1) + fibonnaciFloat(f-2) +} + +func Fibonacci(i interface{}) interface{} { + switch v := i.(type) { + case int: + return fibonnaciInt(v) + case uint: + return fibonnaciUint(v) + case float64: + return fibonnaciFloat(v) + default: + fmt.Println(v) + } + return nil + +} diff --git a/module-2/task1/fibonacci/fibonacci_test.go b/module-2/task1/fibonacci/fibonacci_test.go new file mode 100644 index 0000000..7d3197c --- /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) + } + +} diff --git a/module-2/task1/go.mod b/module-2/task1/go.mod new file mode 100644 index 0000000..e566cc1 --- /dev/null +++ b/module-2/task1/go.mod @@ -0,0 +1,3 @@ +module computator + +go 1.16 diff --git a/module-2/task1/main.go b/module-2/task1/main.go new file mode 100644 index 0000000..7d07711 --- /dev/null +++ b/module-2/task1/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "fmt" + "os" + "regexp" + "strconv" + + "computator/fibonacci" +) + +func main() { + var what string + + // enter our something + fmt.Println("Please enter {int|uint|float64}") + fmt.Scanf("%s\n", &what) + + //what is it actually? + isGood, err := regexp.MatchString(`^(-?)[0-9\.]+$`, what) + if err != nil { + fmt.Println(err) + } + + if !isGood { + os.Exit(1) + } + + isInt, err := regexp.Match(`^(-?)[0-9]+$`, []byte(what)) + isUint, err := regexp.Match(`^[0-9]+$`, []byte(what)) + + switch true { + case isInt: + i, err := strconv.Atoi(what) + if err != nil { + os.Exit(1) + } + fmt.Println(fibonacci.Fibonacci(i)) + case isUint: + i, err := strconv.ParseUint(what, 10, 32) + if err != nil { + os.Exit(1) + } + fmt.Println(fibonacci.Fibonacci(uint(i))) + default: + i, err := strconv.ParseFloat(what, 64) + if err != nil { + os.Exit(1) + } + fmt.Println(fibonacci.Fibonacci(i)) + + } +}