03 November 2015

Stand still - and you are going backwards!



Imagine you are standing on a moving escalator that is supposed to take you from a Ground level to the Top level - only that its moving in the opposite direction. If you stand still on such an escalator you will hit the bottom soon. In order to not hit the bottom - you need to "at least" walk at the -ve speed of the elevator. For an observer it would still be standing still but thats the least one could do especially being in the software industry.

My blog once had crossed a million page views and the most active time I was during 2010. I think looking back - that was the time I had cracked any problems thrown at me. And I enjoyed it. I still could crack em only that I realise recently I've become rusty. However, I developed a variety of different skills down the road.

Alright I think I am diverging too much from what I wanted to say. In the IT world, one needs to constantly keep learning, keep pushing the limits, get hands dirty on a variety of technologies. If you are a manual tester - try poking at Automation testing. If you are an Automation Engineer - learn about performance/scalability testing. If you are an application developer - explore system development. If you are a software architect - see what is going on in that particular space and try to remain on top. That is the only way you could make sure that you are always on the 99th percentile in this industry.

One has to realise doing the mundane and similar work won't scale as we have already headed and running in the First order differentiation of time (speed). Second order differentiation (acceleration) will make us even more redundant. Can't imagine what we differentiated even one more step. The only way to sustain such challenging epochs is to learn. Learn learn learn!

Cheers!
Braga

Printing the permutation of characters from a classical mobile phone

One of the problems I recently faced.

Input: A set of numbers from a mobile keypad
Output: Print all the combinations of characters for that given number.

For example if the input is 26. Then possible outputs are am, an, ao, bm, bn, bo, cm, cn, co

Solution:

There are many ways to solve this problem. One is using a tree. Another way is to simply do it with recursion. I solved this using a linear approach. If you convert the problem from the text domain to number domain - it will seem very simple.

For example - if the input is something like ['abc', 'def', 'pqrs'] (transformed from digits 237). Then the output would have a simple rule - the first character should come from the first element, second from the second and so on. Writing in an incremental number form for the indexes, you would get:

0 0 0
0 0 1
0 0 2
0 0 3
0 1 0
0 1 1
0 1 2
....
2 2 3

That's it - problem solved. Programatically expressed it here

17 August 2015

VIM Cheatsheet

A one page printable cheat sheet for VIM in PDF Format. Direct URL: https://goo.gl/Yu2rpG




Source and credits to RICHARD TORRUELLAS : http://vim.rtorr.com/

Cheers!
Braga

08 August 2015

Ransack namespace collision - The search method

I am a big fan of Ransack, the Rails gem for quickly building a quick and easy Search based on pure ActiveRecord queries. For projects that require some on the fly search without having to set up dedicated Search Engines like Elastic Search, SOLR, ThinkinSphinx - this is a very juicy alternative.

However I recently faced one problem when I introduced this to a project where there already was SOLR integrated. It was because of the collision of the search method.

This simply patch did the job for me. All you have to do is include it to your initializers folder say under config/initializers/ransack.rb

That's it, the Ransack library will no longer collide with your existing suite. As a matter of fact the method search deprecated already and the library itself wants us to use the Alias ransack

Cheers!
Braga

02 June 2015

Good bye Heroku !

I primarily work on Ruby on Rails and I use to deploy one of my pet projects to Production entirely on the Heroku's free tier. It was a wonderful two years to see having my application running there during which time I had around a thumping 10 Million page views! Heroku's free tier is one of the most used around the world for quickly showcasing development applications. However, my Production application was such that all my needs felt well under Heroku's free tier.

Here are some of the freebies I use to enjoy
  • Free Dedicated (techincally not) machine with 512 MB - The reason its technically not because you are allotted a free worker that would go to sleep if the application is not up for 30 minutes. However you could easily overcome this with something like Pingdom.
  • Free Postgres Database - Although there was no specific size limit to this other than a maximum number of 5000 rows, I would still say this is one of the best things Heroku offers. Not only your application can talk to it, anything anywhere from the world can talk to the database directly. And that too 100% free!
  • Free emails via Sendgrid - A maximum 200 emails per day! Sendgrid's free Addon allows me send upto 200 emails per day. My application's requirement was in the range 20-40.
  • Unlimited Job Schedulers - Although they would take one entire and the only worker allotted to you to spin, this is a boon considering you do not have a full fledged linux machine with you. I use to have only one rake task that takes a routine back up of my data as CSV and email it to me.
  • Free PG Backup - Again, if you want to routinely backup your data, you don't have to write any complex code. Heroku provides this out of the box and for free!

Now that Heroku is pulling down the curtain on the 24 hour availability or to pay $7 a month, I have no other option to switch to a much better alternative - that is Digital Ocean. Sign up here for a free $10 credit - https://m.do.co/c/21693a3f9c9e

Sad to depart ways, but you made me learn a lot of stuff in these two + years.

Cheers!
Braga

27 November 2013

My first ruby gem horoscope is live

I wanted to write a rubygem on my own for a long time. Never had the time really to write one. However, recently I had some spare time during which I wrote this gem called horoscope. This gem will help drafting the horoscope of a person given the birth date, time and the place. Source is hosted at Github. As with every other open source project out there, feel free to fork it and add more to your liking

Screenshot at Github


Proud to release this and I am planning to add more features to this. Also, planning to release different gems. If you want something particular or specific, don't be shy, add it in the comments section and I would be more than happy to contribute!

Cheers!
Bragboy

18 November 2013

Tree Traversal in C++

We've already seen many implementations of tree and its uses in java. Lets look at a simple implementation of traversing on a tree in C++. Code has been given below which I think is quite self descriptive.

Regards,
Jack


Write a program to find the number corresponding to a alphabet and vice-versa with the given rule

Problem:
A = 1
B = A*2 + 2
C = B*2 + 3...
1. Write a program to find the number corresponding to a alphabet and vice-versa.
2. Given a series like GREP find the total in numbers. Given a number like 283883, find the shortest alphabetical series corresponding to it.
Compute above recursively when required and do not pre-compute and store beforehand.

Solution:
The solution is straightest-forward. Java code given below.

Cheers!
Braga

Write a program to build a small knowledge base about top "N" movies listed at IMDB

This was an interesting problem that I came across with. This problem is like an open book exam and race against time. The problem had to be solved within an allocated amount of time with a technology stack that is limited only to Ruby/Python/Node.js/Perl.

Few googling here and there and with some knowledge about Ruby I was able to solve this problem within an hour.

Problem Statement:

Build a small knowledge base about top "N" movies listed at http://www.imdb.com/chart/top. Each movie should mention the cast written on the individual page of the movie.
Given the name of a cast member, you should be able to return the movies out of the top "N" movies he/she has acted in. "N" will be passed by the user.
The knowledge base should be built during the runtime and stored in a data structure of your choice.
Q1. For example, if N=3, then you should parse the first 3 movies' individual pages from that page (http://www.imdb.com/chart/top) and build the knowledge base of the cast (there are about 15-20 on every page). Upon querying for "Morgan Freeman", you should return "The Shawshank Redemption". Do not use any external api for this.
Use only Ruby/Python/Node.js/Perl.

Solution:


Cheers!
Braga

14 December 2012

Novel finder Program

This was one of the problems that I had faced during one of my interviews. The problem sounded interesting and I was given 1 hour to solve it which I did quite comfortably.

Problem:


The K-doublets of a string of characters are the ordered pairs of characters that are K distance from each other. A string is K-singular if all its K- doublets are different. A string is Novel String if it is K-singular for all possible K distances.

For e.g. take the string FLBL, its 0-doublets are FL, LB and BL. Since all these are different, FLBL is 0-singular. Similarly, its 1-doublets are FB and LL, since they are different FLBL is 1-singular as well. Lastly, the only 2-doublet of FLBL is FL, so FLBL is 2-singular. Hence, FLBL is a Novel String.

Note that the fact that FL is both a 0-doublet and 2-doublet is insignificant as zero and two are different distances.

Input:

The input is one or more non-empty strings of at most 100 capital letters, each string on a line by itself, followed by a line containing only two dollars ($$) signaling the end of the input.

Output:

For each input line, output whether or not it is a Novel string using the exact output format shown below.

Sample Input: (Input File: novel.in)
FLBL
FFLL
ORDINARY
R
QYQRQYY
$$

Sample Output: (Output File: novel.out)
FLBL is a Novel string
FFLL is not a Novel string
ORDINARY is a Novel string
R is a Novel string
QYQRQYY is not a Novel string

Solution:

To attack this problem, you first have to find all the doublets for each distance starting from 0 to the maximum distance which would string's length minus one. Then keep pushing them into a hash set. Whenever the add() method of the Set returns false, it means we are adding a duplicate hence, its not a Novel text. Here is the Java source code.


Cheers!
Braga

27 July 2012

Windows Phone 7.5 - Home button Navigation and Exit Confirmation

While developing a windows phone application, the one which involves navigation to various pages starting from something called as a homepage would quickly become annoying. You will find yourself in deeper pages and as a means on coming back to the home page, you will keep pressing the back button so many times it will exit you out of the application.

There are couple of problems associated with this. The user is forced to keep in mind how deep he has in the navigation page. The other main problem is he will not have patience to wait to go to the home page and pressing the back button on the first page will obviously throw you. This is because the navigation details are stored in a stack.

It is always nice to provide a home button to the end user on all the pages so that on click of them will take it to the home. Also, you can always have a confirmation dialogue fired while exiting the application. First problem first.



This code armed with a Home button will do the job cleanly for you.

private void Home_Click(object sender, System.Windows.RoutedEventArgs e)
{
    int depth = NavigationService.BackStack.Count();
    for (int i = 0; i < depth - 1; i++) { NavigationService.RemoveBackEntry(); }
    NavigationService.GoBack();
}

The funda here is it will empty all the stack and will call the back action of the navigation service just once making sure that the next back() will empty the stack and will make you quit.

As I told before adding this code to the MainPage.xaml.cs will show a confirmation popup while quitting the application so that user only deliberately quits but not accidentally.



protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
    if (MessageBox.Show("Are you sure you want to exit?", "Exit?", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
    {
        e.Cancel = true;
    }
}

These techniques are very simple and subtle but will improve the usability of your application by leaps and bounds.

Cheers!
Braga

Windows Phone 7.5 WebBrowserTask - Simple Yet Powerful

WebBrowserTask is one of the most simple yet powerful tasks I found in Windows Mobile development. It is amazingly simple, just two or three lines of code and super tempting to include it in your project. Lets quickly see how to use it and places where you can utilize its capability at the presentation level.

Enough intro, here is the code

WebBrowserTask pptTask = new WebBrowserTask();
pptTask.Uri = new Uri("http://www.iasted.org/conferences/formatting/presentations-tips.ppt");
pptTask.Show();

Damn simple isnt it? You could use this in places where you need to launch or open an URL in the inbuilt Internet Explorer. And the best thing about this is it will automatically detect the attachment file types (most of them) and opens them cleanly. For example, it could read ppt, pdf, xls even videos and open them cleanly. You can make the integration look rich by providing a button in the Application bar. Following is a screenshot.



Besides opening simple URLs consider using this Task to open regular attachments and make your end application look extremely rich with just one or two lines of code.

Cheers!
Braga

26 July 2012

Windows Phone 7.5 - ShareStatusTask's Limitations

The Microsoft.Phone.Tasks namespace in Windows phone 7 is a neat way to access the default phone specific tasks. One such task is the ShareStatusTask. If you want to say share your status on facebook or twitter or to windows live account, you can do it in one shot.

The code looks very simple,

Very cool isnt it. But the usage of this kind of limited. You will have to have all the accounts configured already in your phone settings. I was looking for a direct solution to just provide a way to tweet. But it was not possible to tell the ShareStatusTask to open it only if it has Twitter. I had asked this question in Stackoverflow only to double confirm the above said point. This feature is not available in Emulator, hence posting a screenshot from a real device.



Anyways, this is one of the cool tasks available and just two to three lines will add social feature to your App, so do add it whatever be the case.

Cheers!
Braga

25 July 2012

Windows Phone 7.5 - Rating/Review control Packages

There was a simple specification in my application that needs to have a Review feature. I started off with Google, but the irony here is the keyword "ratings" or "review" accompanied with Windows Phone 7 did not return results that I've been looking for. Instead it was returning the features and real reviews written for Windows Phone 7. The catch here was to convert my keyword to star rating control. I found some decent resources which are implementable, let me summarize them.

1) Silverlight Star Rating Control - This had most of the meat I was looking for. It was actually designed for silverlight and not really tested in Mobile or I can put it like I was not able to find snippets for Mobile implementation. However, it worked alright. The problem was that this package was not having a mechanism to NOT have the Half-Star Mechanism. Going through the source code confirmed that and I do not want to build a new dll out of it and include it in my project. So gave this up. But I would highly recommend this for web client.



2) The next one I stumbled upon was from a guy Jani Nevalainen. He has blogged it in his website and it had what I needed. But since it was not on a codeplex site/github, and due to its lack of documentation or support I had to push it down the priority list. I have not tried this, but my gut says it will surely work.



3) Another one which really looked promising was from a blogger named Matthieu. I thought it had everything and even the source on Github and the demo project worked flawlessly. I was expecting for a library file so that I could include it in my project and breathe easily. Unfortunately, that was not something already available. Yes, I can go through the code and compile a source out of it. But I was really lazy to do so. Hence dropped it. But I have to say this was a really good and careful implementation. Kudos to the Author.



4) And the winner was Coding4Fun! The most sexy and the most famous library for the Windows phone 7.5. It has everything and the forums are really great! Every week you can see builds coming and they are very much responsive to the features that you are suggesting. I could not say that this is the Open source folks' answer to Telerik, but this library is really good. I used their super slider to design my review page. I know it is not like the star mechanism, but still this did the job and it did really great for me!



Cheers!
Braga

19 July 2012

Exception Handling in Windows Phone 7 - When all else fails

I was recently testing something with Toast Notifications with Windows Phone 7.1. Visual Studio that I use has amazing debugging features like any other IDE available IDE out there. Here was scenario. I need to send two types of Notifications Raw and Toast Notifications to my mobile and need to assert scenarios. Debugging the Raw Notification was very simple. However, I was not able to debug/step into when I launch the application after clicking the toast Notifcation.

It was really painful not to be able see any exception message. It was very annoying. Then I found a way to at least know what is happening. To do that, you can just go the App.xaml.cs file which has all the main entry and exit points to the application. To read more about this, just google life cycle of a windows phone app.

All I had to do was add a message box to the method Application_UnhandledException in the App.xaml.cs.

private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
    MessageBox.Show(e.ExceptionObject.Message + "\n" + e.ExceptionObject.StackTrace);
    if (System.Diagnostics.Debugger.IsAttached)
    {
        // An unhandled exception has occurred; break into the debugger
        System.Diagnostics.Debugger.Break();
    }
}

Problem Solved. Now I could see the error messages/exception stack trace in a neat Message box!

Cheers!
Braga