- Back to Home »
- Analytics , Interview Question , Python »
- MxxN = M^M^M^....M for N times - Interview Question (DP Analytics) [Solved in Python]
Posted by : Netbloggy
Tuesday, October 13, 2015
DP Analytics, a well-known analytics company came up with an interestingly logical written interview test.
Question:
2xx2 = 4
2xx3 = 16
2xx4 = 65536 = 2^16
3xx2 = 27
3xx3 = 7625597484987 = 3^27
4x3 = 64
2xxx3 = 65536
2xxx3 = 2xxx4
In general,
MxxN = M^M^M^....^M for N times
MxxxN = MxxMxxMxx.....xxM (N times)
and so on
Let the symbol 'x' be represented by a computer function 'T'. The inputs to
'T' function are(left number, number of x, right number). so,
T(2,2,2)=4
T(2,2,3)=16
T(2,4,3)=T(2,3,4)
Solution:
def T(m,t,n): if t == 1: result = m ** n return result n = n + (t-2) result = m while(n>1): result = m ** result n = n-1 return result print T(3,2,3) print T(2,2,3) print T(4,1,3)Understanding: For t=2, N-1 is the number of times M must be raised to the power of itself (eg: when N is 2, then M ^ M and when N is 3, then M ^ M ^ M) We built a logic that is only valid for xx hence if the number of x is greater than two then it is brought down to 2 and N is incremented by the same count, and when x is one, M ^ N is the actual result. Happy Coding!