Python symmetric words

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jane Janey
    New Member
    • Apr 2013
    • 4

    Python symmetric words

    How do I write code to find words that are in the same specified position in a string. The position should be like this:

    Code:
    word1= ('a','b','c','d','e','f','g','h','i','j','k','l','m')
    word2= ('z','y','x','w','v','u','t','s','r','q','p','o','n')
    
    word1[n] == word2[n]
    An example:

    Code:
    text = "A boy is being bully by Aloz"
    And it will return the result:

    Code:
    ['a','boy','aloz']
    This is because 'a' has no pair therefore it is counted. As for 'boy', 'b' is on the same position of 'y' and 'o' has no pair while for 'aloz', 'a' is in the same position with 'z' and 'l' is in the same position as 'o'.

    I will really appreciate anyone who can help me with this.
    Last edited by bvdet; Apr 19 '13, 03:19 PM. Reason: Change thread title
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Maybe it's just me, but I do not understand the rules.

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      I don't understand your example, but I think it would be something like this. Note that the "y" in boy is the third letter while the "y" in word2 is the second letter.
      This is because 'a' has no pair therefore it is counted.
      "A" is also in "Aloz" so I suppose that is not what you mean by "pair".
      Code:
      word1= ('a','b','c','d','e','f','g','h','i','j','k','l','m')
      word2= ('z','y','x','w','v','u','t','s','r','q','p','o','n')
      text = "A boy is being bully by Aloz"
      text_split = text.split()
      
      if len(word1) == len(text_split):
          for ctr in range(len(text_split)):
              if word1[ctr].lower() == text_split[ctr][0].lower():
                  print text_split[ctr],
          print
      else:
          print "The two strings are not of equal length"

      Comment

      • Jane Janey
        New Member
        • Apr 2013
        • 4

        #4
        python symmetric words

        I have to write a function which takes one arguments text containing a block of text in the form of a str, and returns a sorted list of “symmetric” words. A symmetric word is defined as a word where for all values i, the letter i positions from the start of the word and the letter i positions from the end of the word are equi-distant from the respective ends of the alphabet. For example, bevy is a symmetric word as: b (1 position from the start of the word) is the second letter of the alphabet and y (1 position from the end of the word) is the second-last letter of the alphabet; and e (2 positions from the start of the word) is the fifth letter of the alphabet and v (2 positions from the end of the word) is the fifth-last letter of the alphabet.

        For example:

        Code:
        >>> symmetrics("boy great bevy bully")
        ['bevy','boy']
        >>> symmetrics("There is a car and a book;")
        ['a']
        All I can think about the solution is this but I can't run it since it's wrong:

        Code:
        def symmetrics(text):
            punc_char= ",.?!:'\/"
            for letter in text:
                if letter in punc_char:
                  text = text.replace(letter, ' ') 
            alpha1 = 'abcdefghijklmnopqrstuvwxyz'
            alpha2 = 'zyxwvutsrqponmlkjihgfedcba'
            sym = []
            for word in text.lower().split():
                n = range(0,len(word))
                if word[n] == word[len(word)-1-n]:
                    sym.append(word)
                return sym
        The code above doesn't take into account the position of alpha1 and alpha2 as I don't know how to put it. Is there anyone know how to do this?

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Jane Janey,

          The use of str method index in combination with extended slicing will help a great deal.
          Code:
          >>> alpha = 'abcdefghijklmnopqrstuvwxyz'
          >>> alpha[::-1]
          'zyxwvutsrqponmlkjihgfedcba'
          >>> alpha.index("z")
          25
          >>> word = "bevy"
          >>> for i in range(int(len(word)/2)):
          ... 	if alpha.index(word[i]) != alpha[::-1].index(word[-i-1]):
          ... 		print "not symmetrical"
          ... 		
          >>> word = "symmetric"
          >>> for i in range(int(len(word)/2)):
          ... 	if alpha.index(word[i]) != alpha[::-1].index(word[-i-1]):
          ... 		print "not symmetrical"
          ... 		
          not symmetrical
          not symmetrical
          not symmetrical
          not symmetrical
          >>>
          HTH

          Comment

          • dwblas
            Recognized Expert Contributor
            • May 2008
            • 626

            #6
            For starters, the "return sym" statement returns/exits after the first word. Instead, send each word to the function
            Code:
            def symmetrics(word):
                """ this function goes through all letters in the word, while
                    you only have to compare the first half to the last half,
                    so that adjustment is left up to you
                """
            
                ## remove punctuation
                ## if you don't understand list comprension 
                ## then just use a for() loop
                word_2 = [ltr for ltr in word.lower() if "a" <= ltr <= "z"]
                print "".join(word_2), 
            
                alpha1 = 'abcdefghijklmnopqrstuvwxyz'
                alpha2 = alpha1[::-1]
            
                ## front and back locations to compare
                front = 0
                back = len(word_2) - 1
                while back > -1:  ## goes all the way through the word
                    if front != back:  ## center letter is not compared is it???
                        front_ltr = word_2[front]
                        back_ltr = word_2[back]
                        front_location = alpha1.find(front_ltr)
                        back_location = alpha2.find(back_ltr)
                        if front_location != back_location:  ## any mismatch exits
                            return False
                    front += 1
                    back -= 1
            
                return True  ## everything matched
            
            for word in ["bevy", "boy", "Aloz", "not", "knot's&", '"guess"']:
                print word, symmetrics(word)

            Comment

            Working...