Tuesday, August 9, 2011

Sorting algorithms in Ruby

If you wish to learn sort algorithms, this is the best website to visit. Bad news for English speakers is that this site is written in Japanese.

The author of the website uses Java for his sample code. I could write Java code but I am not a big fan of it since Java forces me to type too much (Who wants to type "HashMap<nteger, Boolean> map = new HashMap<Integer, Boolean>()" when you can do the same task by typing "map = {}" in Ruby?). So I rewrote his sample programs in Ruby.

There are three interesting algorithms: heap sort, merge sort and quick sort. They are the most efficient ones among all the sorting algorithms.

Let's start with heap sort.
class HeapSort
def initialize
@heap = []
end

def push(item)
@heap << item
    i = @heap.size
    j = i / 2
    while i > 1
      if @heap[i - 1] < @heap[j - 1]
        t = @heap[i - 1]
         @heap[i - 1] = @heap[j - 1]
         @heap[j - 1] = t
       end
       i /= 2 
       j = i / 2
    end
  end 

  def pop
   res = @heap.shift
   return res if @heap.size == 0
   last_item = @heap.pop
   # bring the last item to the root
     @heap.unshift(last_item)
     i = 1
     j = 2
     while j <= @heap.size
       if j < @heap.size && @heap[j - 1] > @heap[j]
       j += 1
    end
    if @heap[i - 1] > @heap[j - 1]
      t = @heap[i - 1]
      @heap[i - 1] = @heap[j - 1]
      @heap[j - 1] = t
    end
    i = j
    j *= 2
  end
  res
end

def run(list)
    list.each do |item|
    push(item)
end

res = []
while item = pop
res << item
    end

    res 
  end
end

Heap sort is a bit tricky in terms of how to maintain a balanced binary tree. It looks complicated especially when you remove the root element from the tree (look at pop() method above). The second algorithm is merge sort.
class MergeSort
  def initialize
  end

  def merge(a1, a2, a)
    i = 0
    j = 0
    while i < a1.size || j < a2.size
      if j >= a2.length || (i < a1.length && a1[i] < a2[j])
        a[i+j] = a1[i]
        i += 1
      else
        a[i+j] = a2[j]
        j += 1
      end 
    end
  end

  def merge_sort(a) 
    if a.size > 1
      m = a.size / 2
      n = a.size - m
      a1 = []
      a2 = []
      0.upto(m - 1) do |i|
        a1[i] = a[i]
      end
      0.upto(n - 1) do |i|
        a2[i] = a[m + i]
      end 
      merge_sort(a1);
      merge_sort(a2);
      merge(a1, a2, a);
    end 
  end 

  def run(list)
    a = list.dup
    merge_sort(a)
    a
  end
end
The idea of merge sort is rather simple. You can implement it with recursion elegantly. And the last one is the queen of sorting algorithms, quick sort.
class QuickSort
  def initialize
  end
  
  def pivot(a, i, j)
    k = i + 1
    while k <= j && a[i] == a[k] 
      k += 1
    end
 
    if k > j  
      return -1 
    end
  
    if a[i] >= a[k] 
      return i
    end
 
    return k
 end

  def partition(a, i, j, x)
    l=i
    r=j
    while l <= r
      while l<=j && a[l] < x  
        l += 1
      end
      while r>=i && a[r] >= x 
        r -= 1
      end
      if l > r 
        break
      end
      t=a[l]
      a[l]=a[r]
      a[r]=t
      l += 1
      r -= 1
    end 
    return l
  end 

  def quick_sort(a, i, j)
    if i == j 
      return
    end
    p = pivot(a, i, j)
    if p != -1
      k = partition(a, i, j, a[p])
      quick_sort(a, i, k - 1)
      quick_sort(a, k, j)
    end 
  end 

  def run(list)
    a = list.dup
    quick_sort(a, 0, a.size - 1)
    a
  end
end

Although finding a pivot is a little tricky, quick sort is a cool algorithm. It is so elaborated that it almost looks like magic.

Usually Java or C/C++ is used to describe algorithms, but Ruby is also great because it can highlight the essence of algorithms with a fewer lines. I LOVE RUBY, MAY RUBY LIVE FOREVER!

Thursday, August 4, 2011

A philosophical difference in hiring processes for software engineers between US and Japan

A person currently working in US suggested me reading a book named "Cracking the Coding Interview". He told me that the book was one of the most famous books that let software engineers prepare for their recruiting interviews. I was so impressed and excited because the way US software companies hire engineers is so rational.

It says that interviewers can ask very intricate technical questions that relate to algorithm and coding. Sounds great! That's the way it should be.

In Japan, interviewers rarely test candidates with coding questions, odd as it might sound. Japan's IT industry was dominated by a few big software "general contractors" such as NTT Data and they usually don't look into candidates' technical skill set scrupulously. Instead, they rather attach greater importance to candidates' "communication skills", a vague concept that allegedly guarantees a candidate to be a good "team player".

