Created
January 20, 2019 06:40
-
-
Save meysampg/357de859d601a9ebff2fa3b22fd97a78 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
"os" | |
"strconv" | |
) | |
func main() { | |
m, n := sizeOfArea() | |
// Well, we get width and height in human readable format, but we count from 0 :)) | |
fmt.Printf("Number of possible pathes from (0,0) to (%d,%d) is %d.\n", m, n, numberOfPath(m-1, n-1)) | |
} | |
// Can you make this code more efficient? You can find more information from any book about algorithm design or dynammic programming ;). | |
func numberOfPath(width, height int) int { | |
if width == 0 && height == 0 { | |
return 0 | |
} | |
if width == 0 || height == 0 { | |
return 1 | |
} | |
return numberOfPath(width-1, height) + numberOfPath(width, height-1) | |
} | |
func sizeOfArea() (int, int) { | |
if len(os.Args) > 1 { | |
m, errm := strconv.Atoi(os.Args[1]) | |
var n int | |
var errn error | |
if len(os.Args) > 2 { | |
n, errn = strconv.Atoi(os.Args[2]) | |
} else { | |
n = m | |
} | |
if errm == nil && errn == nil { | |
return m, n | |
} | |
} | |
var m, n int | |
fmt.Print("Please enter width of area >> ") | |
fmt.Scanf("%d", &m) | |
fmt.Print("Please enter height of area >> ") | |
fmt.Scanf("%d", &n) | |
return m, n | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment