Sunday, February 26, 2017

Get Unique Line Counts on Linux

cat Duplicates.txt | sort | uniq -c > Counts.txt

Friday, February 17, 2017

Drupal Contact Form - Missing or Disappearing Emails

Linux sendmail sometimes drops emails mysteriously without any error message or log of the written email.

/var/log/maillog will show an entries such as:
Unauthenticated email from is not accepted due to [DMARC]

The way to fix this is to have the domain name of the current server in the from field, and the actual from address in the reply-to field.

This can easily be done with Drupal 7 using the contact_reply_to module. Drupal 8 does not require the module.

Monday, February 13, 2017

Drupal's Confusing Online Readme Text Files

Module pages usually say:
"The most complete and up to date documentation is included with the module, in the README.txt file."

But the online README.txt files linked to there usually include documentation for unreleased updates. This can cause considerable confusion / unnecessary trouble-shooting when newly installing modules.

Always use the README files included with the module, not the ones online.

Performance of Facebook promotions

Regarding the promoted post Quotes - Warren Buffett and Benjamin Graham.
Here are the top 10 duplicate shares on the post:

Lucious White - 28 shares
Ako Nai - 17 shares
Stephen Schreiber - 17 shares
Nathan Musikchild Ray - 9 shares
Farakhan Norris - 6 shares
Beau Forester - 5 shares
Dennis Vinci - 5 shares
Bob Holden - 4 shares
James Thomas Downey - 4 shares
T Donebullshidden Knight - 4 shares

These accounts have been observed sharing and liking other promoted posts too.

The total list of displayed shares is only 382, even though the displayed count is 1363.
Understandably, this is due to private shares.

But only 244 of the shares are by unique people.

[Support email #2]

As you can see, the high bounce rates are only for Facebook clicks.
The bounce rates for users from Google, Bing, Yahoo etc are much lower.

In any case, my concern is not the high bounce rate in itself.
That could be content and context specific.

My real concern is the large number of confirmed duplicate shares by
certain users. These duplicate shares have me worried about the authenticity of the
likes and clicks as well.

Monday, January 30, 2017

Drupal DDoS Prevention Code

// The following code will ensure that there are no more than 30 non-cached requests in 60 seconds.
// Please adjust the first two variables below in a manner optimal to your server config.

// Note that this only protects heavier non cached pages.
// A powerful enough attack can even overload your server with requests for cached pages.

function hook_init()
    $MinTime = 60;
    $MaxAttmpts = 30;

    $MyAppDDoSCheckUrl = variable_get('MyAppDDoSCheckUrl');
    $TimeCheckNow = time();

    if ($MyAppDDoSCheckUrl == '')

    $SerDDosVars = explode(',',$MyAppDDoSCheckUrl);
    $TimeLast = intval($SerDDosVars[0]);
    $ReqAttmt = intval($SerDDosVars[1]);
    if ((($TimeCheckNow - $TimeLast) < $MinTime) && ($ReqAttmt > $MaxAttmpts))
        header('HTTP/1.1 503 Server busy, try again later');
        die('The server is currently overloaded. Please try again in a couple of minutes. Thank you!');
    else if (($TimeCheckNow - $TimeLast) < $MinTime)

Toggle #input_group on fields in Drupal 7x

Here's the code for iterating through all fields in a form and setting $element['#input_group'] = TRUE;
function hook_form_alter(&$form, $form_state, $form_id) 
 if ($form_id == 'nodetype1_node_form') 
  iterateForm($form, 'setIgt');

// Generic iterate function. Can be used with any callback.
function iterateForm(&$form, callable $callback) 
 foreach (element_children($form) as $key) 
  $element = &$form[$key];
  $children = element_children($element);
  if (empty($children)) 
   call_user_func_array($callback, array(&$element, &$key));
   iterateForm($element, $callback);

// Custom callback
function setIgt(&$element, &$key) 
 if ($element['#type'] == 'textfield')
  $element['#input_group'] = TRUE;

Thursday, October 27, 2016

Shortest Code For Generating Prime Numbers


for ($i = 1; $i < 1000; $i++)
    $print = TRUE;
    // Optimized till square roots
    for ($j = 2; ($j*$j) <= $i; $j++)
        $print = FALSE;

        if ($i % $j == 0)
        $print = TRUE;

    if ($print)
        print $i.',';


Wednesday, October 26, 2016

The Essence of Computer Programming

Computers are basically for the automation of repetitive work.
So programming computers is essentially the art of automation of repetitive work using loops.

Thus, the most essential thing one needs to understand to program computers is how to write loops.
That covers everything essential.

PS: Recursion is assumed here to just be a more advanced form of a loop, which is just a more elegant solution for certain types of problems.

C Code for Numerology 9999

Got a strange request from a friend:

Generate all 4 digit numbers whose recursive sums evaluate to nine, and whose non-zero digits are in ascending order.

Anyway, here's the code.

First run as usual :-) Apart from a few syntax corrections.
No logical errors.


for ($i = 9; $i < 10000; $i++)
    if (isAsc($i))
        if (isSumNine($i))
            if ($i < 10)
                print '000'.$i.PHP_EOL;
            else if ($i < 100)
                print '00'.$i.PHP_EOL;
            else if ($i < 1000)
                print '0'.$i.PHP_EOL;
                print $i.PHP_EOL;

