forked from burov/homework
-
Notifications
You must be signed in to change notification settings - Fork 0
Module 2 #1
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
Open
wodowod
wants to merge
3
commits into
master
Choose a base branch
from
module-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Module 2 #1
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Tasks: | ||
|
|
||
| * Fibonacci calculator [task description](task1/README.md) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) {...} | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| 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 | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module computator | ||
|
|
||
| go 1.16 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) | ||
|
|
||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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