/var/log/mandarg

Mandar Gokhale's weblog on the internets

Comparisons Between Operands of Different Types

Recently, a colleague of mine tracked down a bug which was, in essence, caused by the fact that one side of a comparison in Python was a string, and the other was an integer, which led to behaviour that was sometimes expected and sometimes not.

1
2
3
4
>>> '45' > 43
True
>>> '41' > 43
True

This is one of the more unintuitive parts for Python for me. Intuitively, I would expect some sort of a TypeError if I am trying to compare things that are not of the same type. But, apparently, in Python, when you compare two items of distinct types, Python compares the types themselves.

Objects of different types, except different numeric types and different string types, never compare equal; such objects are ordered consistently but arbitrarily (so that sorting a heterogeneous array yields a consistent result). Furthermore, some types (for example, file objects) support only a degenerate notion of comparison where any two objects of that type are unequal. Again, such objects are ordered arbitrarily but consistently.

I looked at this in a couple of other languages. Javascript seems to do implicit type conversion,

1
2
3
4
> '45' > 43
true
> '41' > 43
false

…whereas Ruby seems to behave fairly sanely, throwing an error – just like I expect. Perhaps it’s time to start using Ruby a bit more for my small scripts (though it often seems to lead to more time spent yak shaving than Python in the average case).

1
2
3
4
5
irb(main):001:0> '45' > 43
ArgumentError: comparison of String with 43 failed
    from (irb):1:in `>'
    from (irb):1
    from /usr/bin/irb:12:in `<main>'

❧ Suggestions, comments, etc. can be emailed to comments@mandarg.com

Adding Missed Calls to Contacts on Android

I became the proud owner of an iMac-esque blue rubber-and-plastic Nokia 3100 in twelfth standard / grade. At the time, the standard method of exchanging contacts between person A and B went like this:

A: Hey, give me a missed call. My number is 99000 12345

B: (Gives A a call, and thus knows A’s number)

A: (Looks at most recent calls, and saves B’s number)

B: (Looks at most recent calls, and saves A’s number)

On a standard Nokia phone, saving a recent missed call took exactly four keypresses, and was fairly intuitive.

Recently, I was using my Android phone to take down a friend’s contact number, and we tried doing this. I was initially unable to make any headway in saving the contact from my most recent missed call. After we figured it out, it turns out that the “correct” solution in this case (for Android Lollipop anyway) is to:

  • Click on the number that called you
  • Tap the word “Details”
  • On the resulting “Call details” screen, tap the human figure that appears beside the number (but don’t click on the number itself)

  • On the resulting pop-up, click the little human figure with a + next to it, which bring up your standard “Add to Contact” screen, where you will then “Create new contact”.

Compared to the Nokia workflow, I personally think this is very kludgy. I guess people don’t necessarily exchange their contacts over the phone now in person, rather than pulling them directly from Google+ or Facebook.

❧ Suggestions, comments, etc. can be emailed to comments@mandarg.com.

Consumption and Convenience

Roasted seaweed is a snack that I first ate in the US. I like its umami taste a lot; and apparently it is quite rich in minerals too. Never one to indulge in moderation, I picked up a 10-pack at Costco recently.

When opening one of the small packs, I noticed that if cut the “normal” way (i.e., parallel to the crimp with the serrations) the plastic tray that slides out has the sheets oriented in such a way that you have to pull the whole tray out to eat a single sheet.

At first I though this was a simple design flaw. But thinking about it further, it probably makes sense from the point of view of the company. Seaweed being a highly addictive snack, if I pull the entire tray out, I have a higher probability of finishing the whole thing. If I could pull only a third of it out, and eat only that, saving the rest for later – Kirkland would probably sell me fewer packs. I tried to get around this by turning it sideways and pushing it back in, but the dimensions don’t make this feasible. I could always cut the packaging from the side, but that would just be wrong, and probably messier.

Anyway, this goes to show that user convenience and the seller’s incentive to get you to consume more can be completely at odds with each other. I guess this is something to keep in mind while analysing user interfaces, specially those that include things such as in-app purchases.

❧ Suggestions, comments, etc. can be emailed to comments@mandarg.com.

Email Yourself a New Link Every Day

I often gather tons of links which I propose to read later, and never end up doing so. So I decided to write myself a simple utility that I could run on my server, to email me one of these.

Get a new link every day from a list of links
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#! /bin/bash

COUNTER_FILE='/tmp/.number'

