Skip to content

Commit

Permalink
Fix RankN function when n greater than 10 (#1)
Browse files Browse the repository at this point in the history
Co-authored-by: danghh <dang.hoang@teemazing.work>
  • Loading branch information
danghh-1998 and danghh committed Jun 24, 2024
1 parent 7e12562 commit a07818e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lexorank.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lexorank

import (
"errors"
"fmt"
"strconv"
)

Expand Down Expand Up @@ -59,9 +60,12 @@ func RankN(prev, next string, n int) ([]string, error) {
return nil, err
}

suffixRankLen := len(strconv.Itoa(n))
suffixRankFormat := fmt.Sprintf("%%0%dd", suffixRankLen)

res := make([]string, 0, n)
for i := 0; i < n; i++ {
res = append(res, idx+strconv.Itoa(i))
res = append(res, idx+fmt.Sprintf(suffixRankFormat, i))
}
return res, nil
}
1 change: 1 addition & 0 deletions lexorank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func TestRankN(t *testing.T) {
{"az", "b", 3, []string{"azU0", "azU1", "azU2"}, false},
{"a", "d", 2, []string{"b0", "b1"}, false},
{"a", "c", 4, []string{"b0", "b1", "b2", "b3"}, false},
{"0", "z", 20, []string{"U00", "U01", "U02", "U03", "U04", "U05", "U06", "U07", "U08", "U09", "U10", "U11", "U12", "U13", "U14", "U15", "U16", "U17", "U18", "U19"}, false},
{"a", "_", 4, []string{"b0", "b1", "b2", "b3"}, true},
}

Expand Down

0 comments on commit a07818e

Please sign in to comment.