-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 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
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,63 @@ | ||
package day205 | ||
|
||
import ( | ||
"errors" | ||
"sort" | ||
) | ||
|
||
var errNoGreaterPermutation = errors.New("greater permutation impossible") | ||
|
||
// ErrNoGreaterPermutation is the error when the greater | ||
// permutation doesn't exist. | ||
func ErrNoGreaterPermutation() error { | ||
return errNoGreaterPermutation | ||
} | ||
|
||
// NextPermutationUint find the next greater permutation | ||
// of it's digits in absolute order. | ||
// Return an error if a greater permutation isn't possible. | ||
func NextPermutationUint(num uint) (uint, error) { | ||
digits := separateDigits(num) | ||
var i int | ||
for i = len(digits) - 1; i > 0; i-- { | ||
if digits[i] > digits[i-1] { | ||
break | ||
} | ||
} | ||
if i == 0 { | ||
sort.Ints(digits) | ||
return 0, ErrNoGreaterPermutation() | ||
} | ||
x := digits[i-1] | ||
min := i | ||
for j := i + 1; j < len(digits); j++ { | ||
if digits[j] > x && digits[j] < digits[min] { | ||
min = j | ||
} | ||
} | ||
digits[i-1], digits[min] = digits[min], digits[i-1] | ||
sort.Ints(digits[i:]) | ||
return convertToUint(digits), nil | ||
} | ||
|
||
func convertToUint(digits []int) uint { | ||
var result uint | ||
multiplier := 1 | ||
for i := range digits { | ||
result += uint(digits[len(digits)-1-i] * multiplier) | ||
multiplier *= 10 | ||
} | ||
return result | ||
} | ||
|
||
func separateDigits(num uint) []int { | ||
var result []int | ||
for num != 0 { | ||
result = append(result, int(num)%10) | ||
num /= 10 | ||
} | ||
for i := 0; i < len(result)/2; i++ { | ||
result[i], result[len(result)-1-i] = result[len(result)-1-i], result[i] | ||
} | ||
return result | ||
} |
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,32 @@ | ||
package day205 | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
var testcases = []struct { | ||
in uint | ||
out uint | ||
err error | ||
}{ | ||
{123, 132, nil}, | ||
{132, 213, nil}, | ||
{321, 0, ErrNoGreaterPermutation()}, | ||
} | ||
|
||
func TestNextPermutationUint(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result, err := NextPermutationUint(tc.in); result != tc.out || err != tc.err { | ||
t.Errorf("Expected (%v,%v) got (%v,%v)", tc.out, tc.err, result, err) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkNextPermutationUint(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
NextPermutationUint(tc.in) | ||
} | ||
} | ||
} |