2 Replies - 224 Views - Last Post: 18 April 2013 - 03:32 PM Rate Topic: -----

#1 Jane Janey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-April 13

Python function solution

Posted 18 April 2013 - 02:54 PM

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:

>>> 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:

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?

Is This A Good Question/Topic? 0
  • +

Replies To: Python function solution

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Python function solution

Posted 18 April 2013 - 03:26 PM

The following code will at least run:

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(" "):
        for n in range(0,len(word)):
            if word[n] == word[len(word)-1-n]:
                sym.append(word)
    return sym

print(symmetrics("whatever text here"))

As you are aware, it doesn't use alpha1 or 2. Instead of comparing word to word you need to find the position of each character in word within the alpha1 array and, from this number, obtain the corresponding character in alpha2. Then you can check the corresponding character at the other end of the word.

An alternative approach that you might consider is to just have one 'alpha', find the position of a character in word, and extract from this same alpha the corresponding character (starting at the end of alpha).

index() will be useful, regardless the approach you decide to take.

This post has been edited by andrewsw: 18 April 2013 - 03:28 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Python function solution

Posted 18 April 2013 - 03:32 PM

There is also find, rfind and rindex. The docs
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1