30 May, 2008 | Written by Vincent Tan Leave a Comment

You have 10 seconds

I was walking along the Orchard Road (a busy “consumercial” street) in Singapore. There was this guy who approached me with maybe 2 pieces of paper and a blue plastic envelope/bag in his hands. He looked a bit flustered, and started talking to me and pointing to the top piece of paper.

I heard words such as “help”, “ex-convict” and “get jobs”. I saw the word “ex-convict” on the piece of paper he’s pointing to. The foremost thought on my mind was, “what does he want me to do?“. Do I need to sign a petition? Do I donate some money? Do I have to give out contact information?

After, I don’t know, 30, maybe 40 seconds, he finally got to the point. “Please donate.” And that’s what the plastic envelope was for. I’m not sure if you’ve seen this. It’s a flat carrier, with a small slit near the handle on top, large enough for coins to be slotted. You can even slot dollar notes if you fold them a little.

I donated some money and I was off. I am aware of this movement; I read some poems written by inmates on those service tray paper in Coffee Bean. It’s the Yellow Ribbon movement in Singapore.*

There was another incident where I was having lunch with my colleagues, and this young lady approached us and started her conversation with the words “I’m not here to sell you anything”. Loosely speaking, she was selling us something. I’ll get to that in a moment.

I can’t even remember what she was representing. She pointed to her badge hung around her neck, said something to the effect that she’s authentic and real and part of an authorised movement. She started talking about her cause. For, I don’t know, 30, maybe 40 seconds.

Then she got to the point. “Please donate”.

I have every respect for your cause. I empathise with the people you want to help. I also treasure my time very much. So please tell me exactly what you want me to do, and then tell me why I should do it.

Get to the point

Let’s look at this in a simple situation. A tourist asking for directions. “Hi there, I’m kinda lost. Can you tell me how to get to such-and-such building? I’ve been walking up and down this street for a long time.”

Short, simple and under 10 seconds. From your look, your mannerisms, your dressing, your behaviour and then with your question in context, I have already made up my mind about what I want to do with you. Say a stranger walked up to me, and from appearances I could guess he’s a tourist. Facial expressions and body movements suggested tiredness and slight frustration. I could probably tell if he’s lying, if his words matched his outward appearance.

Frankly speaking, when the stranger walked up to me, I probably already decided that he’s lost and he’s going to ask me for directions. And I’ve already decided to help him if I could. Verbalising the question was just a formality.

What happened for the 2 people above was that, when they approached me, I’ve already made many unconscious decisions and arrived at an answer. They were asking for donations, and maybe spread awareness of some kind. They were asking for help.

I’ve already decided they’re probably asking for donations, and I was waiting for them to confirm that. I could be wrong. So I waited for them to tell me if they wanted a donation, and if it was, what was I donating to.

I waited and waited. I didn’t interrupt them since it would be rude. And waited for 40 seconds. I was deciding if I wanted to donate, and what else they wanted with me. And that thought occupied my entire thinking process while they were trying to educate me about whatever it was they stood for.

Make it simple and painless

Look, it’s hard to approach someone as a stranger, and start trying to educate that person about something within minutes. That person is already busy trying to figure out what you want with him. You’re making it harder by introducing a concept that might possibly be unfamiliar to the person.

Now, this might sound a little odd. Anytime you ask for something, you’re actually selling something. It’s just that sometimes, your price isn’t monetary. In fact, if you’re reading this far, I’ve already sold you something. I sold you information, and it’s free. In monetary price anyway. The price was actually your time, your attention. And it’s very valuable to you and to me.

For donations, the product being sold is the feeling of helping someone or a cause.** It might sound materialistic and cynical. It took me a while to wrap my brain around this. If it helps take the edge off, you can think of the product sold as just helping someone or a cause.

Donations obviously have a monetary price tag on it. Is there a need for people to pay with time and attention too? There are empathic people. There are also empathic people with busy schedules and limited time. You can spread awareness about your cause in another fashion. The street is not the most conducive environment to do it.

There’s something I read, written by David Eddings. It’s fiction, but I think it has real life roots. Eddings wrote about this beggar. The beggar was obviously asking for donations. The “trick” was to get the message across, and make it as painless for the donor to donate as possible. A missing leg, dressed in rags, covered in dirt, and crying out “Charity!” in the feeblest, most pathetic voice while sticking out a donation bowl worked wonders.

