From 783c5d9fa3e0374f592847984d6e16d70839cace Mon Sep 17 00:00:00 2001 From: Aliaksei Burau Date: Thu, 8 Apr 2021 20:46:50 +0200 Subject: [PATCH 1/2] Update README.md --- ls_0/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ls_0/README.md b/ls_0/README.md index 459b7a3..e1b99b4 100644 --- a/ls_0/README.md +++ b/ls_0/README.md @@ -1,11 +1,11 @@ # Introduction task -The purpose of this task is to figure out with basic tooling of the Golang as well as check your IDE setup. +The purpose of this task is to figure out how to use basic tooling of the Golang as well as check your IDE setup. Task: * Initialize project with Go modules * Add dependency "github.com/kyokomi/emoji" to add emojy into the string * Using Sprint function from this package build a message "Hello from 🇵🇱!" -To run tests for run cmd "cmd test -v ." -Tasks considered as completed in case tests are not failed +To run tests for run cmd "go test -v ." +Tasks considered as completed in case test is not failed From b275fbcd27f3107ee373591c34025649bc8f36d7 Mon Sep 17 00:00:00 2001 From: Michal Kupisinski Date: Wed, 14 Jul 2021 14:03:28 +0200 Subject: [PATCH 2/2] Task for module 2 --- go.mod | 5 +++ go.sum | 2 + module-2/task1/fibonacci/fibonacci.go | 53 +++++++++++++++++++++++++++ module-2/task1/go.mod | 3 ++ module-2/task1/main.go | 35 ++++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 module-2/task1/fibonacci/fibonacci.go create mode 100644 module-2/task1/go.mod create mode 100644 module-2/task1/main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..572e28d --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module com.github/honestit/homework + +go 1.16 + +require github.com/kyokomi/emoji v2.2.4+incompatible diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b4d9b18 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/kyokomi/emoji v2.2.4+incompatible h1:np0woGKwx9LiHAQmwZx79Oc0rHpNw3o+3evou4BEPv4= +github.com/kyokomi/emoji v2.2.4+incompatible/go.mod h1:mZ6aGCD7yk8j6QY6KICwnZ2pxoszVseX1DNoGtU2tBA= diff --git a/module-2/task1/fibonacci/fibonacci.go b/module-2/task1/fibonacci/fibonacci.go new file mode 100644 index 0000000..531eaf5 --- /dev/null +++ b/module-2/task1/fibonacci/fibonacci.go @@ -0,0 +1,53 @@ +package fibonacci + +func Fibonacci(n interface{}) interface{} { + switch v := n.(type) { + case uint: + return fibonnaciUint(v) + case int: + return fibonnaciInt(v) + case float64: + return fibonnaciFloat(v) + } + return nil + +} + +func fibonnaciUint(n uint) uint { + if n == 0 { + return 0 + } + if n == 1 { + return 1 + } + if n == 2 { + return 1 + } + return fibonnaciUint(n-1) + fibonnaciUint(n-2) +} + +func fibonnaciInt(n int) int { + if n == 0 { + return 0 + } + if n == 1 { + return 1 + } + if n == 2 { + return 1 + } + return fibonnaciInt(n-1) + fibonnaciInt(n-2) +} + +func fibonnaciFloat(n float64) float64 { + if n == 0 { + return 0 + } + if n == 1 { + return 1 + } + if n == 2 { + return 1 + } + return fibonnaciFloat(n-1) + fibonnaciFloat(n-2) +} diff --git a/module-2/task1/go.mod b/module-2/task1/go.mod new file mode 100644 index 0000000..0782af5 --- /dev/null +++ b/module-2/task1/go.mod @@ -0,0 +1,3 @@ +module honestit/computator + +go 1.16 diff --git a/module-2/task1/main.go b/module-2/task1/main.go new file mode 100644 index 0000000..761ede3 --- /dev/null +++ b/module-2/task1/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "honestit/computator/fibonacci" + "strconv" +) + +func main() { + var input string + + fmt.Print("Provide a number for fibonnaci function: ") + fmt.Scanf("%s", &input) + + n, err := cast(input) + if err == nil { + fib := fibonacci.Fibonacci(n) + fmt.Printf("Fibonacci for %v = %v", n, fib) + } else { + fmt.Printf("Something wrong with your input: %q", input) + } + +} + +func cast(input string) (interface{}, error) { + if v, err := strconv.ParseInt(input, 0, 0); err == nil { + return int(v), nil + } else if v, err := strconv.ParseUint(input, 0, 0); err == nil { + return uint(v), nil + } else if v, err := strconv.ParseFloat(input, 64); err == nil { + return float64(v), nil + } else { + return nil, fmt.Errorf("not a number") + } +}