# Python function solution

Page 1 of 1

## 2 Replies - 333 Views - Last Post: 18 April 2013 - 03:32 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=318916&amp;s=557aeebb3ed1bba223eae6399288fc65&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Jane Janey

• New D.I.C Head

Reputation: 0
• 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

• I'm not here to twist your niblets

Reputation: 4209
• Posts: 15,315
• 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

• I'm not here to twist your niblets

Reputation: 4209
• Posts: 15,315
• 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

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }