1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
tatuchka [14]
4 years ago
15

The compare_strings function is supposed to compare just the alphanumeric content of two strings, ignoring upper vs lower case a

nd 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

Computers and Technology
1 answer:
Korolek [52]4 years ago
4 0

Answer:

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.

You might be interested in
A(n)…………is the interface used toconnect external devices to the computer.
Mama L [17]

Answer:

D. PORT

Explanation:

A Port is the interface used to connect external devices to  computer.

Ports are of various types based on the type of communication interface:

For example:

  • Serial port : information transfer takes place one bit at a time.
  • Parallel port : transfer of multiple bits at a time.
  • Ethernet : used for connecting network cable
  • USB : Universal Serial Bus

Computer peripherals like mouse,keyboard, modem, printer etc connect to the computer via the port which is in accordance with their specified communication interface.

6 0
4 years ago
A(n) __ is a list of main points and sub-points of a topic to include in a presentation
skad [1K]

Main points are  key ideas that support a thesis and helps an audience understand and remember what is most important about a speaker's topic, while sub points are supporting points like <span><span>examples, definitions, testimony, and statistics that support or illustrate a speaker's main points.
</span>A preview is a list of main points and sub-points of a topic to include in a presentation</span>


8 0
3 years ago
Read 2 more answers
What is an example of a condition controlled loop? What would the syntax look like?
irina1246 [14]

Answer:

The answer to this question is given below in the explanation section.

Explanation:

WHILE loops and DO WHILE loops are called the condition controlled loops. The execution of these loops depends on a certain condition, when the condition is true, the loop body will be executed and when the condition becomes false, the loop body will not be executed. the major difference between both loops is given below.

In the WHILE loop,  the condition is checked at the beginning of the loop whereas in the do-while loop condition is checked at the end of the loop and, in the do-while loop, the loop body is executed at least once.

The syntax of the while loop is given below

while (condition) {

 // code block to be executed

}

The syntax of do-while loop is given below

do {

 // code block to be executed

}

while (condition);

6 0
3 years ago
"The _____ of the Open Systems Interconnection (OSI) model generates the receiver’s address and ensures the integrity of message
aleksklad [387]

Answer:

The transport layer

Explanation: Layer 4, is the transport layer of the Open System Interconnection (OSI), that handles message transfer of data between end systems or hosts and ensures complete packets transfer.

7 0
3 years ago
Read 2 more answers
What is a hash function? A. A function that computes the location of the values in the array B. A function has allocated memory
galina1969 [7]

Answer:

A hash function is a function which when given a key, generates an address in the table. The example of a hash function is a book call number. Each book in the library has a unique call number. ... The later is always possible only if you know (or approximate) the number of objects to be proccessed.

Explanation:

A

3 0
3 years ago
Read 2 more answers
Other questions:
  • World wide web is another name for Internet <br> True or false?
    8·2 answers
  • 6. Write a program that can multiply an n x m matrix and m x n matrix together: The input specifications are these: Read n and m
    11·1 answer
  • Difference between query and filter<br><br>​
    12·1 answer
  • Why is the following statement true - ideally, your information is abbreviated
    10·2 answers
  • A Windows computer on your network is having issues resolving queries for a specific domain, but not other domains. Other comput
    15·1 answer
  • 1. when is it a good idea to use lossless compression
    8·1 answer
  • write a program that inputs 3 numbers and prints the largest input must be transformed into a integer but printed as a string​
    11·1 answer
  • Select the correct answer
    15·1 answer
  • 5.9.5 Fibonacci <br><br> Y’all I’m struggling here do any of you have the code?
    12·1 answer
  • Can you show me how to code this is GDBonline? explaining each statement with notes//
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!