Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions module-2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Tasks:

* Fibonacci calculator [task description](task1/README.md)

20 changes: 20 additions & 0 deletions module-2/task1/README.md
Original file line number Diff line number Diff line change
@@ -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) {...}
```
49 changes: 49 additions & 0 deletions module-2/task1/fibonacci/fibonacci.go
Original file line number Diff line number Diff line change
@@ -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
}
Comment on lines +27 to +32
Copy link

Choose a reason for hiding this comment

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

Suggested change
if f == 0 {
return 0
}
if f == 1 {
return 1
}
if f < 2 {
return math.Floor(f)
}

Copy link

Choose a reason for hiding this comment

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

Try to explain what do we want to fix by this change

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

}
36 changes: 36 additions & 0 deletions module-2/task1/fibonacci/fibonacci_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

}
3 changes: 3 additions & 0 deletions module-2/task1/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module computator

go 1.16
53 changes: 53 additions & 0 deletions module-2/task1/main.go
Original file line number Diff line number Diff line change
@@ -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))

}
}