Skip to content

Instantly share code, notes, and snippets.

@meysampg
Created January 20, 2019 06:40
Show Gist options
  • Save meysampg/357de859d601a9ebff2fa3b22fd97a78 to your computer and use it in GitHub Desktop.
Save meysampg/357de859d601a9ebff2fa3b22fd97a78 to your computer and use it in GitHub Desktop.
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