lolamontes69

Python/ Cue Programming 1

May 19th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. def first(word):
  2.     """Returns the first character of a string."""
  3.     return word[0]
  4.  
  5. def last(word):
  6.     """Returns all but the first character of a string."""
  7.     return word[-1]
  8.  
  9. def middle(word):
  10.     """Returns all but the first and last characters of a string."""
  11.     return word[1:-1]
  12.  
  13. def is_palindrome(word):
  14.     """Returns True if word is a palindrome."""
  15.     if len(word) <= 1:
  16.         return True
  17.     if first(word) != last(word):
  18.         return False
  19.     return is_palindrome(middle(word))
  20.  
  21. def main(text):
  22.     longest = ""
  23.     highscore = 0
  24.     list1 = []
  25.     a = len(text)
  26.     for b in range(a):
  27.         for c in range(a-1):
  28.             word = str(text[c:c+b])
  29.             d = is_palindrome(word)
  30.             if d == True:
  31.                 list1.append(word)
  32.                 if len(word) > highscore:
  33.                     longest = word
  34.                     highscore = len(word)
  35.     return longest
  36.  
  37. if __name__ == "__main__":
  38.  
  39.     text = "FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"
  40.    
  41.     longest = main(text)  
  42.     print "The First Password is:",longest
Advertisement
Add Comment
Please, Sign In to add comment