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)  

More suggestion are always welcome. In case if you wish me to write about any specific scenario, please feel free to write to me or else you can Comment below.

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  

More suggestion are always welcome. In case if you wish me to write about any specific scenario, please feel free to write to me or else you can Comment below.

Tuesday, November 4, 2014

Python: Python Challenge - Level 0 : Proposed Solution

First problem on "pythonchallenge.com" is an image which displays the number 2^38. To solve this, I tried the following Solution:

 uRLStringConstant = "http://www.pythonchallenge.com/pc/def/"  
 variableComponent = 2**38  
 print '{}{}.html'.format(uRLStringConstant,variableComponent)  

Output was:
 :::::OUTPUT::::::  
 http://www.pythonchallenge.com/pc/def/274877906944.html  
 Redirected To  
 http://www.pythonchallenge.com/pc/def/map.html  

More suggestion are always welcome. In case if you wish me to write about any specific scenario, please feel free to write to me or else you can Comment below.

Friday, October 31, 2014

Python: Check if the string is "Palindrome"

I am trying to demonstrate one or two methods (among many to achieve the stated problem).

First let us try to understand what is Palindrome:

A palindrome is a word, phrase, number, or other sequence of symbols or elements that reads the same forward or reversed, with general allowances for adjustments to punctuation and word dividers.

First Method:
 FirstString="ABCDEFFEDCBA"  
 Reversed=(FirstString [::-1])  
 if list(FirstString) == list(Reversed):  
   print("It is Palindrome")  
 else:  
   print("It is not Palindrome")  


Second Method:
 SecondString="1234ABCCBA4321"  
 def palindrome(num):  
   return num == num[::-1]  
 if palindrome(SecondString) == True:  
   print("It is palindrome")  
 else:  
   print("It is not palindrome")  

You can try to run this code on Codebunk. You can refer to my Codebunk. These are simple code which can be used in complex algorithm.

More suggestion are always welcome. In case if you wish me to write about any specific scenario, please feel free to write to me or else you can Comment below.

Python - Reverse the String

I am trying to demonstrate one or two methods (among many to achieve the stated problem).

First Method:
 FirstString="First String"  
 print (FirstString [::-1])  
Above example is extended slice syntax. It works by doing [begin:end:step] - by leaving begin and end off and specifying a step of -1, it reverses a string.

Second Method:
 SecondString="Second String"  
 def reverse(text):  
   if len(text) <= 1:  
     return text  
   return reverse(text[1:]) + text[0]  
 print(reverse(SecondString))  
You can try to run this code on Codebunk. You can refer to my Codebunk. These are simple code which can be used in complex algorithm.

I am glad to add below video in my blog created by Webucator. This video explains the performance of two methods demonstrated above.


For more details on python training visit, Webucator-Python

More suggestion are always welcome. In case if you wish me to write about any specific scenario, please feel free to write to me or else you can Comment below.