React Native

A return to gigabyte-guzzling development

Now that RecipeTrader is mostly fleshed out and working, I set my sights once again on React Native. I had to re-download a lot of system images, Android Studio, Expo emulators and such, which I had freed up for space. The downloading alone makes this dev-setup a very protracted process.

/Screenshot_2019-07-10_16-16-35-reactnative.png

It’s also extremely rough on space. Here I am, once again precariously close to maxing out the meager 48GiB EXT4 volume which has been my home for years now.

/Screenshot_2019-07-11_10-06-25-gparted.png

Additionally, while emulating phone hardware with Android Studio, I only can expect about 90 minutes of battery life, significantly less than my typical 180 minutes.

[Read More]

Recipe Trader

Culinary Social Network

https://recipetrader-demo.herokuapp.com/

I am very excited to announce RecipeTrader - my newest React.js demo website. RecipeTrader is a social network for sharing culinary recipes. It features user registration, login, and profile creation - all securely authenticated by JSON Web Token. Users can post recipes, and even like and comment on other users’ submissions.

This full-stack project employs a MongoDB backend, Express.js API, and a React.js frontend with Redux.js state management, and routes through React-router. It is deployed via a Heroku CI/CD pipeline, streamlined with Webpack, and accessible at recipetrader-demo.herokuapp.com

[Read More]

Elegant Ruby

Ruby lends itself to some syntactically elegant solutions, like in this program that swaps diagonally opposed elements in a matrix.

def swapDiagonals(matrix)
  matrix.each_with_index.map do | row, row_index |
    row.each_with_index.map do | element, col_index |
      if col_index == row_index
        row[row.length - row_index - 1]
      elsif  col_index == row.length - row_index - 1
        row[row_index]
      else
        element
      end
    end
  end
end

input:
 [[1,2,3], 
 [4,5,6], 
 [7,8,9]]

output:
[[3,2,1], 
 [4,5,6], 
 [9,8,7]]
[Read More]

Functional and Recursive Ruby

This program returns Squares. From a functional perspective, the use of a variable for the ‘range_object’ is technically a side-effect, but I include it here for sake of clarity.

Range objects are a good example of the kind syntactic sugar that I sometimes felt was lacking in JavaScript.

class Squares
  def initialize(n)
    @number = n
  end

  def square_of_sum
    def factorial(n)
      n == 1 ? 1 : n + factorial(n - 1)
    end

    factorial(@number) ** 2
  end

  def sum_of_squares
    range_object = 1..@number
    range_object.reduce { | a, b | a + b ** 2 }
  end

  def difference
    square_of_sum - sum_of_squares
  end
end

This Week in Dev

JavaScript Word-Search Puzzle

Revisiting JavaScript this week - I think my solution to the wordsearch puzzle grid problem is particularly elegant. Check out my Solution here

I also tweeted about an elegant recursive algorithm I devised to generate integer sequences in JavaScript (which is utilized in the word search challenge) Some languages (Ruby, Haskell) use syntax like [1..10] to represent an integer sequence, but JavaScript doesn’t have a built-in way to do this, so I created my own.

function intSequence(start, end, n = start, arr = []) {
    return n === end ? arr.concat(n)
        : intSequence(start, end, start < end ? n + 1 : n - 1, arr.concat(n));
}
[Read More]

As We May Think

Ideas of Vannevar Bush in practice

Referencing your entire knowledge-base

Vannevar Bush in his seminal essay As We May Think

One of my very favorite strategies is prototyping all of my solutions with unit tests, and having all of my solutions locally, because with a simple text search I can easily reference code I have used in the past.

Refer to past work for convenience

Languages like Java can be frustrating Even the most quotidian operations are often difficult to remember. For example, I KNOW I have converted an int array to an integer list in the past, dozens or hundreds of times. Especially with Java, I never expect anything to work exactly as anticipated, so I reference past work, and put it to the test (with unit testing)

How the heck did I do that?

When your past work is indexed, you can instantly scour reams of code to derive solutions from existing content. That’s working smart.

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]

Practical Uses for Bitwise Operators

I was very bemused at my first encounter of bitwise operations, but quickly realized that I couldn’t think of even a single use for this language feature.

My solution to the Say challenge on exercism.io represents the first occasion that I have been able to make use of this knowledge.

The challenge was to convert numbers into English words. To do this, I would need to group them into groups of three, as we normally do using comma notation, so that a number like 6,145,010 would group such as “6 million” “145 thousand” “ten”.

There are a couple different ways to do this, but I decided to pad the number so that the number of digits would always be a multiple of 3. Without a pad, I would end up getting groups like [“614”, “501”, “0”], instead of [“6”, “145”, “010”].

One possibility would be to use the remainder from a modulus division to determine the amount of pad needed. However, it is made much more concise by using the bitwise XOR operator. Here is the resulting code, and my unit tests.

const padString = function (number) {
  const pad = "0".repeat(((String(number).length-1)%3^3)-1);
  return (pad + String(number));
};

const magnitudeGroupedArray = function (number) {
  const paddedString = padString(number);
  return paddedString.match(/.{1,3}/g);
}

describe('pad', () => {
  test('pad-3', () => {
    expect(magnitudeGroupedArray(9)).toEqual(["009"]);
    expect(magnitudeGroupedArray(89)).toEqual(["089"]);
    expect(magnitudeGroupedArray(789)).toEqual(["789"]);
  });
  test('pad-6', () => {
    expect(magnitudeGroupedArray(6789)).toEqual(["006", "789"]);
    expect(magnitudeGroupedArray(56789)).toEqual(["056", "789"]);
    expect(magnitudeGroupedArray(456789)).toEqual(["456", "789"]);
  });
  test('pad-9', () => {
    expect(magnitudeGroupedArray(3456789)).toEqual(["003", "456", "789"]);
    expect(magnitudeGroupedArray(23456789)).toEqual(["023", "456", "789"]);
    expect(magnitudeGroupedArray(123456789)).toEqual(["123", "456", "789"]);
  });
});
[Read More]

This Week in Dev

Java, HackerRank and PowerMock

Venture Prize Pitch Night at Brown was really fun. The $25,000 first place prize was awarded to Formally - a tech company that aims to streamline visa and asylum applications, which might save law firms thousands of man hours per year!

Here I am with some of my classmates, posing for a photo with Virgin Pulse CMO Rajiv Kumar, who is an exceptional entrepreneur that founded “Shape Up RI” and managed to grow it into something extraordinary, and get acquired by Richard Branson’s Virgin Group.

/2019.03.06-BrownVenturePrize.jpg
It might be surprising to some that there is such a wealth of entrepreneural talent right here in little Rhode Island!

[Read More]