This Week in Dev

Java, Unit Tests, CodeSignal

Up to level 40 this week on CodeSignal.com - that’s over 100 challenges solved in just a couple weeks

Evaluating Efficiency with Big-O Notation

Check out my recursive solution to this arcade problem, which made it possible for me solve a problem in *constant time* O(1), a whole world of improvement over my previous approach, which turned out to be a *factorial time* O(n!) solution. Big-O notation is a means of evaluating the computational efficiency of different algorithms.

My solution

boolean stringsRearrangement(String[] inputArray) {
    String[] sorted = Arrays.stream(inputArray).sorted().toArray(String[]::new);

    // test input that does not include duplicates
    if (inputArray.length == Arrays.stream(inputArray).distinct().count()
            && allStringsConform(sorted)) return true;

    // test starting with each individual input string
    for (String string : Arrays.stream(sorted).distinct().toArray(String[]::new)) {
        ArrayDeque<String> input = new ArrayDeque<>(Arrays.stream(sorted).collect(Collectors.toList()));
        input.remove(string);
        ArrayDeque<String> output = new ArrayDeque<>(Collections.singletonList(string));
        if (testPermutations(input, output)) return true;
    }

    return false;
}
[Read More]

This Week in Dev

Java, Streams and Loops - Affine Cipher

import java.math.BigInteger;

class AffineCipher {

    private static final int M = 26;        // size of alphabet
    private static final int A = (int) 'a'; // zero-index of 'a' (ASCII code point 97)

    String encode(String input, int a, int b) {
        if (greatestCommonDivisor(a, M) != 1) {
            throw new IllegalArgumentException("Error: keyA and alphabet size must be coprime.");
        }

        StringBuilder output = new StringBuilder();

        for (int codePoint :
            input.replaceAll("\\W", "") // nix punctuation and whitespace
                    .toLowerCase()
                    .toCharArray()
        ) {
            output.append( Character.isDigit(codePoint)
                    ? (char) codePoint
                    : (char) (((a * (codePoint - A) + b) % M) + A));
        }

        return String.join(" ", splitString(output.toString(), 5));
    }
[Read More]

This Week in Dev

Java, HackerRank and PowerMock

I completed the 30 days challenges on HackerRank! HackerRank seems to employ an intentionally convoluted Input scheme, given that they use STATIC classes and instantiate Scanner class as FINAL, but i did manage to create JUnit tests for all the exercises that function equivalently to the online tests. I would be happy to share these (the tests) with others who appreciate the expediency and more robust debugging capabilities of haxoring in their local environment.

I used a tool called PowerMock to get around JUnit’s incapacity for testing static classes, so these unit tests are able to transparently run unmodified code from HR. Ask me for the repo (it just includes my tests and the intermediate code templates they provide for each exercise).

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import org.powermock.core.classloader.annotations.PrepareForTest;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.junit.Assert.assertEquals;

@RunWith(MockitoJUnitRunner.class)
@PrepareForTest({Solution.class})
public class SolutionTest {

    // a stream to record the output of our program
    private ByteArrayOutputStream testOutput;

    // run before each test (prepare for input / output)
    @Before
    public void setUpOutputStream() {
        testOutput = new ByteArrayOutputStream();
        System.setOut(new PrintStream(testOutput));
    }
[Read More]