How do I write code to find words that are in the same specified position in a string. The position should be like this: - 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: - text = "A boy is being bully by Aloz"
And it will return the result:
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.
5 4017 bvdet 2,851
Recognized Expert Moderator Specialist
Maybe it's just me, but I do not understand the rules.
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". - 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"
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?
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. - >>> 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
dwblas 626
Recognized Expert Contributor
For starters, the "return sym" statement returns/exits after the first word. Instead, send each word to the function - 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)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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)
{
...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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.
...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |