Your task is to make a function that can take any non-negative integer as an argument and return it with its digits in descending order. Essentially, rearrange the digits to create the highest possible number.
Input: 42145
Output: 54421
Input: 145263
Output: 654321
Input: 123456789
Output: 987654321
## Descending-Order.jl
using Pipe, Test
"""
使n的各位数字降序排列
"""
#> "使n的各位数字降序排列\n"
function descending_order(n::Int)::Int
@pipe n |> string |> collect |> sort |> reverse |> join |> parse(Int64, _)
end
#> descending_order (generic function with 1 method)
@test descending_order(42145) == 54421
#> Test Passed
#> Expression: descending_order(42145) == 54421
#> Evaluated: 54421 == 54421
library(tidyverse)
#' 整数各位数字降序排列
#' @param n 整数
#' @return 整数
<- function(n) {
descending_order %>%
n as.character() %>%
str_split("") %>% # 注意,该函数返回列表
unlist() %>%
sort(decreasing = TRUE) %>% # 可直接对字符排序
str_c(collapse = "") %>%
as.integer()
}
descending_order(42145) # 54421
#> [1] 54421
/**
*
* @param {Number} n
* @returns {Number}
*/
function descendingOrder(n) {
return parseInt(String(n).split('').sort().reverse().join(''));
}
// 同理有
function squareDigits(n) {
return parseInt(String(n).split('').map(x => x ** 2).join(''));
}console.log(squareDigits(9812) === 816414);
.exports = descendingOrder; module
#> true