However, I think that this is an absolute nonsense. A software engineer is supposed to be hired to code, not to chat. If a candidate is a very likable nice guy, but is unable to code even a line, he is useless in the workplace of software production. No wonder that Japanese IT industry has lost its competitive edge.

I feel very encouraged. I have finally found a reason to study computer science in earnest. If nobody really cares about proper knowledge on algorithm, who is willing to make serious efforts to learn it, after all?

Saturday, July 23, 2011

What do I want to achieve through information technology?

A tweet told me that Twitter was seeking a Japanese software engineer at their San Franscisco Headquarters.

Software Engineer - Japanese Product Focus, Applications

It looked interesting to me, so I applied for it. Let's see what will happen.

It is actually a little premature for me to apply for any job. I am not well-prepared yet.

I need a story... I want to accomplish something in this world. However, I still haven't found it yet. I am most interested in the web among many different fields inside information technology. I am especially interested in social networks or education. I am interested in making a difference in the society. I used to write a tiny electronic bulletin board system for communication with my old friends. I was also interested in groupware software like Lotus Notes for a better communication in workplace.

Maybe I have some ideas to make the world a better place. But I don't know how to make them happen yet.

Friday, July 22, 2011

Inventing my own English

As a non-native speaker of English, I have been feeling awkward about it. My pronunciation carries a heavy Japanese accent and my listening skills are far from perfect. I am a very slow reader of English. However, when it comes to writing English, there is a silver lining. Unless you are participating a real-time chatting, you are not required to respond to something quickly when you write it. You can stop to think for a while. You can modify your sentences after drafting swiftly.

I am aware that my grammar is not perfect. I do know that I keep making small mistakes regarding choices of particles and singular/plural. Writing nouns is the most difficult part of English. The distinction between countable and uncountable nouns is just terrifying.

Choice of words is not the only problem. Even problematic is choice of phrases or syntax. There are some very English-like syntaxes. (Sorry, I can't come up with a good example) Not only words but also syntaxes are very different between Japanese (my mother tongue) and English. It is very difficult for me to compose natural sentences characteristic of English.

After a long reflection, however, I came to a conclusion that it does not really matter. The most important thing is to have people understood my ideas. As long as there exists a context, my subtle grammatical errors or unnatural syntaxes in English do not prevent people from understanding what I want to mean. What a great relief am I given if I think this way!

In a way, this is the invention of a new language, my own English. It is not exactly the same as what typical American people speak, but still understandable to them. I can express my ideas with that tool. Let's not worry too much about superficial things such as grammar and wordings. Let's focus on the thoughts that I want to convey.

My English is already good enough to express anything in the world. Let's stop worrying and start telling something more interesting. Probably, my writing skills also grow gradually. But it doesn't really matter. The most important thing is what you say, not how you say.

Thursday, July 21, 2011

You don't need a formal education to be a good programmer

Y Combinator is a startup funding founded by Paul Graham. He is a programming guru as well as a popular author. One of my friends suggested me look at "Hacker News" of their website, where job openings of many startups are listed.

Hacker News at ycombinator.com

As I looked into it, I found some interesting features regarding the long list.

Very few of these job openings require candidates to have a formal education in information technology field. They emphasize how active their companies are and how exciting their jobs are. They list programming languages and technologies they use to perform projects. However, they don't say "you need to have a master degree or above in information technology or related disciplines" or something like that.

I don't have any formal education in the field of information technology. I started programming at thirteen. I studied economics at university. I learned everything about being a professional software engineer on the job. It didn't take long before I became a decent programmer at work.

As I observed people working at workplace, paradoxically, many of the most excellent programmers have any formal education in neither IT nor computer science. And some of the most useless programmers had degrees in computer science. I thought this mystery over and came to a conclusion. For the most talented people, probably, programming is too boring to learn in the classroom. All they need to be a good programmer is a cheap ordinary personal computer. You can learn almost everything about programming from computers themselves. If you still don't understand, then, you can also rely on the Internet (when I first started programming, I didn't have access to the Internet. I had to read technical books then. Nowadays, you don't need to read books any longer).

Therefore, it is quite a correct attitude that you don't ask candidates for a formal IT education when you try to hire good people. I think that people who put ads at Y Combinator know how software should be created. I like it. Their way of thinking resembles mine.

I am a kind of crazy person who can't adapt a normal society. My way of thinking is too little of conformist to fit an ordinary conservation organization. Bay Area seems to attract this kind of eccentric but smart people. If so, I have no choice but to go there, no matter how difficult it is.

Wednesday, July 20, 2011

Move beyond the comfort zone

Recently I stopped blogging in Japanese. Why? Maybe, my Japanese blog has too many readers for me to express something very personal :-). It might sound absurd, but when your blog get too much attention from the public, it will turn into a kind of "media" rather than mere personal records.

