Tag: al

  • Happy New Year 2011 Wishes

    Happy new Year 2011

    I wish ALL of you a very Happy & Prosperous New Year 2011 !

  • Nicolas performing You should be dancing tonight

    Hilarious, isn't it?

  • Maintaining relationship – common friends

    I was posed a very trivial question from one of my friends recently. It picked my interest, since I could not readily solve it, and I had the inkling that there is a better, simple solution for this problem.

    The problem was, let’s say I have a table of users as below
    User's table data

    And these users have friends as below

    User's friends data or more easier to read as User's friends data

    Now, the trivial task was find common friends for users 1, and 5, hence naturally the answer should be

    User 1 : Woody’s friends, and User 5 : Andy’s friends  are

    Woody's friends and Andy's friends

    So, the question I had was what could be the best SQL query that would be return me this result?

    Woody and Andy's common friends

    I thought of 2 different SQL queries

    SELECT f.fid, u.name 'Woody and Andy\'s common friends' FROM
    (SELECT f.`fid` FROM friends f
    WHERE f.`uid` IN (1,5)
    GROUP BY f.`fid` HAVING COUNT(f.`fid`) > 1) f INNER JOIN user u ON (u.uid=f.fid)

    -or- another one was

    SELECT f.fid, u.name 'Woody and Andy\'s common friends' FROM
    (SELECT a.fid FROM
    (SELECT * FROM friends f
    WHERE f.`uid`=1) a INNER JOIN
    (SELECT * FROM friends f
    WHERE f.`uid`=5) b ON (a.fid=b.fid)) f INNER JOIN user u ON (u.uid=f.fid)

    Both seem to work, but is there any elegant way of getting common friends in a much simpler SQL query? At this moment, I logically find first SQL query more intuitive since all it does is pickup friends who appear more than once when users are 1 and 5. The second SQL query seems plain brute force approach.

  • Impolite Japanese

    Got a warning ticket yesterday for parking in a “handicap” zone. (Well, it is convenient to park there, I admit). But look at the ticket content, you’ll be shocked !

    Handicap Parking Ticket

    Here’s how it translates (tried my best, since this is native Japanese and had to look up the dictionary many times)
    This parking place is for [physically handicapped], not [mentally handicapped] person like you. Can make out from a car like yours, a person like you won’t understand. You really think your car is too cool? Aren’t you ashamed to act like a complete idiot? Did you ever see your ugly face in the mirror? Pity you do not understand, thus I noted your car number, model and informed to the nearest police station. Don’t ever park in parking reserved for handicap again !

    Pretty rude and forthcoming for a Japanese, don’t you think?

    I wonder why didn’t this person put his number or contact in case I wish to “consult” to remedy my pitiful situation. No Pity

  • Merging pre-sorted lists

    A while ago, I came across an interesting problem. We have 3 different sorted queues (entries were sorted by time), out of which we had to pickup the smallest time, and send the first arrived entry out of those 3 queues in sequence.

    Q1 Q2 Q3
    2 3 1
    5 4 6
    7 6 9
    8 10 11

    As you can see, all queues (Q1, Q2, Q3) are already sorted, but on application side we have to pickup entries across all queues in sorted order and process them. So the entries should be processed in 1, 2, 3, 4, 5, … 11 sequence. Frankly this is merging of already sorted lists, and a person from computer science background should already know this. But I don’t come from such background, and the solution one of my friends gave was plain amazing.

    The solution was pickup first entry from each queue, compare them, choose the smallest and process it first.

    Q1 Q2 Q3 Smallest
    2 3 1 1

    Thus, here 1 is processed first. Now pickup next entry from queue where smallest was found (Q3 in this example), so now the next comparison becomes like this

    Q1 Q2 Q3 Smallest
    2 3 6 2

    The smallest is 2, so this is processed and next entry from Q1 was picked-up

    Q1 Q2 Q3 Smallest
    5 3 6 3

    And so on… giving us

    Q1 Q2 Q3 Smallest
    2 3 1 1
    2 3 6 2
    5 3 6 3
    5 4 6 4
    5 6 6 5
    7 6 6 6
    7 10 6 6
    7 10 9 7
    8 10 9 8
    10 9 9
    10 11 10
    11 11

    A simple, and elegant solution, isn’t it? Is there any other better alternative?

  • Amrita enjoying in Doho park

    Amrita enjoying in Doho park
    Amrita enjoying in Doho park
  • Spring and Logback

    IoC, or DI definitely takes a perspective turnaround inside your head, but you get around it slowly.  I was playing around ways to integrate LogBack, and Spring – essentially around having Spring give me a pre-created instance of LogBack logger.

    I searched around posts, but most people seem to be against using Spring just for substituting one-line of Logback (Logger log = LoggerFactory.getLogger("LogbackTest");) to get your logger.

    I, on the other hand, was more interested in how to get Spring give me a LogBack logger instance without too much contrived hand-written code to achieve so. And with a bit of reading through Spring principles, documentation I found the way.

    Essentially, when using Logback’s LoggerFactory you have access to only a single getLogger() factory method. This is static which makes things a bit different for what Spring would call a bean – a class providing constructor, getter, setter methods. To circumvent this non-bean style, Spring provides what you call as static initializers a.k.a substitutes for constructors, which allow you to call a static method in lieu of calling a constructor on an object.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
     "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
     <bean id="bean1" class="org.slf4j.LoggerFactory" factory-method="getLogger">
     <constructor-arg value="LogbackTest" />
     </bean>
    </beans>

    Now, this bean1 can be used as a regular bean inside your class

    ApplicationContext ctx = new FileSystemXmlApplicationContext("logbacktest.xml");
     Logger log = (Logger) ctx.getBean("bean1");
    
     log.debug("This is my first message");
     log.info("How about this information message");

    Throw in a logback.xml in your classpath, and viola you have a nice Spring injected dependency – log in your code, while still using Logback!

  • Spring IoC, DI quick tutorial

    There has been an (evident) craze amongst Java community with contrived terms such as IoC (Inversion of Control), DI (Dependency Injection) mainly through Spring. I initially found the connotation “Don’t call me, I’ll call you” a bit difficult (yeah, it’s bending your head upside down) to understand, but after spending few hours around Spring documentation, I get the gist.

    To a layman (Java layman, of course) it is helpful to picture that each Java program is a set of one or more classes, which act together. Essentially, this means classes are “dependent” on some classes in order to be fully functional. Usually, the class requiring functionality of another class instantiates the class, and uses it. This is called coupling because class instantiates the object of required class. What if we always got an “instantiated” instance of required class, and our class did not have to worry of instantiation? – This is called IoC (Inversion of Control) principle in Spring, and it achieves this by providing ready-to-use instance (injecting dependency) to your class. This has some important uses, since now you don’t worry of creating connections to databases, loggers to log4j, or sessions for JMS queues. Spring will create these for you, and your class can focus on the actual purpose – using the pre-instantiated ready-to-use object.

    Get it? OK, to simplify it further, let us assume you have a class A having one field – log. You want to use log to log information, but your class nowhere has the logic to instantiate or initialize log. You accept a pre-instantiated log through constructor, or getter/setter methods, and you will have a ready-to-use log instance passed to your class via Spring!

    I won’t go into details of Spring further, since Spring’s own documentation on http://static.springsource.org/spring/docs/2.5.x/reference/index.html is the definitive source to look into.

  • Interesting books – Crystal Skull

    Recently, I read an interesting book. Was an good read, after a long time focusing only on technical books.

    Crystal Skull - Manda Scott

    Is this for real?

  • Javascript sprite animations

    Javascript sprites weren’t as difficult as I thought. With a little bit of help, math, some css, js, images (of course) a decent animation can be shown without using any flash at all. I coupled it in a small class Animator, and wrote a small demo for anybody interested in using it.

    http://www.naiksblog.info/jsspriteanim/index.html