Showing posts with label Lambda. Show all posts
Showing posts with label Lambda. Show all posts

Tuesday, July 4, 2017

CyclicRotation

import java.util.Arrays;
import java.util.stream.Collectors;

public class CyclicRotation {

     public static void main(String[] args) {
          print(solution(3, 1, 2, 3, 4, 5));
     }

     public static int[] solution(int k, int... a) {
          int l = a.length;
          int[] r = new int[l];
          for (int i = 0; i < l; i++) {
               r[(i + k) % l] = a[i];
          }
          return r;
     }

     public static void print(int... a) {
          System.out.println("[" + Arrays.stream(a).boxed().map(x -> x.toString()).collect(Collectors.joining(",")) + "]");
     }
}


Binary Gap

A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.

import java.util.Arrays;

public class BinaryGap {
     public static void main(String[] args) {
          BinaryGap bg = new BinaryGap();
          for (int i = 1; i < 10; i++) {
               System.out.println(bg.solution(i));
          }
     }

     public int solution(int n) {
          return Arrays.stream(Integer.toBinaryString(n).replaceAll("0+$", "").split("1")).mapToInt(String::length).max().orElse(0);
     }
}


Saturday, July 1, 2017

Prime Number

import java.util.stream.IntStream;

public class PrimeNumber {
    
     public static void main(String[] args) {
          PrimeNumber primeNumber = new PrimeNumber();
          System.out.println(primeNumber.isPrime(2));
          System.out.println(primeNumber.isPrime(9));
     }
    
     public boolean isPrime(int number) {
          if (number > 1) {
               return !IntStream.rangeClosed(2, (int) Math.sqrt(number)).anyMatch(x -> number % x == 0);
          }
          return false;
     }
}


Output:
true
false

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);
     }
}


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