The wordless exchange

There’s another incident where I was having my meal at a diner. It was relatively empty, only another lady and me. A person walked into the diner and laid something on that lady’s table. “I’ve seen this before.” was my first impression. And that person came over to me and laid something on my table too. It’s a card and a key chain, shaped like a key. The card read something to the effect of “I’m dumb and please buy this key chain from me.”.

When that person walked in and placed something on that lady’s table, I already came up with an image. A dumb (as in unable to speak) person asking people to buy something, almost like a donation. That person placed his card and key chain on my table, and left. His body movements, and my previous memories of similar encounters, and the card told me everything I needed to know. And I knew what he wanted, to buy his key chain (in this case).

He didn’t stay to explain his situation. He didn’t have to explain his cause. He couldn’t tell me even if he wanted to. Yet within seconds, I knew what he wanted me to do.

He returned after a while, and came to retrieve the cards and the key chains. If the person wanted to buy, great. If not, he’d just collect his card and key chain.

He came to me. I bought his key chain. I took out the required amount and handed the money over to him. He took out a set of key chains and gestured. Oh, I get to choose the colour, and chose gold. He took out a new gold key chain, and handed it to me. Then he took out another card and showed it to me. Oh, it acts as a bottle opener too.

Key chain shaped like key

I smiled at him. He smiled back, and then left. I didn’t feel stupid at all, despite some fumbles over communication, despite the fact that no words were ever exchanged. I felt good.

Impassive interface

Human face-to-face interactions have the benefit of facial expressions and body movement to add context. With computer programs, your users miss out on this benefit. Suppose the user already know what the interface is for, for example, an online sales order form. Is the interface intuitive enough that the user knows what he needs to do next?

I’m not talking about people who have the attention span of a gnat, who clicks away within 2 seconds if the web page doesn’t load fast enough, or the initial look isn’t interesting enough. I’m talking about people who already know what they need to do, and why. I’m talking about people on that web page, or program interface, and they are confused about what they need to do to make what they want happen.

Make the steps they need to do obvious. Make the text fields and required input format obvious. Have appropriate text for action buttons. Some of these people could be busy on the phone, attending to customers. The last thing they need is a confusing interface. Make it obvious to them what they need to do.

Because on the screen, you have 10 seconds to tell them what to do.

* I went to the web site, and it turns out there’s a pop-up warning about donation soliciting on streets. Maybe I was conned?

** There’s this idea that when you buy, you’re actually buying a feeling. That car you bought? You’re buying the feeling of freedom, of convenience, of status. That dinner you bought? You’re buying the feeling of staving off hunger, of being satiated. Just a different perspective.

28 May, 2008 | Written by Vincent Tan Leave a Comment

Encryption, bijection and reversibility

The whole point of this article is to tell you that you need to write reversible programs, or programs with results that are easy to undo. There, you’ve got the point. That’s it.

You still here? Then get ready for a bucket-load of concepts below.

The swap

How do you change DAN from a him to a HER? Add 4. Suppose you have the entire alphabet on one row and another set shifted by 4 placings:

ABCDEFGHIJKLMNOPQRSTUVWXYZ
EFGHIJKLMNOPQRSTUVWXYZABCD

Can you see the correspondence? We can even undo the sex change, reversing HER back to DAN.

This is one of the simplest forms of encryption. It’s also easily broken with the use of computers (and programming of course). Still it illustrates the concept of changing something that’s RIGHT to something unintelligible like VMKLX.

The whole point of encryption is to scramble a plain text message into something obscure so no one except the intended can read it. There’s no point in having an obscure message without being able to get the plain text message back, is there? So encryption has to be reversible.

Now we’re going to do math.

Please, no trees

Let ALPHA be the set of all 26 English alphabets in upper case. Suppose a function F behaves such that if F(x) equals F(y), then x must be equal to y, where x and y belong to the set ALPHA. Then the function F is said to be injective.

Using the encryption model above, where our function F swaps the letters from the top row to the letters in the bottom row, let’s have the final letter be D. There’s only one letter that can map to D, and that’s Z. F is injective.

