473,503 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python symmetric words

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

Expand|Select|Wrap|Line Numbers
  1. word1= ('a','b','c','d','e','f','g','h','i','j','k','l','m')
  2. word2= ('z','y','x','w','v','u','t','s','r','q','p','o','n')
  3.  
  4. word1[n] == word2[n] 
An example:

Expand|Select|Wrap|Line Numbers
  1. text = "A boy is being bully by Aloz"
And it will return the result:

Expand|Select|Wrap|Line Numbers
  1. ['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.
Apr 18 '13 #1
5 4017
bvdet
2,851 Recognized Expert Moderator Specialist
Maybe it's just me, but I do not understand the rules.
Apr 18 '13 #2
dwblas
626 Recognized Expert Contributor
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".
Expand|Select|Wrap|Line Numbers
  1. word1= ('a','b','c','d','e','f','g','h','i','j','k','l','m')
  2. word2= ('z','y','x','w','v','u','t','s','r','q','p','o','n')
  3. text = "A boy is being bully by Aloz"
  4. text_split = text.split()
  5.  
  6. if len(word1) == len(text_split):
  7.     for ctr in range(len(text_split)):
  8.         if word1[ctr].lower() == text_split[ctr][0].lower():
  9.             print text_split[ctr],
  10.     print
  11. else:
  12.     print "The two strings are not of equal length" 
Apr 18 '13 #3
Jane Janey
4 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. >>> symmetrics("boy great bevy bully")
  2. ['bevy','boy']
  3. >>> symmetrics("There is a car and a book;")
  4. ['a']
  5.  
All I can think about the solution is this but I can't run it since it's wrong:

Expand|Select|Wrap|Line Numbers
  1. def symmetrics(text):
  2.     punc_char= ",.?!:'\/"
  3.     for letter in text:
  4.         if letter in punc_char:
  5.           text = text.replace(letter, ' ') 
  6.     alpha1 = 'abcdefghijklmnopqrstuvwxyz'
  7.     alpha2 = 'zyxwvutsrqponmlkjihgfedcba'
  8.     sym = []
  9.     for word in text.lower().split():
  10.         n = range(0,len(word))
  11.         if word[n] == word[len(word)-1-n]:
  12.             sym.append(word)
  13.         return sym
  14.  
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?
Apr 19 '13 #4
bvdet
2,851 Recognized Expert Moderator Specialist
Jane Janey,

The use of str method index in combination with extended slicing will help a great deal.
Expand|Select|Wrap|Line Numbers
  1. >>> alpha = 'abcdefghijklmnopqrstuvwxyz'
  2. >>> alpha[::-1]
  3. 'zyxwvutsrqponmlkjihgfedcba'
  4. >>> alpha.index("z")
  5. 25
  6. >>> word = "bevy"
  7. >>> for i in range(int(len(word)/2)):
  8. ...     if alpha.index(word[i]) != alpha[::-1].index(word[-i-1]):
  9. ...         print "not symmetrical"
  10. ...         
  11. >>> word = "symmetric"
  12. >>> for i in range(int(len(word)/2)):
  13. ...     if alpha.index(word[i]) != alpha[::-1].index(word[-i-1]):
  14. ...         print "not symmetrical"
  15. ...         
  16. not symmetrical
  17. not symmetrical
  18. not symmetrical
  19. not symmetrical
  20. >>> 
HTH
Apr 19 '13 #5
dwblas
626 Recognized Expert Contributor
For starters, the "return sym" statement returns/exits after the first word. Instead, send each word to the function
Expand|Select|Wrap|Line Numbers
  1. def symmetrics(word):
  2.     """ this function goes through all letters in the word, while
  3.         you only have to compare the first half to the last half,
  4.         so that adjustment is left up to you
  5.     """
  6.  
  7.     ## remove punctuation
  8.     ## if you don't understand list comprension 
  9.     ## then just use a for() loop
  10.     word_2 = [ltr for ltr in word.lower() if "a" <= ltr <= "z"]
  11.     print "".join(word_2), 
  12.  
  13.     alpha1 = 'abcdefghijklmnopqrstuvwxyz'
  14.     alpha2 = alpha1[::-1]
  15.  
  16.     ## front and back locations to compare
  17.     front = 0
  18.     back = len(word_2) - 1
  19.     while back > -1:  ## goes all the way through the word
  20.         if front != back:  ## center letter is not compared is it???
  21.             front_ltr = word_2[front]
  22.             back_ltr = word_2[back]
  23.             front_location = alpha1.find(front_ltr)
  24.             back_location = alpha2.find(back_ltr)
  25.             if front_location != back_location:  ## any mismatch exits
  26.                 return False
  27.         front += 1
  28.         back -= 1
  29.  
  30.     return True  ## everything matched
  31.  
  32. for word in ["bevy", "boy", "Aloz", "not", "knot's&", '"guess"']:
  33.     print word, symmetrics(word) 
Apr 19 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1563
by: Joe | last post by:
The recipe in question is "Implementing Static Methods". It shows how to use staticmethod(). This sentence in the Discussion section isn't clear to me: "An attribute of a class object that...
1
1358
by: Garett Shulman | last post by:
Hello, I am trying to create a wrapper for a function with the following prototype: int ap_get_playlist(int session, int *argc, char ***the_list); I'm not quite sure how to construct the...
1
2298
by: Yanping Zhang | last post by:
Here are more details about my codes, please help! The function declared in C: typedef void (WINAPI *PLEARNCALLBACKPROC) (unsigned int progress, unsigned int sigQuality, unsigned long...
0
1514
varuns
by: varuns | last post by:
if i need to call a python function from c, i can use PyImport _Import() python-c API. Following code shows calling python function "add1" from python module "def1" int add(int x, int y) { ...
5
2035
by: fernando | last post by:
Could someone post an example on how to register a python function as a callback in a C function? It expects a pointer to PyObject... how do I expose that? Basically, the signature of the function...
0
2286
by: grbCPPUsr | last post by:
I am new to Python. I would like to use Python for the specialized purpose of dynamic expressions parsing & evaluation in my C++ application. I would like to encapsulate the expressions to be...
9
4975
by: grbgooglefan | last post by:
I am trying to pass a C++ object to Python function. This Python function then calls another C++ function which then uses this C++ object to call methods of that object's class. I tried...
0
1389
by: bruce | last post by:
hey guys... i managed to solve what i was attempting.. my goal was rather simple, to be able to have a python script, call a ruby app, and be able to return a value from the ruby (child) app to...
6
2543
by: Daniel | last post by:
I hope this question is OK for this list. I've downloaded Rpyc and placed it in my site packages dir. On some machines it works fine, on others not so much. Here is one error I get when I try...
2
2646
by: natachai | last post by:
I am new in python and pysqlite. Right now, I am reallly trying to figure it out the way that I can make python function read the table in sqlite database and calculate data using SQL language. ...
0
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7313
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3146
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.