This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(go/fib): ensure example consistency
Signed-off-by: Roman Volosatovs <[email protected]>
- Loading branch information
1 parent
2cdcb12
commit 390c4ca
Showing
3 changed files
with
50 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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,15 @@ | ||
# Arguments | ||
args = [ | ||
"7", | ||
"21" | ||
] | ||
|
||
# Pre-opened file descriptors | ||
[[files]] | ||
kind = "stdin" | ||
|
||
[[files]] | ||
kind = "stdout" | ||
|
||
[[files]] | ||
kind = "stderr" |
This file contains 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 |
---|---|---|
@@ -1,37 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"flag" | ||
"fmt" | ||
"strconv" | ||
"log" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
func FibonacciSequence(n uint64) uint64 { | ||
if n <= 1 { | ||
return n | ||
} | ||
return FibonacciSequence(n-1) + FibonacciSequence(n-2) | ||
func init() { | ||
log.SetFlags(0) | ||
} | ||
|
||
func fib(n uint64) uint64 { | ||
if n <= 1 { | ||
return n | ||
} | ||
return fib(n-1) + fib(n-2) | ||
} | ||
|
||
func main(){ | ||
if len(os.Args) > 1 { | ||
for _, arg := range os.Args[1:] { | ||
n, err := strconv.ParseUint(arg, 10, 64) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "%s\n", err) | ||
continue | ||
} | ||
fmt.Printf("Fibonacci sequence number at index %d is %d\n" , n , FibonacciSequence(n)); | ||
} | ||
} else { | ||
var input string | ||
fmt.Print("Which Fibonacci index to find? ") | ||
fmt.Scanln(&input) | ||
n, err := strconv.ParseUint(input, 10, 64) | ||
func main() { | ||
flag.Parse() | ||
|
||
args := flag.Args() | ||
if len(args) == 0 { | ||
fmt.Println("Enter a non-negative number:") | ||
sc := bufio.NewScanner(os.Stdin) | ||
sc.Scan() | ||
b, err := sc.Bytes(), sc.Err() | ||
if err != nil { | ||
log.Fatal("Failed to read stdin: %s", err) | ||
} | ||
args = []string{string(b)} | ||
} | ||
|
||
for _, arg := range args { | ||
n, err := strconv.ParseUint(arg, 10, 64) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "%s\n", err) | ||
os.Exit(-1) | ||
log.Fatalf("Failed to parse number: %s", err) | ||
} | ||
fmt.Printf("Fibonacci sequence number at index %d is %d\n" , n , FibonacciSequence(n)); | ||
} | ||
fmt.Printf("Fibonacci sequence number at index %d is %d\n", n, fib(n)) | ||
} | ||
} |
This file contains 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