Through this exercise, I realised that I can reverse a string using a [::-1] operator. Really cryptic, but yet handy.
1234567891011121314151617181920212223242526
# A palindromic number reads the same both ways. The largest palindrome# made from the product of two 2-digit numbers is 9009 = 91 x 99.# # Find the largest palindrome made from the product of two 3-digit numbers.defisPalindromic(Num):# For string reversal, pls check this url:## http://www.python.org/doc/2.3.5/whatsnew/section-slices.htmlN=str(Num)l=len(N)div,mod=divmod(l,2)ifmod==1:# oddfh=N[0:l/2]sh=N[1+l/2:]# print “%s %s” % (fh, sh)else:fh=N[0:l/2]sh=N[l/2:]returnfh==sh[::-1]if__name__=="__main__":g=[x*yforxinxrange(100,999)foryinxrange(100,999)]printmax(filter(lambdax:isPalindromic(x),g))
Here comes an erlang version. In this rare occasion, erlang runs faster than python. It is probably because, first of all, the list comprehension I used in erlang produces a smaller list. Secondly the test of palindromic is more straightforward in the erlang version.
I will revisit the python version, later, when I get a bit more time.