Ad
Code
Diff
  • fn mean(x: &[f64]) -> f64 {
        let x_len_float = x.len() as f64;
        return x.iter().sum::<f64>() / x_len_float;
    }
    
    • fn mean(x: &[u64]) -> u64 {
    • x.iter().sum::<u64>() / x.len() as u64
    • fn mean(x: &[f64]) -> f64 {
    • let x_len_float = x.len() as f64;
    • return x.iter().sum::<f64>() / x_len_float;
    • }

Skipped the string-building part and did a sum instead.

For an array of chars [48+5, 48+6, 48+7] you have an array of numeric values [5, 6, 7] which leads to 5 * 10^0 + 6 * 10^1 + 7 * 10^2 = 765 (which is the maximum and the equivalent of reversing the string)

Added a test for execution time performance (if that is a concern)
and a test for random values

Code
Diff
  • import java.util.Arrays;
    
    public class MaxNumber {
        public static long print(long number) {
            long sum = 0;
          
            String numStr = String.valueOf(number);
            char[] digits = numStr.toCharArray();
            Arrays.sort(digits);
          
            
            // here the digits are sorted in ascending order
            for (int i = 0; i < digits.length; i++) {
                sum += Math.round( Character.getNumericValue(digits[i]) * Math.pow(10, i) );
            }
            return sum;
        }
    }
    • import java.util.Arrays;
    • public class MaxNumber {
    • public static long print(long number) {
    • long sum = 0;
    • String numStr = String.valueOf(number);
    • char[] digits = numStr.toCharArray();
    • Arrays.sort(digits);
    • StringBuilder sortedStr = new StringBuilder(new String(digits));
    • return Long.parseLong(sortedStr.reverse().toString());
    • // here the digits are sorted in ascending order
    • for (int i = 0; i < digits.length; i++) {
    • sum += Math.round( Character.getNumericValue(digits[i]) * Math.pow(10, i) );
    • }
    • return sum;
    • }
    • }