Category Archives: Programming

Long

From int to long

The past few days, I have been working on a programming assignment to develop efficient multiplication algorithm. I have passed all graded test cases but got stuck at the non-graded challenge task, task Y. The requirement is 500k-digits numbers multiplication in 2 seconds.

My algorithm made use of both Karatsuba algorithm and a technique to combine neighbouring digits together (similar to converting from decimal to a large base).

Initially, I used int for all the variables, which allowed me to combine 8 digits at a time. This got me 2/10 points for the task. Then I thought why not use long? (I was using Java so no long long, and we were prohibited from using BigInteger) After changing my variables to long, I discovered that I could only increase the max digits from 8 to 10 before errors in the result showed up. I knew that theoretically the max digits should be around 16, but I did not bother finding out why and assumed that other parts of my code is limiting the max digits.

Two days later, after exhausting other possible optimization techniques, I still could not get better than 2/10, so I decided to investigate the issue with max digits. This time, I used the debugger and checked all the intermediate steps carefully. And I finally found out what was wrong: I constructed an array with base 10 raised to different powers, for combining the digits. However, the array was in the int range, therefore causing the error when increased the threshold for max digits. After resolving the issue (changing it to long), I was able to get 4/10 for the task. It might be that I need a much better algorithm for full marks, but at that point of time, I was filled with joy for figuring the bug out.

Long term goal

Setting a long term goal is important. I only truly appreciated it after experiencing regrets. When I started playing games, I was always able to identify an ultimate goal for me and work towards it. If I found out that the goal was too hard to achieve, I would drop the game. If I managed to achieve the goal after putting in a lot of effort, I will look for another one.

However, game is too short compared to life. What I thought was the long term goal was too short. Before I graduated from junior college, all I wanted was entering a good university with good computer science department. I assumed that this would lead me to my next goal naturally. Now it looks like I was thinking too short term. I failed to see many other factors that would affect the really long term, like the well-being of the IT industry, the general perception of technology and what I envision myself to be 20 years down the road.

It is funny that I actually did an exercise back in junior college on “what I imagine myself to be in 20 years”. It was my best chance to plan for long term. However, I wasted it by putting down vague descriptions like “working in a company of global scale”, without actually thinking through what it meant.

Now I really need a long term goal, but how long? Should it be more than 5 years? Or even 10 years? Going back to the discussion of games, it should be challenging, and it should take a lot of effort. Obviously getting an internship would not fulfill the criteria of challenging, neither would graduating from NUS with a job offer. This time, I need to think through, considering the feasibility and the challenge, the possible avenues and obstacles.

TBC…

Moved to self-hosted blog on DigitalOcean

Today is a big day for this blog as it moves from WordPress.com to a self-hosted WordPress site on DigitalOcean.

The process of transfer was easier than I expected. Moving content was easy, just export and import.

DNS and domain were the main issues. I had to point my name servers to digital ocean instead of the default wordpress ones. Then I need to create DNS records in DigitalOcean. Finally I need to configure the WordPress settings (which is just Apache settings for host name).

After finishing all these, I realized that my domain still points to the old WordPress.com site. It turned out that NUS cached the site (via swiftcache2-pgp.network.nus.edu.sg) and did not refresh it after I changed the name servers and the domain mapping. So I had to use VPN to verify that my site is working properly for everyone else (indeed it is!).

Moving to self-hosted site means a lot of changes. Some are what I have expected, like the freedom to use plugins and customize anything I want. Some plugins look really cool, like Editorial Assistant by Zemanta, which suggest links, images and tags automatically when writing new posts.

However, there are some downsides that I did not expect. Stats page was gone. I was under the impression that it should be a built-in function but apparently it is not. So I went to search the plugins and found a good replacement – WP Statistics. Later on, I realized that I could get back some WordPress.com functionality with Jetpack.

Overall, I think it would make a good decision as this blog has become more independent. I have control over more things than I used to. Although there are price to pay, literally for renting servers.

Whatsoever – My Forth Android App

