Saturday, October 13, 2012

Observer Pattern In Ruby

Let add one more pattern into the design pattern series by consider Observer pattern. In observer pattern, there is a subject which will be observed by multiple observers. When an event happen with the subject, all observers who registered with the subject will get notify. One obvious example of this pattern is you and my blog. If you are subscribed to my blog, you will get notify when my blogs get updated. This relation between subject and observers provide a clear pattern. It's clear enough to be implemented as a module in ruby standard library, called Observable.

In this post, I am going to use this observable module to implement a program that allow me follow stocks prices, and also calculate the average over time.

Assume that the stock market allow me to read current stock price with a function call "get_current_price(stock_symbol)".  With this method, I can implement a simple class, SimpleStockWatcher, to read the current price and calculate the average as follow:
class SimpleStockWatcher 
  def initialize(symbol)
    @symbol = symbol
    @sum = 0
    @count = 0
  end

  def update
    price = get_current_price(@symbol)
    @sum += price
    @count += 1
    show(price)
  end

  def show(price)
    puts "Symbol #{@symbol}"
    puts "  Time:          #{Time.now}"
    puts "  Current Price: #{price}"
    puts "  Average:       #{@sum/@count}"
  end
end
If I want to monitor Bank of America (BOA), and Google (GOOG), I implement the following loop:
stocks = [ SimpleStockWatcher("BOA"), SimpleStockWatcher("GOOG") ]
loop do
  stocks.each(&:update)
  sleep(1)
end
This loop will print the stocks price and average very second.

Now, I want to add more analysis, say 30 or 100 days moving average. No problem, I add more code into the SimpleStockWatcher. Then every second we get the stock prices, average, 30-day moving average, and 100-days moving average.

Let get a bit more complicate, I want to show only current price and 30-days moving average for stock "BOA". For "GOOG", I want to show current price, the average, and 100-days moving average. How do I satisfy this requirement?

Thursday, October 11, 2012

Transformer Prime and Linux Installer

Android is a great platform for consuming. I means, using it for internet, email, read books, keep your notes, etc. One may go as future as write a book, create art work, edit photos, compose musics, etc. There is an app for every thing you want, even for a linux enthusiastic like me. What I mean is I can use linux side by side with android.

First of all, you can find instruction to install Ubuntu on prime here on the xda-developers site, if you are interested. It's risky, but you will get full ubuntu running on the prime. I won't go into this approach thou. It's not what I aim for.

For me, I prefer another method, chroot to a linux image file. This method allow me to run linux along side with android. I can do things that android does great, like reading email, facebook, websurfing, view pictures, read books, etc. I also can do things that normally android won't allow you. Actually, I can do pretty much every thing that linux provide you.

The idea is to create a linux image file on your android device, mount the image file to android system, and chroot to it.

Guess what, there is an app for that too. It's call "linux installer". Unfortunately, it's some what doesn't work perfectly on transformer prime.

The app work only with rooted device, which mean you need to unlock you prime. After installed linux installer 4.1 (the version at the time this blog is written) from the market to your prime, you have to setup before you can go ahead with installation by select menu and setup. By default, there are couple options are disable. I choose to enable all of them, specially
  • Bind Android
  • Allow write to /system
  • Allow remount with dev/exec
should be turn on. It's make the chroot integrate better with android.

By default, the debian/squeeze is chosen for distribution/version. Don't forget to choose your hostname, and domain name. I leave the chroot launcher script and other options untouched. 

For the loop file location, I leave it at the default location. You should take note where it is. My is "/storage/sdcard0/Linux.loop". It's important that you need to know about this.

For the loop file size, it's need to be above 300MB. However, we are not going to use this loop file anyway, so choose some thing small, say 300.

Tuesday, October 9, 2012

Carpeting and Traffic light - Conclusion

Don't you feel the approach that I used in the previous post, traffic light setting, is a little bit overwhelming? I don't blame you if you do. But it does demonstrate some thing important, it's show what we can do with the problem.

First, with this approach, I always get an answer which may be "no, I can not do it in 3 turn per cycle" or "yes, I can". Not only that, I get a specific light setup, a pretty good one. Even with more complicate situations, say bigger intersections, I still definitely get an answer.

I also know that there is no way to let every one pass through safely only 2 turns per cycle, no matter how much I try. It is definitely good to know your limit, isn't it?

Moreover, if I have a problem that have hundreds of directions. Once I turn the problem to a graph, I probably going to look up a solution of graph coloring, which suit my case, or use one if it's already out there. Either way, I have a pretty good idea how to solve my problem.

You probably realize by now that I am using mathematical abstraction, a graph, to help us to understand, learn more about limitations and provide a solution to the problem.

This concept is important in computer science, because when you try to solve problem with computers, you actually turn a real problem into an abstract problem in computers. Mathematics give you a good guide line now to implement these abstractions. This what you are going to learn when you go to collage for computer degree, learning to solve problem using mathematics, and learn how to implement it in computers

Sunday, October 7, 2012

Carpeting and Traffic light, what do they have in common? - Part 2

In the previous post, I define a carpeting problem. It quite a simple problem that can be described, including a solution in couple paragraphs. The traffic light problem is quite more complex.

Traffic light setting: I want to setup a traffic light on a intersection (figure 1), which A, B, and C are two-way streets, while D is a one way street, and have to make sure that every one can get thought the intersection safely.

figure 1
If I setup the light as follow:
  1. let A go to B and C, and block every one else
  2. let B go to A and C, and block every one else
  3. let C go to A and B, and block every one else
  4. let D go to A, B and C and block every one else.
There will be 4 turns signal for each cycle. I  ensure that every one get their turn, and pass through the intersection safely. 

You may notice that while we let A go to B and C, in the same time C can safely go to A. This raise the question, can we do 3 turns signal in each cycle, and still let every one get through safely?

Carpeting and Traffic light, what do they have in common?

Nothing ... you may say so. And I completely agree with you. However, what I am talking about is two problems, Carpeting a house and Traffic light setting. They may have some thing in common that might surprise you. I have to be more specific, haven't I?

Carpeting a house: I want to fully carpeting this house with one condition. I want every room that connect to each other have different color. For the room that is not directly connect, may share the same color.

figure 1
For this particular house, kitchen (K) and living room (L) are connected, they have to have different color. Assuming the small area in front of rest room (R) is belongs to kitchen. All bedroom (MB, B1 and B2) can have the same color. In this case, 3 colors is enough. But can I do this with two color?

Remember the small area in front of restroom is belongs to Kitchen, so B1 and B2 are not directly connected, so living room.
figure 2

Let draw this floor plan differently (figure 2):  All the room are connected directly to kitchen, except the master bedroom (MB). We have to choose two different colors for kitchen and living room, say C1 for kitchen and C2 for living room. Then we can use C1 for master bedroom and C2 for bedroom 1 and 2. Using two colors is possible then.