It might be a bit hard for you to visualise that. Let’s use the contrapositive version of the statement. If x not equal to y, then F(x) not equal to F(y). So if P not equal to Q (which is true), then F(P) = T which is not equal to U = F(Q).

Injectivity is a one-to-one relation. No tree-like structure, no linking of nodes, no multiple results from the same starting point, no multiple starting points ending in the same result.

Must start somewhere

Let ALPHA be the set of all 26 English alphabets in upper case. Suppose a function F behaves such that for every letter y in ALPHA, there’s another letter x in ALPHA such that F(x) = y, then F is said to be surjective.

Using the encryption model above, every letter in the bottom row has a corresponding letter in the upper row. So our function F that swaps the top letter to the bottom letter is surjective.

Surjectivity is a there-exists-a-starting-element concept. On a philosophical note, your life journey can be likened to surjectivity; you started somewhere didn’t you?

Two way street

A function F is said to be bijective if it is both injective and surjective. It is one-to-one, and for every destination, there’s a starting point. So you can go back and forth in a unique manner without fear of getting lost.

Bijections allow reversibility. Encryptions are bijections. Thus encryptions are reversible. Actually, I just thought I’d introduce you to the concept of encryption and bijection. Their relation to reversibility is hand-wavily tenuous… But I’m forging on anyway, so…

Can you write a bijective program?

I would have to say no, in general. There’s usually more than one way to do something, and that violates the injective rule. But if you conform to a stricter set of rules, you can approach the situation where there are fewer ways of writing code to do whatever you need done. So you get an almost injective state. Then you get a reversible program under strict rules.

What’s the whole point of this? A program whose results can be easily reversed is useful. Suppose the result isn’t satisfactory. Do you need to do a lot to undo that damage? A computer virus is (generally speaking) not useful. It’s effects are not easily reversible. In fact, its intent is to be as irreversible as possible.

A reversible program, a program that you can rerun with minimal fuss and effort. That’s useful. Why would reversibility be useful?

Because it allows humans to make mistakes.

And we will make mistakes, make no mistake about that *smile*. When users make a mistake, how have you written your program so it’s easy to recover from it?

26 May, 2008 | Written by Vincent Tan Leave a Comment

Only quarters counted

Last week, at the end of the article, I presented a problem on floating point values:

Given the range of non-negative 2-decimal-point values less than 1, what are the values that can be represented exactly?

I was referring to dollars and cents, monetary values. As of this writing, there’s no answer submitted. I will assume that you’re very modest, and so will provide my version of the answer. First, we dissect the question. “non-negative” means cannot be negative, so zero and positive numbers only. “less than 1″ means, well less than 1. And the values are restricted to numbers with up to 2 decimal points of accuracy.

So this means the set of values 0.00, 0.01, 0.02 … 0.97, 0.98, and 0.99 (1.00 not counted).

Before writing this article, I already an answer, intuited without actual calculations. Then I went to do some research of mine to verify that answer. I was mortified that my understanding of the IEEE floating point standard was wrong. I dug up my old university programming textbook, where there’s a section on internal floating point representation, and confirmed my misunderstanding.

My error was in how the exponent and mantissa were represented. In the end, I realised I had to convert all the 100 values (0.00 to 0.99) into binary representation to check. I didn’t relish doing long division by hand for a possible indefinite number of iterations (up to 32 anyway), and for 100 values. So I took the easier way out. I wrote a program. *smile*

decimal decBinary = 0.5m;
decimal decBuffer = 0;
int i, j;
for (i = 0; i < 100; ++i)
{
    decBuffer = i * 0.01m;
    Console.Write("{0} ", decBuffer.ToString("f2"));
    decBinary = 0.5m;
    for (j = 0; j < 32; ++j)
    {
        if (decBuffer < decBinary)
        {
            Console.Write("0");
        }
        else
        {
            Console.Write("1");
            decBuffer -= decBinary;
        }
        decBinary /= 2;
    }

    if (decBuffer != 0)
    {
        Console.WriteLine(" non-terminating");
    }
    else
    {
        Console.WriteLine(" terminated");
    }
}

