answer.
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
hodyreva
3 months 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:
Natasha_Volkova [1K]3 months ago
4 0

Answer:

The issue within the provided code is located in this part:

Problem:

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

This results in the following error:

Error:

bad character range

Solution:

The hyphen - needs to be positioned at either the beginning or end of the character list. In this context, the hyphen acts to specify a range of characters. Another option is to escape the hyphen - using a backslash \.

Thus, the corrected statement would be:

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

Alternatively, you can write it as:

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

Additionally, you might modify it like this:

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

Explanation:

The complete program is outlined as follows. I have inserted a print statement print('string1:',string1,'\nstring2:',string2) that outputs string1 and string2 , followed by return string1 == string2, which will return either true or false. However, you can omit the print('string1:',string1,'\nstring2:',string2) line and the result will still show true or false

import re  #to employ regular expressions

def compare_strings(string1, string2):  #defining compare_strings function that takes in two strings and checks them

   string1 = string1.lower().strip()  # converts string1 to lowercase using lower() method and gets rid of leading and trailing spaces

   string2 = string2.lower().strip()  # similarly converts string2 to lowercase and removes spaces

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

   string1 = re.sub(punctuation, r"", string1)  # applies RE pattern for punctuation in string1

   string2 = re.sub(punctuation, r"", string2)  # same action as above for string2

   print('string1:',string1,'\nstring2:',string2)  #displays both strings, separated by a new line

   return string1 == string2  #compares the strings and returns true if they match, otherwise false

#function calls to validate the functionality of 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

A screenshot of the program and its output is provided.

You might be interested in
Assume that the following variables have been properly declared and initialized.
ivann1987 [1066]

Answer:

boolean rsvp;

int selection;

String option1;

String option2;

(a) if(rsvp){

System.out.println("Attending");

} else {

System.out.println("Not attending");

}

(b) if (selection == 1){

System.out.println("Beef");

} else if (selection == 2){

System.out.println("Chicken");

} else if (selection == 3) {

System.out.println("Pasta");

} else {

System.out.println("Fish");

}

(c) switch(selection){

case 1:

if(rsvp){

option1 = "Thanks for attending. You will be served beef.";

} else{

option1 = "Sorry you can't make it";

}

break;

case 2:

if(rsvp){

option1 = "Thanks for attending. You will be served chicken.";

} else{

option1 = "Sorry you can't make it";

}

break;

case 3:

if(rsvp){

option1 = "Thanks for attending. You will be served pasta.";

} else{

option1 = "Sorry you can't make it";

}

break;

default:

if(rsvp){

option1 = "Thanks for attending. You will be served fish.";

} else{

option1 = "Sorry you can't make it";

}

break;

}

(d) if (option1.equals(option2)){

System.out.println("True");

} else {

System.out.println("False");

}

Explanation:

This code is written in Java.

a) It uses an if statement to print "attending" if rsvp evaluates to true and "not attending" if rsvp is false.

b) An if-else structure determines which food choice is printed depending on user input. If selection equals 1, beef is printed; if it is 2, chicken appears; if it is 3, pasta is displayed; any other value results in fish being printed.

c) The switch statement checks the value of selection and uses an inner if statement to assign "Thanks for attending. You will be served beef." to option1 if rsvp is true. This assignment varies with the selection's value. When rsvp is false, option1 gets the message "Sorry you can't make it."

d) An if statement evaluates whether option1 and option2 are equivalent and outputs true if they match; otherwise, it indicates false.

8 0
3 months ago
You are given a string of n characters s[1 : : : n], which you believe to be a corrupted text document in which all punctuation
ivann1987 [1066]

Response: explained in the explanation section

Explanation:

Given that:

Assume D(k) =║ true if [1::: k] is a valid sequence of words, or false otherwise

  • In determining D(n)

the sub problem s[1::: k] is a valid sequence of words IFF s[1::: 1] is valid and s[ 1 + 1::: k] is a valid word.

Thus, we derive that D(k) is defined by the following recurrence relation:

D(k) = ║ false max(d[l] ∧ DICT(s[1 + 1::: k]) otherwise

Algorithm:

Valid sentence (s,k)

D [1::: k]             ∦ array of boolean variables.

for a ← 1 to m

do;

d(0) ← false

for b ← 0 to a - j

for b ← 0 to a - j

do;

if D[b] ∧ DICT s([b + 1::: a])

d (a) ← True

(b). Algorithm Output

      if D[k] == True

stack = temp stack            ∦stack assists in displaying the strings in order

c = k

while C > 0

stack push (s [w(c)]::: C] // w(p) denotes the index in s[1::: k] of the valid word // at position c

P = W (p) - 1

output stack

= 0 =

cheers, I hope this aids you!!!

8 0
2 months ago
8. In time series, which of the following cannot be predicted? A) large increases in demand B) technological trends C) seasonal
Natasha_Volkova [1026]

Answer: Random fluctuations.

Explanation: A time series consists of data points ordered chronologically, arranged in equal intervals. Typically, this data sequence is systematic and has defined intervals. However, there’s no allowance for randomness, making unpredictable variations, or random fluctuations, absent in such series. Thus, option (D) is the correct choice.

6 0
2 months ago
Sushant is a new manager and he wants to share his ideas and working protocol with his team.Compare the advantages and disadvant
oksian1 [950]

Answer:

Benefits of Oral Communication

  1. Utilizing visual aids such as PowerPoint during discussions can enhance his team's understanding of his concepts and procedures.
  2. Staff responses tend to be more immediate and sincere compared to written replies.

Drawbacks of Oral Communication

  1. If Sushant struggles with stage fright or lacks strong communication skills, it may negatively impact the team's perception of him.
  2. There is a risk that staff may forget portions of Sushant's spoken communication, as written information is generally more reliable for retention.

Benefits of Written Communication

  1. A well-crafted memo articulates Sushant's ideas and procedures clearly to the staff.
  2. A feedback questionnaire could be included for staff input.
  3. Clearly stated goals and objectives.

Drawbacks of Written Communication

  1. Excessively lengthy written material may pose challenges for comprehension or retention.

It is advisable for Sushant to prioritize written communication to share his ideas and protocols effectively.

3 0
3 months ago
Other questions:
  • When using the Python shell and code block, what triggers the interpreter to begin evaluating a block of code
    14·1 answer
  • This program outputs a downwards facing arrow composed of a rectangle and a right triangle. The arrow dimensions are defined by
    15·1 answer
  • Write a statement that reads a floating point value from standard input into temperature. Assume that temperature. has already b
    6·1 answer
  • A company decides to relocate its operations to another state in order to take advantage of some new business investment credits
    15·1 answer
  • Consider a one-way authentication technique based on asymmetric encryption: A --> B: IDA B --> A: R1 A --> B: E(PRa, R1
    13·1 answer
  • Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, a
    10·1 answer
  • Write a program that calculates an adult's fat-burning heart rate, which is 70% of 220 minus the person's age. Complete fat_burn
    10·1 answer
  • Write the execution steps if the input is a lion.
    11·1 answer
  • Kylee needs to ensure that if a particular client sends her an email while she is on vacation, the email is forwarded to a
    10·1 answer
  • Which of the following are recommended techniques for protecting computer files and data? Check all of the boxes that apply.
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!