I am sick and tired with the Japanese blogsphere. Many of them are too much of inward-looking...whenever I read something in Japanese, I feel it too domestic and Japan-specific. It lacks universality which I adore.

I started programming at the age of 13. I have been a professional software engineer in the past 15 years. I am a native computer user. I am very familiar with the way digital devices work. Actually, it is so natural to me that I often forget the fact that a bunch of people still have difficulty in using computers.

Unfortunately, I could not find any computer job that is very interesting for me in Japan. The IT industry of Japan is not tech oriented. It is rather sales oriented. That is, IT companies don't pay attention to technology itself. As long as they can get enough amount of work from big corporations, they are happy with it and don't make a serious effort to improve their technical skills. This seemingly mysterious behavior has to do with the hierarchical industrial structure of Japanese IT industry. It runs deep.

When I was younger, I was more tech oriented. That's why I could not find any interesting IT company in Japan. More accurately, I was more innovation oriented. There are a plenty of tech guys who are exclusively interested in IT itself. However, most of them are not interested in innovation, which is a process where technology transforms the society. Technical people are not interested in social issues in Japan.

I am interested in both technology and the society. More precisely, I am only interested in the interface between them. I am very curious about the dynamic process where technology creates a new society. I am not so interested in subtle technicalities of information technology.

One small problem that bothers me is that I am so accustomed to be self-employed or bohemian that I am stuck in a rut. If I wish to engage in some more interesting business activities, I have to give up some part of freedom I am enjoying now. I have to sacrifice some part of my current lifestyle for a more valuable cause. Something inside me resists the change.

To judge it objectively, my current life is comfortable but somewhat boring. It is easy but doesn't help me grow. I can't keep on doing this forever. It is just not right. I must move beyond my "comfort zone" to enter another growing phase. Today we are all obliged to keep learning and growing until we die. Otherwise, we can't rationalize our long life for young people. We are not supposed to be just burden to them when we become old.

Thursday, July 14, 2011

What prevents the middle-aged from new challenges

I've been thinking of moving to and working in Silicon Valley, also known as the Bay Area in US. It's a famous mecca for IT engineers. It's been my long time dream. I used to be a very eager-to-learn and frustrating young programmer who was never satisfied with Japanese IT industry. I really wanted to go to the Bay Area. But I didn't. I can name a dozen of reasons why I didn't go there. This might be just a "sour grapes" attitude, that is, a kind of rationalization, though.

I moved to Canada in 1999. That would have been the best timing to move to the Bay Area, but I didn't. It's rare for me to regret something. Exceptionally, however, I feel a little regrettable about it. The first decade of the 21st century saw so many the Bay Area-based IT companies flourishing including Google, Apple and facebook. I missed the best decade of the Internet expansion from the Bay Area to the rest of the world.

The Bay Area has been a very exciting place for ambitious engineers and entrepreneurs. And it will be. I met a Japanese guy who had a long working experience in US. According to him, there's neither "best" nor "worst" time to move to the Bay Area. Even the macro economy is in slump, some new startups are growing rapidly there. Similarly, even when the macro economy is booming, some companies are going bankrupt there. "The real best time for you to go to the Bay Area is when you WANT to go there," he added.

I have no wife. I have no children, either. I have nothing which binds me to my home country, unlike other typical middle-aged people. I can do whatever I want to do.

Nevertheless, I still feel hesitating to go to the Bay Area. Why?

The real enemy resides in my own body. It's the memory of the past 12 years. I didn't go to the Bay Area 12 years ago in 1999. I have wandered all across the world since then. It was a great fun. I do appreciate these unique experiences. However, the idea of going to the Bay Area now makes me feel that I made a huge error in the past 12 years. Why did I not go there in 1999? Did I make a big mistake? "Are you stupid?" The inside of myself keeps blaming on me.

It's considered normal for the middle-aged people to get conservative and stop challenging something new. They usually say that it's because their body is getting old and does not function as before. Or they may say that they have a family to support and can't jeopardize the life stability. However, the real reason why they go with the status quo might be somewhat different. Probably, the real reason resides in themselves. They have too much memory to deal with. They feel compelled to rationalize what they have been doing. They might say "When I was younger, I had a dream and wanted to do that. The dream has gone, but it's OK. I don't want it any longer, and even if I did, it would be too late. What else could I do?"

Sometimes, those middle-aged people are right. Actually, their dream has gone forever. They can't do anything about it. Even if they still can do something about it, however, they also tend to give everything up. That's because they don't want to admit that they got wrong somewhere in the past.

It is always painful for you to admit your mistakes. The more serious the error is, the more painful you feel. But middle age is not old age yet. It is also true that it's too early to give up everything and to retire with the status quo. It's really difficult for me to admit my own mistakes. But I will. And I will challenge again.