Computers and Technology
26.09.2020 05:13
156
431
7
Solved by an expert

The compare_strings function is supposed to compare just the alphanumeric content

The compare_strings function is supposed to compare just the alphanumeric content of two strings, ignoring upper vs lower case and punctuation. But something is not working. Fill in the code to try to find the problems, then fix the problems. import re
def compare_strings(string1, string2):
#Convert both strings to lowercase
#and remove leading and trailing blanks
string1 = string1.lower().strip()
string2 = string2.lower().strip()

#Ignore punctuation
punctuation = r"[.?!,;:-']"
string1 = re.sub(punctuation, r"", string1)
string2 = re.sub(punctuation, r"", string2)

#DEBUG CODE GOES HERE
print(___)

return string1 == string2

print(compare_strings("Have a Great Day!", "Have a great day?")) # True
print(compare_strings("It's raining again.", "its raining, again")) # True
print(compare_strings("Learn to count: 1, 2, 3.", "Learn to count: one, two, three.")) # False
print(compare_strings("They found some body.", "They found somebody.")) # False
Show Answers
fatherbamboo
fatherbamboo
4,4(79 marks)

There is a problem in the given code in the following statement:

Problem:

punctuation = r"[.?!,;:-']"

This produces the following error:

Error:

bad character range

Fix:

The hyphen - should be placed at the start or end of punctuation characters. Here the role of hyphen is to determine the range of characters. Another way is to escape the hyphen - using using backslash \ symbol.

So the above statement becomes:

punctuation = r"[-.?!,;:']"  

You can also do this:

punctuation = r"[.?!,;:'-]"  

You can also change this statement as:

punctuation = r"[.?!,;:\-']"

Explanation:

The complete program is as follows. I have added a print statement print('string1:',string1,'\nstring2:',string2) that prints the string1 and string2 followed by return string1 == string2  which either returns true or false. However you can omit this print('string1:',string1,'\nstring2:',string2) statement and the output will just display either true or false

import re  #to use regular expressions

def compare_strings(string1, string2):  #function compare_strings that takes two strings as argument and compares them

   string1 = string1.lower().strip()  # converts the string1 characters to lowercase using lower() method and removes trailing blanks

   string2 = string2.lower().strip()  # converts the string1 characters to lowercase using lower() method and removes trailing blanks

   punctuation = r"[-.?!,;:']"  #regular expression for punctuation characters

   string1 = re.sub(punctuation, r"", string1)  # specifies RE pattern i.e. punctuation in the 1st argument, new string r in 2nd argument, and a string to be handle i.e. string1 in the 3rd argument

   string2 = re.sub(punctuation, r"", string2)  # same as above statement but works on string2 as 3rd argument

   print('string1:',string1,'\nstring2:',string2)  #prints both the strings separated with a new line

   return string1 == string2  # compares strings and returns true if they matched else false

#function calls to test the working of the above function compare_strings

print(compare_strings("Have a Great Day!","Have a great day?")) # True

print(compare_strings("It's raining again.","its raining, again")) # True

print(compare_strings("Learn to count: 1, 2, 3.","Learn to count: one, two, three.")) # False

print(compare_strings("They found some body.","They found somebody.")) # False

The screenshot of the program along with its output is attached.


The compare_strings function is supposed to compare just the alphanumeric content of two strings, ig
imtimthetatman
imtimthetatman
4,6(2 marks)

The brains of a computer

Popular Questions about the subject: Computers and Technology

ANSWER IF UR SINGLE im bored...
Computers and Technology
22.11.2021 22:39
Answer if u get the joke ANSWER IF UR SINGLE im bored...
Computers and Technology
29.04.2020 14:02
A graph is a diagram of a relationship between two or more variables...
Computers and Technology
07.12.2020 02:39
Any one has what s app?​...
Computers and Technology
04.04.2020 04:42
Deleting a folder will the subfolders....
Computers and Technology
24.11.2021 09:55
What is Ethical Video?...
Computers and Technology
05.07.2022 23:01

New questions by subject

Literature in the middle ages was impacted by...
History
25.11.2022 10:59
Marijuana, lsd, and club drugs are included together because:...
Biology
17.06.2023 23:07
Sound that a person makes when they are drawing in a piece of...
Social Studies
07.06.2022 03:33
What is an opinion held by a group of people on a problem or...
Health
15.09.2021 04:16
State the definition of the term differentiated diploid nucleus....
Biology
03.04.2020 05:38
What s the relationship between chromosome numbers in body cells...
Biology
28.02.2023 03:52
Los chicos simpáticos a.)ser b.)es c.) somos d.) son...
Spanish
30.04.2022 11:02
#
#
#
#
# #

We expand our knowledge with many expert answers