Amazon

Top Trending Books

Wednesday, November 19, 2014

Python: Find out dates from Last Week.

Following code can be used to findout the Dates of Last week starting from monday
 import time  
 from datetime import date, timedelta  
 def foo(year, week):  
   d = date(year,1,1)  
   d = d - timedelta(d.weekday())  
   dlt = timedelta(days = (week)*7)  
   return d + dlt, d + dlt + timedelta(days=6)  
 todaysDate=time.localtime()  
 weekNumber=time.strftime("%W",todaysDate)  
 yearInTest=time.strftime("%Y",todaysDate)  
 print "Current Week Number is :::" + weekNumber  
 print "Current Year is :::" + yearInTest  
 lastWeeknumber=int(weekNumber)-1  
 print "Last Week Number is :::" + str(lastWeeknumber)  
 yearInTest=int(yearInTest)  
 dateInTest=foo(yearInTest,lastWeeknumber)  
 print str(dateInTest)  

Wednesday, November 5, 2014

Python: Python Challenge - Level 1 : Proposed Solution

Second problem on "pythonchallenge.com" is an image which displays the offset of alphabets we should apply in the given string below the image. To solve this, I tried the following Solution:

 uRLStringConstant = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."  
 listSet=[]  
 for i in uRLStringConstant:  
      convertedValue = ord(i) +2  
      if convertedValue > 122:  
           convertedValue = 97  
      if convertedValue == 34:  
           convertedValue = 32  
      if convertedValue == 48:  
           convertedValue = 46  
      convertedCharater = chr(convertedValue)  
      listSet.append(convertedCharater)  
 print '[%s]' % ''.join(map(str, listSet))  

Output was:
 :::::OUTPUT::::::  
 Using 'map', we convert it to 'ocr'  
 Hence the URL is http://www.pythonchallenge.com/pc/def/ocr.html