if [[ -r $COUNTER_FILE ]] ; then
    count=$(cat ${COUNTER_FILE})
else
    count=1
fi

MESSAGE=$(sed -n ${count}p $1)

echo $MESSAGE
count=$((count+1))

echo $count > ${COUNTER_FILE}

Now, all I have to do is to add the following to my crontab…

Get a new link every day from a list of links
1
0 14 * * * /home/user/scripts/newlink /home/user/text/links.txt | mail -s "Link for $(date +'\%d \%B \%Y')" links@mandarg.com

…and I have a fresh link every morning. Linkbait for the win.

Note: The cronjob didn’t initially work as I had expected, because % signs indicate new lines in a crontab, and have to be escaped. Good to know.

❧ Suggestions, comments, etc. can be emailed to comments@mandarg.com.

How Many Times *did* Sherlock Holmes Say ‘Elementary’?

It is now common knowledge (well, if it is on Snopes, it must be common knowledge, right?) that Sherlock Holmes never said “Elementary, My Dear Watson”. But anyone who has read the books has probably noticed the word flit in and out of his speech.

So, in the canon text, how many times did Holmes say just the word “Elementary?”

The first published Holmes story

This site has a pretty good selection of formats in which to download the canon works, thus making this a pretty easy question to answer.

1
2
3
4
5
6
7
8
9
$ grep -ne '[Ee]lementary' cano.txt
712:     mastering more elementary problems. Let him, on meeting a
12309:     though rather elementary, but I must go back to business, Watson.
27150:     "Elementary," said he.  "It is one of those instances where the
44895:     "Interesting, though elementary," said he as he returned to his
46102:     your Esquimau. The detection of types is one of the most elementary
59667:     tin box, and an elementary book on botany, there are instructive days
64083:     with a mischievous twinkle. "It belongs to the same elementary class
68387:     the root of the matter. Still, elementary as it was, there were

Only eight instances in total, which is rather fewer than I expected.

  • A Study in Scarlet:

    Before turning to those moral and mental aspects of the matter which present the greatest
    difficulties, let the enquirer begin by mastering more elementary problems.

  • A Case of Identity:

    All this is amusing, though rather elementary, but I must go back to business, Watson.

  • The Crooked Man: (This is probably the closest Holmes comes to saying the misattributed phrase.)

    As I perceive that your boots, although used, are by no means dirty, I cannot doubt that you are at present busy enough to justify the hansom.

    “Excellent!” I cried.

    “Elementary,” said he.

  • The Hound of the Baskervilles:

    “Interesting, though elementary,” said he as he returned to his favourite corner of the settee.

  • The Hound of the Baskervilles again (and probably quotable in a Haskell book.):

    The detection of types is one of the most elementary branches of knowledge to the special expert in crime, though I confess that once when I was very young I confused the Leeds Mercury with the Western Morning News.

  • The Adventure of Wisteria Lodge

    With a spud, a tin box, and an elementary book on botany, there are instructive days to be spent.

  • The Disappearance of Lady Frances Carfax

    “It belongs to the same elementary class of deduction which I should illustrate if I were to ask you who shared your cab in your drive this morning.”

  • The Blanched Soldier (only one of two stories narrated by Holmes, rather than Watson)

    Still, elementary as it was, there were points of interest and novelty about it which may excuse my placing it upon record.

❧ Suggestions, comments, etc. can be emailed to comments@mandarg.com.

A Nice Approximation for How Big an N-bit Integer Is

The other day, a couple of co-workers and I were talking about whether it was reasonable to expect a user to know the maximum number represented by a 32-bit integer (2147483647 if it’s signed, 4294967295 if not).

A lot of engineers and technical-oriented people tend to know roughly “2 billion” and “4 billion”. But I was in the camp tending towards “No, we should be more explicit for end users”. So, as an experiment, I asked people if they knew the nearest power of 10 for the largest 64-bit integer off the top of their heads.

Discussion about estimating this for a general N-bit integer ensued, and Jim mentioned a pretty decent trick. The logarithm of 10 to the base 2 is a bit less than 3 1/3, so that’s not a bad approximation. So to get the nearest power of 10 for 264 , divide 64 by 10/3 , i.e multiply by 3 and shift the decimal point to the left. So, the nearest power of 10 for the largest 64-bit (unsigned) integer is ⌊(64 * 3) / 10⌋ = 19. The correct answer is 1.8446744e+19, so we are pretty close.

❧ Suggestions, comments, etc. can be emailed to comments@mandarg.com.