Shuhei Kagawa

Algorithms: Design and Analysis, Part 1

Nov 29, 2015 - Algorithms

Today I finished the final exam of Stanford's "Algorithms: Design and Analysis, Part 1" at Coursera. I solved all its programming assignments with JavaScript and put the solutions at GitHub:

The course focused on fundamental algorithms and their analysis. What I learned so far were:

As a self-taught programmer, I had been away from formal Computer Science topics like big O notation. But after I learned Machine Learning and tried to implement some ML algorithms for large datasets a few months ago, I noticed that I needed some knowledge of basic algorithms and mathematical analysis of them.

The Algorithms course focused not only on algorithms themselves but also on their performance analysis and mathematical proofs. The analysis and proofs are crucial especially for the cases when we design custom algorithms for real-world problems in the future, and more importantly made me more confident about the algorithms.

Another good side effect of the course was that I got interested in Mathematics. Through the course and a study meetings on SICP with my co-workers, I noticed that Mathematics is interesting but I lacked some prerequisites to understand it. At the moment, I came across Introduction to Mathematical Thinking by Keith Devlin. It was written to fill the gap between the school math, which focuses following given procedures, and the college math, which focuses on thinking. It is perfect for someone like me, who skipped proper Mathematics training at college. I am still on the way reading it but it already started working on my analysis of the SICP exercises.

The part 2 of the course will start probably on March 2016. I cannot wait for it to start and get my eyes open again.

Node.js executable module

Oct 25, 2015 - JavaScript

You may want to create a Node module that is also an executable. The convention is to create two files, one for lib and the other for bin, but I think it's OK for simple modules.

To achieve it, you need to detect whether it's executed as an entry point. According to the official documentation of Node:

When a file is run directly from Node.js, require.main is set to its module. That means that you can determine whether a file has been run directly by testing

require.main === module

For example:

#!/usr/bin/env node

function sum(a, b) {
  return a + b;
}

module.exports = sum;

if (require.main === module) {
  var a = parseInt(process.argv[2], 10);
  var b = parseInt(process.argv[3], 10);
  console.log(sum(a, b));
}

Then you can use it as an executable and a module.

$ node sum.js 3 4
7
$ node
> require('./sum')(3, 4)
7

Color prompt by exit code

Oct 18, 2015 - Bash

The idea of customizing bash prompt hooked me a few months ago. I tried a few crazy things like showing random emojis and finally settled down with just showing the current directory, git branch and status.

At Tokyo Node Gakuen #18 on October 8th, @yosuke_furukawa talked about Node.js v4.0. Aside from the well-organized and informative talk, I found another interesting thing at his demo. His terminal prompt turned red when a command failed.

It looked pretty and useful. So I emulated it.

Changing prompt color

The code in .bash_profile is pretty straightforward. It changes the prompt color depending on whether the exit code is 0 or not. The only one trick is to capture the exit code at the very first line of the prompt command to prevent it from being changed in prior lines.

# Colors
light_green="\[\e[1;32m\]"
light_red="\[\e[1;31m\]"
yellow="\[\e[0;33m\]"
gray="\[\e[0;37m\]"
reset="\[\e[m\]"

# Customize prompt
prompt_command() {
  local status="$?"
  local status_color=""
  if [ $status != 0 ]; then
    status_color=$light_red
  else
    status_color=$light_green
  fi
  export PS1="[${yellow}\w${reset}]${gray}$(__git_ps1)${reset} ${status_color}λ${reset} "
}
export GIT_PS1_SHOWDIRTYSTATE=1
export PROMPT_COMMAND=prompt_commandsh

One of the advantages of attending real events over just browsing slides online is being able to take a peek of other people's dev environments. Looking forward to seeing more cool stuff on upcoming events like Tokyo Node Fest 2015.