Weight for weight

Weight for weight

مساله:

من و دوستم عضو یک گروه به اسم “Fat To Fit Club” شدیم. جان خسته شد برای اینکه هر ماه که لیست وزن عضو ها داده می شد آن نفر آخر لیست بود یعنی از همه چاق تر بود.

من کسی بودم که لیست را منتشر می کردم پس بهش گفتم: نگران نباش، من ترتیب لیست رو عوض می کنم. من تصمیم گرفتم که وزن رو به اعداد نسبت بدم. وزن عددی شد که از حاصل جمع اعداد آن عدد بدست می آید.

برای مثال عدد 99 میشود 18 و یا 100 می شود 1.

رشته ای را بگیرید که حاوی وزن های گروه باشد و به صورتی که گفته شد مرتب کنید.

Description:

My friend John and I are members of the “Fat to Fit Club (FFC)”. John is worried because each month a list with the weights of members is published and each month he is the last on the list which means he is the heaviest.

I am the one who establishes the list so I told him: “Don’t worry any more, I will modify the order of the list”. It was decided to attribute a “weight” to numbers. The weight of a number will be from now on the sum of its digits.

For example 99 will have “weight” 18100 will have “weight” 1 so in the list 100 will come before 99.

Given a string with the weights of FFC members in normal order can you give this string ordered by “weights” of these numbers?

Example:

"56 65 74 100 99 68 86 180 90" ordered by numbers weights becomes: 

"100 180 90 56 65 74 68 86 99"

When two numbers have the same “weight”, let us class them as if they were strings (alphabetical ordering) and not numbers:

180 is before 90 since, having the same “weight” (9), it comes before as a string.

All numbers in the list are positive numbers and the list can be empty.

Notes

it may happen that the input string have leading, trailing whitespaces and more than a unique whitespace between two consecutive numbers

For C: The result is freed.


import java.util.Arrays;
import java.util.Comparator;

import org.apache.commons.lang3.StringUtils;

public class WeightSort {
  
  public static String orderWeight(String strng) {
    String[] data = strng.split(" ");
    Arrays.sort(data, new Comparator<String>() {

          @Override
          public int compare(final String entry1, final String entry2) {
              final int field1 =  sumString(entry1);
              final int field2 = sumString(entry2);
              if (field1 > field2)
                return 1;
              if (field1 < field2)
                return -1;
              return entry1.compareTo(entry2);
          }
       });
    
    
    return StringUtils.join(data, " ");
  }
  
  public static int sumString(String numStr) {
    String[] arr = numStr.split("");
    int result = 0;
    for (String str : arr) {
      result += Integer.parseInt(str);
    }
    return result;
  }
}
import java.util.Comparator;
import java.util.Arrays;

public class WeightSort {
  public static String orderWeight(String string) {
    String[] split = string.split(" ");
    Arrays.sort(split, new Comparator<String>() {
      public int compare(String a, String b) {
        int aWeight = a.chars().map(c -> Character.getNumericValue(c)).sum();
        int bWeight = b.chars().map(c -> Character.getNumericValue(c)).sum();
        return aWeight - bWeight != 0 ? aWeight - bWeight : a.compareTo(b);
      }
    });
    return String.join(" ", split);
  }
}
import java.util.stream.IntStream;
import java.util.Comparator;
import java.util.stream.Collectors;
import java.util.Arrays;

public class WeightSort {
  
  public static String orderWeight(String strng) {
    return
      Arrays.stream(strng.split(" "))
        .sorted(Comparator
          .comparing(WeightSort::sumDigits)
          .thenComparing(String::compareTo))
        .collect(Collectors.joining(" "));
        
  }

  private static Integer sumDigits(String s) {
    return s.chars().map(c -> c - 48).sum();
  }
}
import java.util.Comparator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class WeightSort {
  
    public static String orderWeight(String weights) {
        return Stream.of(weights.trim().split(" +"))
                .sorted()
                .sorted(Comparator.comparingInt(o -> o.chars().map(Character::getNumericValue).sum()))
                .collect(Collectors.joining(" "));
    }
}
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;
import java.util.Comparator;
public class WeightSort {
  
  public static String orderWeight(String strng) {
        String[] strings = strng.split(" ");
        Map<String, Integer> map = new HashMap<>();
        for (String s : strings) {
            int i = 0;
            for (char c : s.toCharArray()) {
                i += c - '0';
            }
            map.put(s, i);
        }

        Arrays.sort(strings, (s1, s2) -> {
             int i = map.get(s1).compareTo(map.get(s2));
             if(i == 0){
                 i = s1.compareTo(s2);
             }
            return i;
        });

        return String.join(" ", strings);
    }
}
import java.util.Arrays;
import java.util.stream.Collectors;

public class WeightSort {
  
  public static String orderWeight(String strng) {
        return Arrays.stream(strng.split(" ")).sorted((String n1, String n2) -> {
            int w1 = Arrays.stream(n1.split("")).mapToInt(d -> Integer.parseInt(d)).sum();
            int w2 = Arrays.stream(n2.split("")).mapToInt(d -> Integer.parseInt(d)).sum();
            if (w1 < w2) {
                return -1;
            } else if (w1 == w2) {
                return n1.compareTo(n2);
            } else {
                return 1;
            }
        }).collect(Collectors.joining(" "));
  }
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class WeightSort {
  
    public static class OwnData implements Comparable<OwnData>{
      long key;
      long value;
      
    public OwnData(long key, long value) {
      super();
      this.key = key;
      this.value = value;
    }
    public long getKey() {
      return key;
    }
    public void setKey(long key) {
      this.key = key;
    }
    public long getValue() {
      return value;
    }
    public void setValue(long value) {
      this.value = value;
    }
    @Override
    public int compareTo(OwnData o) {
      if(this.key > o.key){
        return 1;
      } else if(this.key < o.key){
        return -1;
      } else {
        return (this.value + "").compareTo(o.value + "");
        /*
        if(this.value > o.value){
          return 1;
        } else if(this.value < o.value){
          return -1;
        } else {
          return 0;
        }
        */
      }
    }
    }
    public static String orderWeight(String strng) {
      if(strng == null || strng.equals("")){
        return "";
      } 
      
    List<Long> l = new ArrayList<Long>();
    List<Long> lQSum = new ArrayList<Long>();
    String[] strA = strng.split(" ");
    List<OwnData> data = new ArrayList<OwnData>();
    
    for(int i = 0; i < strA.length; i++){
      l.add(Long.parseLong(strA[i]));
      long num = Long.parseLong(strA[i]);
      long sum = 0;
        while (num > 0) {
            sum = sum + num % 10;
            num = num / 10;
         }
         lQSum.add(sum);
         data.add(new OwnData(sum, Long.parseLong(strA[i])));
    }
    Collections.sort(data);
    List<Long> result = new ArrayList<Long>();
    for(int i = 0; i < data.size(); i++){
      result.add(data.get(i).getValue());
    }
    String resultS = "";
    for( int i = 0; i < result.size(); i++){
      resultS += result.get(i) + " ";
    }
    return resultS.trim();
  }
}

دیدگاهتان را بنویسید