math.digits: add a convenience reverse parameter too

pull/13729/head
Delyan Angelov 2022-03-13 22:15:05 +02:00
parent 96ee0efc12
commit 9f22fbafee
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 9 additions and 2 deletions

View File

@ -45,6 +45,7 @@ pub fn degrees(radians f64) f64 {
[params] [params]
pub struct DigitParams { pub struct DigitParams {
base int = 10 base int = 10
reverse bool
} }
// digits returns an array of the digits of n in the given base b. // digits returns an array of the digits of n in the given base b.
@ -74,6 +75,10 @@ pub fn digits(num i64, params DigitParams) []int {
n /= b n /= b
} }
if params.reverse {
res = res.reverse()
}
return res return res
} }

View File

@ -914,9 +914,11 @@ fn test_lcm() {
} }
fn test_digits() { fn test_digits() {
palindrom_digits_in_10th_base := digits(i64(1234432112344321)).reverse() palindrom_digits_in_10th_base := digits(i64(1234432112344321))
assert palindrom_digits_in_10th_base == [1, 2, 3, 4, 4, 3, 2, 1, 1, 2, 3, 4, 4, 3, 2, 1] assert palindrom_digits_in_10th_base == [1, 2, 3, 4, 4, 3, 2, 1, 1, 2, 3, 4, 4, 3, 2, 1]
assert digits(125, base: 10, reverse: true) == [1, 2, 5]
digits_in_10th_base := digits(125, base: 10).reverse() digits_in_10th_base := digits(125, base: 10).reverse()
assert digits_in_10th_base == [1, 2, 5] assert digits_in_10th_base == [1, 2, 5]