Created
September 12, 2019 15:13
-
-
Save dimkouv/5d1932ee5047ecfe036d908c361f7504 to your computer and use it in GitHub Desktop.
number permutations
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" | |
func startCounting(numsLen, maxNum int) { | |
numTable := make([]int, numsLen) | |
for { | |
for i := 0; i < maxNum+1; i++ { | |
numTable[numsLen-1] = i | |
fmt.Println(numTable) | |
} | |
if updated := updateTable(numTable, maxNum); !updated { | |
break | |
} | |
} | |
} | |
func updateTable(numTable []int, maxNum int) bool { | |
lastZeroedNum := -1 | |
for i := len(numTable) - 1; i >= 0; i-- { | |
if numTable[i] == maxNum { | |
numTable[i] = 0 | |
lastZeroedNum = i | |
} else if i == lastZeroedNum-1 { | |
numTable[i]++ | |
return true | |
} | |
} | |
return false | |
} | |
func main() { | |
startCounting(10, 9) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment