Phil McAuley

Posted December 29, 2009 @ 2:09pm by Phil

I recently had an intervew where I was asked to write a program to count the number of 1 bits in an integer. The solution I presented is the following:

unsigned int bit_count ( unsigned int i ) {
  unsigned int n = 0;
  while ( i != 0 ) {
    n += i & 1;
    i = i >> 1;
  }
  return n;
}

Download Source: bit_count.c

The solution above is O(n) where n is the number of bits. It is not a very efficient solution since it must do around 64 computations for each 32-bit number but it did the job of demonstrating my skills. When I was walking home from the interview I thought of a better solution using a lookup table:

unsigned int lookup_table[] = { 0, 1, 1, 2 };
unsigned int bit_count_lookup ( unsigned int i ) {
  unsigned int n = 0;
  while ( i != 0 ) {
    n += lookup_table[i & 3];
    i = i >> 2;
  }
  return n;
}

Download Source: bit_count_lookup.c

Although this solution will also be O(n) where n is the number of bits, it will require half of the computations as the first solution. By making the lookup table even bigger, you could reduce the computations further.

Tags: C/C++, Interview Questions, Quick Tip

Posted September 11, 2009 @ 7:32pm by Phil

I recently moved some of my other sites that were being hosted on a Linux box in my room to a new web host (I will write a more detailed article on this later on). With that move I have had to generate several new passwords for the ftp accounts and MySQL databases. For this purpose I have created the following Python script to generate random passwords of the specified length.

import sys
import string
import random

if len(sys.argv) != 2:
  print "Syntax: python pass_gen.py [length]"
  sys.exit()

n = int(sys.argv[1])

chars = string.lowercase + string.uppercase + string.digits

print ''.join([random.choice(chars) for i in range(0, n)])

Download Source: pass_gen.py

Tags: Python, Quick Tip

Posted August 20, 2009 @ 4:16pm by Phil

Here is a function I wrote for one of my projects that allows you to download a file from a remote server and save it to your server:

<?php
  function download_file ($src, $dest)
  {
    $handle = fopen($src, "rb");
    $contents = "";
    while (!feof($handle))
    {
      $contents .= fread($handle, 8192);
    }
    fclose($handle);
    
    $handle = fopen($dest, "wb");
    fwrite($handle, $contents);
    fclose($handle);
  }
?>

Download Source: download_file.php

Tags: PHP/MySQL, Quick Tip, Webmasters Tips

A Very Effective Landing Page For Affiliate Marketing Posted August 15, 2009 @ 2:41pm by Phil

I started my adventure with Affiliate Marketing about a year ago when I received a $50 Adwords credit as a promotion with a hosting account I bought for one of my clients. I had briefly read a book on Affiliate Marketing at the bookstore and thought it would be a good way to try out what I learned.

For those of you who don't know what Affiliate Marketing is, it is essentially advertising someone's product in the hope that the amount of referral income you generate is greater than how much you spent on advertising.

As for my adventure, I used Adwords to sell ads for my hosting company. My hosting company offered referral income of 50%, which was roughly $40 for a year of hosting. Throughout my $50 campaign I had one sale. So I would have lost roughly $10 if I hadn't received that Adwords credit for free. This is actually pretty good for the first time in Affiliate Marketing. Besides all the things like keyword optimizations, etc, I think the main thing I did wrong was not creating a landing page and instead relying on my host's website to do the selling. Read more...

Tags: Affiliate Marketing, Entrepreneurial, Google Adwords

Firefox 3.5 - A Glimpse Into HTML 5 Posted August 11, 2009 @ 10:26pm by Phil

I got a chance to upgrade to Firefox version 3.5.2 a week or so ago, and with it the first glimpse of HTML 5 on a browser. I have been messing around with some of the features, and so far am pretty impressed. Some of the features are:

  • New <video> and <audio> tags for embedding video and audio.
  • New, faster, Javascript engine.
  • HTML 5 off line data storage for web applications.
  • Location-Aware Browsing

See the Firefox 3.5 for developers page for more details on the new features.

I also found a list of sites that show off some of the new features (source):

Tags: Firefox, Internet, Tools, Webmasters Tips

Implementing a Related Posts Or Articles Section On Your Website Using Tags Posted August 5, 2009 @ 8:44pm by Phil

A great way to keep traffic on your website is to offer related content on your pages. For me, this means linking my posts to other related posts. The simplest way to do this would be to go through all my posts and link to my other posts which I choose to be related. This would not be the easiest way though. Being a lazy programmer, I want the easiest solution that will require the least amount of work. Read more...

Tags: PHP/MySQL, Webmasters Tips

iFart and a Look At Creating Apps For Today's App Industry Posted July 23, 2009 @ 4:08pm by Phil

While reading Tyler Cruz, one of the blogs I follow, I came across an article about Joel Comm's recent appearance on The Daily Show. Joel Comm is a New York Times bestselling author, internet-made millionaire, and social media expert. He is the same guy who created The Next Internet Millionaire, an online reality show that I watched a year ago where contestants compete to team up with Joel on his next internet project. The Daily Show does it again, and the four minute mock interview segment with Joel Comm is just hilarious! It is a look at Pull My Finger and iFart, two fart applications competing to top the charts in the iPhone App Store. I have embedded the segment below:

 

There’s absolutely no risk using sims freeplay cheats, because you don’t have to download a program to your device or install any software.

This important resources cheats will give you unlimited gold and Free Shopping mod.

Those who have Android phone or iOS phones like Apple ipad tablet, iphone, or ipod, this dragonvale kack 2017 tool will definitely meet their exact requirements.

This software are not just simple to use but is free from all kinds of malware and virus threats.

The fully reliable and working trick will make it easy for you to grab hold of unlimited resources, which is a vital aspect to win powerful opponents.

The msp hack is designed by a group of talented programmers and it is completely safe to use.

You don’t need anything expect a agario hack no survey, e.g. Opera, Mozilla Firefox and others.

 

Read more...

Tags: Apps, Entertainment, Internet, Opinion, Social Networks

Posted July 22, 2009 @ 12:39pm by Phil

Using TwitterQuery, my lightweight Twitter API, this simple function looks up and returns the Twitter followers count of the specified user.

<?php
  require "TwitterQuery.class.php";

  function followers_count ($user_id)
  {
    $twitterQuery = new TwitterQuery();
    $results = $twitterQuery->query("users/show/{$user_id}");
    return $results["followers_count"];
  }
?>

Download Source: followers_count.php

Tags: PHP/MySQL, Quick Tip, Twitter, TwitterQuery, Webmasters Tips

Posted July 18, 2009 @ 11:07pm by Phil

After reading my previous quick tip on reversing a null terminated string using recursion, another one of my friends sent me a link to a very elegant function that prints the reverse of a string using recursion:

void print_reverse (char *str)
{
  if (*str)
  {
    print_reverse(str + 1);
    putchar(*str);
  }
}

Download Source: print_reverse.c

Tags: C/C++, Interview Questions, Quick Tip

Posted July 17, 2009 @ 9:46pm by Phil

A friend of mine recently had an interview where one of the questions he was asked was to reverse a string using recursion and the least amount of memory possible. Here is my solution which reverses the string in-place:

void str_reverse (char* start, int len)
{
   if (len <= 1) return;

   char* end = start + len - 1;

   // Swap start and end chars
   char t = *start;
   *start = *end;
   *end = t;

   str_reverse(start + 1, len - 2);
}

Download Source: str_reverse.c

Tags: C/C++, Interview Questions, Quick Tip