Link to the app in Play Store: https://play.google.com/store/apps/details?id=thack.ac.whatsoever

It all started out as an experiment on new views in Android L developer preview(now known as Android Lollipop). Sometime at late September, I was researching on how to Card-like views in Android and found out that CardView is part of the new Widgets introduced in Android L, together with RecyclerView.

When trying out RecyclerView(which is a replacement for old ListView), I thought of this idea to develop a Android app as a search engine for social media sites. I liked the Facebook style where each post is wrapped inside a card, so I want to build a interface where there is a RecyclerView, inside RecyclerView there would be a list of statuses/posts, each wrapped in a CardView.

For the social media site integration, I started with Twitter, and the integration with Twitter API was quite straight-forward thanks to a library called twitter4j. Everything is well-formatted and follows Java conventions and I did not have to do to much to populate the data into my CardView and RecyclerView.

Then I went on to try integrate Facebook, however, unfortunately, it appears that Facebook Graph API does not allow searching of posts/statuses. Hence, I had to give up on Facebook and try other social media sites.

The next one was Google+, there is a Google+ API client for Android, however, it requires logging in. I did not want that since I want to user to be able to search without having the trouble to log in. So in the end I used the Google APIs and parsed the json “half manually”. That is to take advantage of the pre-defined Google+ classes on Google+ provided in the Google+ API Java client, such as com.google.api.services.plus.model.Activity . This was pretty cool as all I had to do was to use response.parseAs(ActivityFeed.class).

Then I decided that Instagram should be also integrated. Instagram has a pretty well-documented API although it is a relatively new platform, however, there are no native or 3rd party libraries for Java or Android so I have to manually parse the json response. This involves some error handling but was still manageable. A good practice would be to create a Class and use response.parseAs, but I was a bit lazy and decided to just use some local variables.

At that point of time, CardView and RecyclerView were not well-documented, in fact there were no official documentations (now they are documented). So I have to work with limited resources and try to figure out how to use them by myself, it was a painful but exciting experience for me. Of course I put in a lot of time in designing the UI and improving the UX. I also got quite a few suggestions from my friends I tried to do modifications to improve the app.

So here it is, the Android App is in Google Play Store, please do give it a try!

Orbital Project [Apollo 11] – Graphpedia

During the summer vacation, I joined the Orbital project in SoC. This is a module-bearing project for SoC freshmen to get some experience in developing real-life applications(mainly web app or mobile app).

The final product for my team is now hosted on Digital Ocean (initially on Heroku): https://viz.paradite.com/

This a team-based project so I partnered up with Dongyan. I was in charge of backend, routing and the visualization and she is in charge of UI and UX. Continue reading

Card Safe – My Third Android App

Link to the Android app in Play Store: https://play.google.com/store/apps/details?id=thack.ac.cardsafe (still waiting for listing as of now)

This project started off with the idea that I can take advantage of the NFC functionality in Android to read data from NUS Matric Card and develop some app to facilitate processes like registration and collection in various events.

Unfortunately, I realized that the matric number sector in the card is key-protected I believe the staff would not let me have the read/write access to the data inside. So that means I can only get my hands on the ID of the card, which is given by the manufacturer, public and most importantly unique(although there may exist techniques to duplicate the cards with the same ID illegally).

I was thinking about the card’s unique ID and necessity for NUS students. Then I suddenly got this idea, why not save some information and use the card as a key to access it. I thought this would be quite useful and relatively easy to implement, so I started working on a prototype. Setting up database and the single activity with a single layout file was quite easy for me, especially just after I have worked on a bigger Android project. The only thing that is troublesome is the different tags in different cards and their different ways to access the card. I have tried ezlink cards and DBS Debit Card, however, I was not able to get something like a unique ID from either of them. In the end, I only managed to get the NUS matric card working.

After a few hours of coding and designing, the version 1.0 with basic functionalities like setting up new “safe”, retrieving content of the safe and modifying existing safe was finished and uploaded to Google Play Store around 3am. Hope it gets good feedback and proves to be a fun and useful app for people(mostly for NUS students now).