It’s in C# and there’s not much comments. But you should still be able to follow much of the code, even if you aren’t familiar in C#. This is assuming a 32-bit is used to represent a float. I ran the long division iteration up to 32 times, even though in a real representation, only 23 bits are used. Here’s a Java applet IEEE binary converter you can play with too.

What happens is that, if after 32 bits are assigned (a 0 or 1), if more bits need to be assigned (not terminated), then the floating point representation isn’t exact (since there’s extra information truncated). You will then note that only 0.00, 0.25, 0.50 and 0.75 have terminating binary representations. This means, only those 4 values can be represented in a float exactly.

So, what was my original intuitive answer? Let’s look at only 4 binary places in the mantissa. So 1010 represents 1*2^(-1) + 0*2^(-2) + 1*2^(-3) + 0*2^(-4), which is 0.5 + 0.125 = 0.625

Look at the value with only the 3rd mantissa digit as 1, 0010. It represents 0.125 in value. How do you “get rid” of the 0.005 to get 0.12 or 0.13? You can’t, not from switching on bits in any of the mantissa digits. So what does this mean? The moment the 3rd mantissa digit is a 1, the floating point representation can never represent any of the 100 2-decimal-point values exactly.

In fact, the moment any mantissa digit that’s the 3rd or after (4th, 5th and so on) is a 1, that representation is inexact already. So only the 1st and 2nd mantissa digits are “allowed” to be 1. So 0000 is 0.00, 0100 is 0.25, 1000 is 0.5, and 1100 is 0.75.

Same answer as from my laborious calculations. Out of the 100 values, only combinations of quarters or 25 cents have an exact floating point representation.

I hope this has been an interesting thought process for you. Has this changed the way you think about storing monetary values?

26 May, 2008 | Written by Vincent Tan Leave a Comment

Seriously? I got in?

I wasn’t sure. I was nervous. I was also hungry. I pored over the email I typed out. Was it too brief? Should I take out some words? What am I talking about?

Request for inclusion in Alltop, specifically the Programming category.

I looked at the email request I had in front of me, and thought “I’m going to go crazy if I don’t send this soon.” And hit the Send button. And went for lunch.

Alltop. Seriously?! I got in?

I didn’t think about it until a day later (I do have a life you know?). I checked my email inbox and there it was, an email from Guy Kawasaki, the man behind Alltop (together with Will Mayall and Kathryn Henkens). Suffice to say, I got in. You are now looking at a proud member of the Alltop Programming family.

Here are some sites listed in that category that I also read:

Please go check out some of the other categories and sites at Alltop too.

I also noticed that the favicon for the Programming category is a white capital P on black background. Hmm… I seem to have seen that somewhere… oh right, my favicon is like that too. The difference is that mine is flat at the top and bottom of the curves of the P.

Ok, I’m going to go do cartwheels in exhilaration or something. Thanks Guy!

23 May, 2008 | Written by Vincent Tan 2 Comments

Mind Trap - Subterra

Leaving the strange ruins behind him, Ryan descended slowly into the enfolding darkness. The afternoon sun was going to set soon, and without any light source, Ryan decided to go as far down the stairs as he could. Placing his right hand on the wall, he focused his attention on the steps, hand feeling along the uneven wall, careful not to blunder and fall down.

Nearing the steps where he could no longer see very much beyond them, his right hand encountered a protrusion. He looked up and made out what appeared to be a rusty metal ring set in the rough stone wall. Several things clicked in his mind from reading medieval stories and adventures.

“Torch bracket!” he thought.

He felt around the torch bracket and felt the reassuring smoothness of a cylindrical object held loosely in its embrace. Much too cylindrical. It was also plastic instead of wood.

“What the?”

“You idiot! You gave him an electric torch?”
“I’m sorry, I panicked ok?”

Turning the flashlight curiously in his hands, Ryan found the on-off switch. And flicked it on.

“Hey, it works!”

“Why don’t you throw him a laptop too?”
“I said I was sorry, didn’t I?”

Beaming at his unexpected good fortune, Ryan decided to ignore the question of what a modern torch was doing in a place like that. Waving the flashlight to his left, he found … nothing. There was nothing beyond the left side of the stairs. Ryan shuddered from the disastrous possibility he averted by using the right wall to guide his descent.

