Tuesday, 16 April 2019

split number into it's digits

package sam.numbers;

import java.util.Arrays;

public class SplitNumber {

    public static int[] split(int num) {
        if(num <  10 && num > -10)
            return new int[]{num};
        
        boolean negative = num < 0;
        num = Math.abs(num);
        
        int[] split;
        
        if(num < 100)
            split = new int[2];
        else if(num < 1000)
            split = new int[3];
        else if(num < 10000)
            split = new int[4];
        else 
            split = new int[10];
        
        int k = 0;
        
        while(num > 0) {
            if(k >= split.length)
                Arrays.copyOf(split, split.length + split.length/2 );
            
            split[k++] = num % 10;
            num /= 10;
        }
        
        if(k != split.length)
            split = Arrays.copyOf(split, k);
        
        for (int i = 0, j = split.length - 1; i < j; i++, j--) {
            int t = split[i];
            split[i] = split[j];
            split[j] = t;
        }

        if(negative)
            split[0] = -1 * split[0];
        
        return split;
    }
}


    @Test
    void test() {
        check(1, 1);
        check(-1, -1);
        
        check(-11, -1, 1);
        check(11, 1, 1);
        
        check(-123, -1, 2, 3);
        check(123, 1, 2, 3);
        
        check(-1234, -1, 2, 3, 4);
        check(1234, 1, 2, 3, 4);
        
        check(-1234567, -1, 2, 3, 4,5,6,7);
        check(1234567, 1, 2, 3, 4,5,6,7);
    }

    private void check(int i, int... expected) {
        int[] actual = SplitNumber.split(i);
        
        System.out.printf("%-10s%s\n", i, Arrays.toString(actual));
        assertArrayEquals(expected, actual);
    }


1         [1]
-1        [-1]
-11       [-1, 1]
11        [1, 1]
-123      [-1, 2, 3]
123       [1, 2, 3]
-1234     [-1, 2, 3, 4]
1234      [1, 2, 3, 4]
-1234567  [-1, 2, 3, 4, 5, 6, 7]
1234567   [1, 2, 3, 4, 5, 6, 7]

Split string without using java provided split method

package sam.string;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class SplitString {
 public static List<String> split(String s, String delimeter) {
  Objects.requireNonNull(s);
  Objects.requireNonNull(delimeter);
  
  if(s.isEmpty() || delimeter.isEmpty())
   return Collections.singletonList(s);
  
  List<String> list = new ArrayList<>();
  int start = 0, end = 0;
  
  while(true) {
   end = s.indexOf(delimeter, start);
   
   if(end < 0) {
    list.add(s.substring(start));
    break;
   } else {
    list.add(s.substring(start, end));
    start = end + delimeter.length();
   }
  }
  
  return list;
 }

}

test

    @Test
    void test() {
        check("1 2 3", " ");
        check("1122123", "12");
        check("1abc2llc3kollma0allmi", "ll");
        check("ll1abc2llc3kollma0allmi", "ll");
    }

    private void check(String s, String deli) {
        List<String> actual = SplitString.split(s, deli);
        List<String> expected = Pattern.compile(deli, Pattern.LITERAL).splitAsStream(s).collect(Collectors.toList());

        System.out.printf("split(\"%s\", \"%s\") = %s\n", s, deli, actual);
        
        assertEquals(expected, actual);
    }


split("1 2 3", " ") = [1, 2, 3]
split("1122123", "12") = [1, 2, 3]
split("1abc2llc3kollma0allmi", "ll") = [1abc2, c3ko, ma0a, mi]
split("ll1abc2llc3kollma0allmi", "ll") = [, 1abc2, c3ko, ma0a, mi]


code highlighted using: hilite.me

A Product Array Puzzle

Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.

For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].

source: Daily Coding Problem: Problem #2


package sam.numbers;

import java.util.Arrays;

public class ProductArrayPuzzle {
    public static int[] compute(int... array) {
        if(array.length < 2)
            return array;
        
        int multiply = 1;
        for (int i : array) 
            multiply *= i;
        
        for (int i = 0; i < array.length; i++) 
            array[i] = multiply/array[i];
        
        return array;
    }
    
    
    public static int[] computeWithoutDivision(int... array) {
        if(array.length < 2)
            return array;
        
        int[] copy = Arrays.copyOf(array, array.length);
        
        for (int i = 0; i < array.length; i++) {
            int m = 1;
            for (int j = 0; j < copy.length; j++) 
                m *= j == i ? 1 : copy[j];
            
            array[i] = m;
        } 
        
        return array;
    }

}


package sam.numbers;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

import org.junit.jupiter.api.Test;

public class MainTest {
    @Test
    public void ProductArrayPuzzleTest() {
        assertArrayEquals(ProductArrayPuzzle.compute(1, 2, 3, 4, 5), new int[]{120, 60, 40, 30, 24});
        assertArrayEquals(ProductArrayPuzzle.compute(3, 2, 1), new int[]{2, 3, 6});
        
        assertArrayEquals(ProductArrayPuzzle.computeWithoutDivision(1, 2, 3, 4, 5), new int[]{120, 60, 40, 30, 24});
        assertArrayEquals(ProductArrayPuzzle.computeWithoutDivision(3, 2, 1), new int[]{2, 3, 6});
    }
}


code highlighted using: hilite.me