function isSumNine($num)
    $temp = $num;
    $sum = 0;

    while ($temp > 0)
        $sum += $temp % 10;
        $temp = floor($temp / 10);

    if ($sum > 9)
        return isSumNine($sum);
        return ($sum == 9);


function isAsc($num)

    $temp = $num;
    $digit1 = 0;
    $digit2 = 10;

    while ($temp > 0)
        $digit1 = $temp % 10;
        $temp = floor($temp / 10);

        if ($digit1 > $digit2)
            return false;

        if ($digit1 > 0)
            $digit2 = $digit1;

    return true;

The code can be easily converted to any C based language, by removing the $ signs and adding Int and Bool type declarations for that language.

Easiest Way To Run Code on Mac OSX

Most links will tell you to install XCode so that you can use GCC to run C / C++ code.

You don't need to. The easiest way is to use the existing PHP program on your Mac OSX.

Monday, September 26, 2016

The Software Industry and Computer Programmers

A google employee once wrote an article in which he said something like:

"Often we would interview a programmer who had an incredible amount of knowledge on a wide range of subjects, but couldn't program Hello World in any language."

It's really amazing how many people in the software industry simply know nothing about programming computers.

We're talking about core technical people such as Product Managers - not people in unrelated departments such as marketing - who have big plans and strong opinions on everything related to technology, but lack basic technical skills.

To put it in perspective, imagine an entire department of road planning where no one really knows to drive.

Sunday, September 4, 2016

Tradeadexchange virus on Android - Only Solution

None of the other solutions online seem to work.

The only solution seems to be:

1. Clear all browser caches completely (both in settings and in app info, to be sure)
2. Reset modem
3. Reset router (if different from the modem)


The virus settles in the browser, and the modem + router.
If any of the three is not clean, the virus seems to infect all three.

There's no need to uninstall the browser or reset the phone.

Monday, February 22, 2016

The 12 best interactive Google Doodles

1. Les Paul's 96th Birthday

2. Robert Moog's 78th Birthday

3. 30th Anniversary of PAC-MAN

4. Alan Turing's 100th Birthday

5. Google Instant Launch

6. Hurdles 2012

7. Basketball 2012

8. Jules Verne's 183rd Birthday

9. Eadweard J. Muybridge's 182nd Birthday

10. Art Clokey's 90th Birthday

11. 25th Anniversary of the Buckyball

11. Total Lunar Eclipse. Live imagery provided by Slooh.

12. John Lennon's 70th Birthday

And two animated ones:

1. Mother's Day 2012

2. Martha Graham's 117th Birthday

Monday, December 7, 2015

A more reliable interview process for computer programmers

One of the first things I noticed in the software industry, was that verbal technical interviews tended to highly unreliable. On many occasions, I had observed average performers at work clear interviews that top performers had not. The former didn’t know much about programming computers, but were very good at clearing interviews. They had the confidence and knew the answers to all the usual interview questions, which they usually brushed up on the previous day.

As an interviewer from Google once remarked, some candidates were often extremely knowledgeable in interviews, but in practice, could not code the most basic programs in any language.

The problem is that verbal technical interviews rely largely on communication and confidence. Confidence is normally considered a sign of ability but the two are not actually correlated. Studies have shown that mildly capable people often tend to be more confident than extremely capable people (Dunning–Kruger effect). But as the average performer’s abilities improved, so did his awareness of his own shortcomings, thus bringing his confidence down to more realistic levels.

The problem can be much more acute in a field as abstract as computer programming, and it was made worse by the fact that most technical interviews were usually conducted by engineers who were not able to get development work. So not only were the interviewers less skilled, they would select candidates more like themselves; consciously or subconsciously asking the ones they liked the wrong questions.

My solution as an interviewer was to eliminate the verbal technical interview altogether. Instead, all potential candidates were given a simple programming problem to solve on paper - using any algorithm and any language they liked. Syntax was not checked and there was no specific time limit. The result was edifying. A person would either solve the problem in 15 minutes, or not at all.

Typically, out of a group 30 candidates, 25 could be eliminated this way in 15 minutes. The Team Leads and HR were then free to interview the others at their leisure. The technical ability of the remaining candidates was no longer in doubt, just their fit. Most rejected candidates would not even contest the results. A few disagreed, but with a laptop, could quickly be shown the flaws in their code.

Suddenly, the technical interview had become a lot more open, fair, faster and reliable. Instead of testing what someone can memorize, the interview now checked how a person thought and worked on a day to day basis. The same abstractness and peculiarity of programming that had made verbal technical interviews so subjective, now made it far easier to find the best candidates.

In fact, computer programming is probably one of the few fields in which such a direct test of aptitude is possible and yields such accurate results.

A Simple JavaScript Redirect Timer

<p class="lead">If this page does not redirect automatically, please <a href="/congratulations">click here</a>.</p> <p class="lead">Redirecting in <span id="DispSec">5</span> seconds.</p> <script type="text/javascript"> TimerSeconds = 5; var TimerVar = setInterval(startTimer, 1000); function startTimer() { if (TimerSeconds < 1) { clearInterval(TimerVar); location.href = "congratulations"; } document.getElementById("DispSec").innerHTML = TimerSeconds; TimerSeconds--; } </script>