Friday, June 30, 2017

Minimum Perimeter of Rectangle


import java.util.stream.Stream;

public class MinimumRectanglePerimeter {
     public static void main(String[] args) {
          System.out.println(perimeter(30));
     }

     public static int perimeter(int area) {
          return Stream.iterate((int) Math.sqrt(area), n -> n - 1).filter(n -> area % n == 0).map(n -> 2 * (n + area / n)).findFirst().get();
     }
}


Output:
22

Thursday, June 29, 2017

Count Coins

There are four types of common coins in US currency:
  quarters (25 cents)
  dimes (10 cents)
  nickels (5 cents)
  pennies (1 cent)

There are 6 ways to make change for 15 cents:
  A dime and a nickel;
  A dime and 5 pennies;
  3 nickels;
  2 nickels and 5 pennies;
  A nickel and 10 pennies;
  15 pennies.

How many ways are there to make change for a dollar
using these common coins? (1 dollar = 100 cents).

[Source http://rosettacode.org]

import java.util.ArrayList;
import java.util.List;

public class CountCoins {
     private static final int QUARTER = 25;
     private static final int DIME = 10;
     private static final int NICKEL = 5;

     public static List solution(int cents) {
          List options = new ArrayList<>();
          for (int quarters = 0; quarters <= cents / QUARTER; quarters++) {
               int afterQuarters = cents - quarters * QUARTER;
               for (int dimes = 0; dimes <= afterQuarters / DIME; dimes++) {
                    int afterDimes = afterQuarters - dimes * DIME;
                    for (int nickels = 0; nickels <= afterDimes / NICKEL; nickels++) {
                         int pennies = afterDimes - nickels * NICKEL;
                         options.add(String.format("Quarters=%d, Dimes=%d, Nickels=%d, Pennies=%d", quarters, dimes, nickels, pennies));
                    }
               }
          }
          return options;
     }

     public static void main(String[] args) {
          List options = solution(100);
          options.forEach(System.out::println);
     }
}


Calculate Statistics

This task will process a sequence of integer numbers
to determine the following statistics:

    o) minimum value
    o) maximum value
    o) number of elements in the sequence
    o) average value

For example: [6, 9, 15, -2, 92, 11]

    o) minimum value = -2
    o) maximum value = 92
    o) number of elements in the sequence = 6
    o) average value = 21.833333



import java.util.Arrays;
import java.util.IntSummaryStatistics;

public class CalculateStatistics {
    
     public static void main(String[] args) {
          double[] stats = solution(6, 9, 15, -2, 92, 11);
          System.out.println(String.format("Minimum=%f, Maximum=%f, Count=%f, Average=%f", stats[0], stats[1], stats[2], stats[3]));
     }
    
     public static double[] solution(int...numbers){
          double[] stats = new double[4];
          IntSummaryStatistics summaryStatistics = Arrays.stream(numbers).summaryStatistics();
          stats[0] = summaryStatistics.getMin();
          stats[1] = summaryStatistics.getMax();
          stats[2] = summaryStatistics.getCount();
          stats[3] = summaryStatistics.getAverage();
          return stats;
     }
}

Combined Number using Java 8

Following is  a function, accepting a list of non negative integers, and returning their largest possible combined number as a string. For example

given [50, 2, 1, 9] it returns "95021"    (9 + 50 + 2 + 1)
given [5, 50, 56]   it returns "56550"    (56 + 5 + 50)
given 420, 42, 423] it returns "42423420" (42 + 423 + 420)

Source [https://blog.svpino.com/about]
  
import java.util.Arrays;

public class CombineNumber{
    public static void main(String... args){
        System.out.println(combine(50, 2, 1, 9));
        System.out.println(combine(5, 50, 56));
        System.out.println(combine(420, 42, 423));
    }
    public static String combine(int... numbers){
        return Arrays.stream(numbers).boxed().map(x->x.toString()).sorted((a,b)->(b+a).compareTo(a+b)).reduce(String::concat).get();
    }
}

Output:
95021
56550
42423420