Tuesday 25 June 2013

Five Friends in a Foreign Restaurant Puzzle


Five friends in Timbuktu go to a local restaurant. They do not recognise any of the dishes on the Menu as they are not familiar with such food but they are excited to try out. Each one of them orders one dish, not necessarily distinct. The waiter brings the dishes and places them in the middle, without saying which is which. At this point, they may be able to tell the names of some of the dishes based on logical reasoning. For example, if two people ordered the same dish, and everyone else ordered different dishes, then the item for which two portions arrived can be told deterministically. They enjoyed the food a lot and were eager to try more of it next day.

They return to the restaurant two more times and followed the same experiment, though with a new set of orders. After three meals, they have eaten all nine dishes on the menu, and can tell which is which.

What pattern of ordering did they follow to fulfil this?

Friday 21 June 2013

Applying State Design Pattern to Code an FSM


A finite-state machine (FSM) or finite-state automaton (plural: automata), or simply a state machine, is a mathematical model of computation used to design both computer programs and sequential logic circuits. The behavior of state machines can be observed in many devices in modern society which perform a predetermined sequence of actions depending on a sequence of events with which they are presented. 

A programmer often needs to model states of classes implementing such real life behavior. States of  a class can be implemented using some class variables denoting the a particular state the class is in. While handling the events the class is sensitive to, a check can be done to find the current state and appropriate action can be taken. For example 'if(state ==ON) do this else do that'. But this approach gets complicated if the number of states or events to which class has to react increase.

Friday 14 June 2013

Expressing Numbers with Number Puzzle


We are given four integers 1, 2, 3 and 4 to express other numbers with an arithmetic expression using these numbers exactly once and the mathematical operators +,-,* (addition, subtraction, multiplication).  For example 1 can be represented as 1 = (2*3)-(4+1). The arithmetic operation can be used zero or multiple times but the digits(1, 2, 3, 4) should be used exactly once. So an expression to represent 1 = 2+3-4 is not valid. Where as 10 = 1+2+3+4 is a valid expression to represent 10. Also you are allowed to reuse operators (such as 24 = 1*2*3*4) and you are allowed to put '(', ')' at any desired position to change the precedence of evaluation.  But you are not allowed to join digits together such as (12+34).


Monday 10 June 2013

C++ Exception, Error Management & Debugging


All programs have bugs. The bigger the program, the more bugs, and many of those bugs actually "get out the door" and into final released software. The biggest expense in many major programming efforts is testing and fixing.The least expensive problems or bugs to fix are the ones you manage to avoid creating. Following coding guidelines is the easiest way to avoid bugs. Code rot is a phenomenon when code deteriorates due to being neglected and a perfectly well-written, fully tested code develops new and bizarre behavior six months after it is released. 

Wednesday 5 June 2013

Sequence of Equations Puzzle


This is a simple yet tricky puzzle. It seems this puzzle has been solved by pre schoolers in an average 10 Mins. By entry level programmers in around 30 Mins. And strangely people good in mathematics have taken more time to solve this riddle. Check which category you belong to. If you get the answer simply post the value in comment section. Within a day or two I will post the explanation.