He looked at the sunlit entrance behind him. Chasing darkness away with his flashlight, he peered at the seemingly unending flight of steps descending in front of him. He gave a sigh.

“Here we go.”

The illumination from the flashlight was weak. Ryan could barely see a few steps in front of him. He didn’t have any spare batteries, and there weren’t any around the torch bracket. Despite the possibility that the flashlight might give out at any moment, Ryan still proceeded carefully. One step, two step, three step…

And then a click sounded in the silence. Ryan froze. His left foot landed on a step that submerged slightly. A whimper escaped his mouth. Heart pounding, yet too afraid to lift his foot, he squeezed his eyes shut and waited in the deathly quiet.

“I thought I told you to take that thing out.”
“It adds excitement, don’t you think?”

After several seconds, a soft crackle was heard from his left. Ryan instinctively hunched down a little. When the expected flying spears or spikes or whatever unpleasant surprise didn’t happen, he risked a peek. A crack appeared in the air, blue light streaming out of it. Then it was gone in a second.

Taking in air in ragged breaths, Ryan quickly pointed the flashlight at his left foot. Still there. He looked up ahead, and saw a faint orange glow. He pointed the flashlight at the steps before him, and made a decision. He took a deep breath and started bounding down the steps, two or three at a time, towards that questionable yet hope-giving orange light.

He reached the bottom of the stairs quickly. The right stair wall joined up with a wall in front of him. Ryan turned to his left and found the source of the orange light, a real fire burning torch in a torch bracket this time. It was at the end of a corridor, perhaps 12, 15 metres long.

He walked slowly towards the torch, not daring to run. Several nerve-wracking moments later, he stood at the end of the corridor. Another corridor branched to the right. As Ryan turned, he noticed some writings carved into the stone below the torch.

His army’s SIZE was PUNY,
And he’s said to be loony.
His troops fell, FORTY to EIGHT.
So tell me, was that his fate?

There’s another seemingly senseless set of carvings beneath that poem.

KYTUPEGINY

Shrugging with confusion, Ryan turned to move along the corridor on his right. Up ahead was what appeared to be a brightly lit large room. Realising that he’s still pointing the flashlight in front of him, Ryan switched it off and placed it in his pocket, since there’s enough light to see.

As he walked along that corridor, there was a noticeable drop in temperature. Ryan shivered. Near the end of the corridor, he saw two bronze statues, armoured with helms, chest plates and wielding in a raised position, the ugliest pain inflicting weapons he had ever seen. It looked like a sword, with two hooks at the end. Ryan wondered what a weapon like that would do to him, then realised it would do as much damage sliding into his body as it would do coming out.

Imagining the statues hammering down on him while he’s passing through, Ryan backed up a little. Then proceeded at a run and made a mad dash through to the room, rolling into a ball and laid on the floor in a foetal position. No sound and movement from the statues. Ryan got up and dusted himself off.

“I hate this place.”

He looked around the room. Three torches burned in the middle of the left, right and front walls of the room. At the corners furthest away from the corridor were two stone statues, a goat on the right corner and a lion on the left. Ryan turned around and saw a dragon statue on the right corner and a tall smooth plaque on the left. There appeared to be writings on the plaque, so he moved towards it to investigate.

There were two rows of numbers at the top:

935
9351246780

Then there’s a drawing of a fire symbol in the middle, and this below it:

RBETTOLCQUCQJVE

Confused again, Ryan looked around the room. Dead end.

“There’s got to be something here,” he consoled himself. “Some brick panel, or contraption or…”

Ryan looked up and grinned. “Or puzzle.”

What should Ryan do?

The answer will be published here in a comment a week after, or when a reader submits an answer. I think I cut off the fun too soon for the last puzzle, so I’m going to wait a few days first, even if a reader answers correctly.

When you submit an answer, please include your reasoning too. Part of the fun of giving the answer is explaining it. Anyone can come up with an answer. Not everyone can fully explain it though. Oh man, I hope I gave sufficient clues…

No torches were harmed in the writing of this puzzle. Flashlights and batteries each sold separately.

[UPDATE: There were no takers for this puzzle. So I answered my own puzzle. View answer.]

Next Page →