Question

In: Computer Science

Having some trouble with this python question. An answer would be greatly appreciated! #Write a function...

Having some trouble with this python question. An answer would be greatly appreciated!

#Write a function called are_anagrams. The function should
#have two parameters, a pair of strings. The function should
#return True if the strings are anagrams of one another,
#False if they are not.
#
#Two strings are considered anagrams if they have only the
#same letters, as well as the same count of each letter. For
#this problem, you should ignore spaces and capitalization.
#
#So, for us: "Elvis" and "Lives" would be considered
#anagrams. So would "Eleven plus two" and "Twelve plus one".
#
#Note that if one string can be made only out of the letters
#of another, but with duplicates, we do NOT consider them
#anagrams. For example, "Elvis" and "Live Viles" would not
#be anagrams.


#Write your function here!

#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print: True, False, True, False, each on their own line.
print(are_anagrams("Elvis", "Lives"))
print(are_anagrams("Elvis", "Live Viles"))
print(are_anagrams("Eleven plus two", "Twelve plus one"))
print(are_anagrams("Nine minus seven", "Five minus three"))

Solutions

Expert Solution

I have uploaded the images of the code,typed code and output of the code. I provided explanation using comments.

Some predefined functions that are used in the program.
1)String.replace(char1,char2):It replaces the all char1 in the string with char2
2)String.lower():Convert the the string into Lowercase
3)len(list):return the length of the List.
4)list.count(x):return the number of occurrence of character x in the list.

Images of the code:

Note:You may not get indented code by copying the typed code(below) .Please refer code images and indent the copied code.

Typed code:

#Function named 'are_anagrams' that take a pair of strings as parameters
def are_anagrams(string1,string2):
#As we have to Ignore spaces.
#Removing space(s) in the String1 and String2
string1=string1.replace(" ","");
string2=string2.replace(" ","");
  
#As we have to Ignore case
#Converting Both the Strings into Lowercase
string1=string1.lower()
string2=string2.lower()

#Converting Both strings into a List Characters
string1=list(string1)
string2=list(string2)

#Comparing the length of the String1 and String2
if(len(string1)==len(string2)):
#Comparing Occurrence Of each Character in String1, String2 are equal
for x in string1:
if(string1.count(x)!=string2.count(x)):
#If the Number of Occurrence of a character varies then return false
return False
#If the Number of Occurrence of every character eqaul then return false
return True
else:
#If the length varies then return False
return False
  
#Some test Cases
print(are_anagrams("Elvis","Lives"))
print(are_anagrams("Elvis","Lives Viles"))
print(are_anagrams("Eleven plus two","Twelve plus one"))
print(are_anagrams("Nine minus seven","Five minus three"))

Output:

If You Have Any Doubt's please ask using comments

Have A Great Day!


Related Solutions

I have to write a random password generator in Python 3 and im having some trouble...
I have to write a random password generator in Python 3 and im having some trouble writing the code and i dont really know where to start. The prompt goes as such: The problem in this assignment is to write a Python program that uses functions to build (several options of) passwords for the user. More specifically, your program will do the following: 1. Prompt the user for the length of the password to be generated. Call this length lenP....
Please answer the following in regard to Varicella Zoster, sources/references would be greatly appreciated. TIA Varicella...
Please answer the following in regard to Varicella Zoster, sources/references would be greatly appreciated. TIA Varicella Zoster is caused by? Varicella Zoster is identified by the following characteristics: Incubation period: Symptoms: Following the presentation of patients with symptoms indicative of Varicella Zoster diagnosis may be made through: Treatments may include: Preventative methods for preventing the growth and establishment of Varicella Zoster Carriers Transmission Epidemiology Prevalence Target population: Susceptibility Current Research:
If you could answer all 4 that would be greatly appreciated, thanks 1. Hull Company reported...
If you could answer all 4 that would be greatly appreciated, thanks 1. Hull Company reported the following income statement information for the current year: Sales $ 413,000 Cost of goods sold: Beginning inventory $ 136,500 Cost of goods purchased 276,000 Cost of goods available for sale 412,500 Ending inventory 147,000 Cost of goods sold 265,500 Gross profit $ 147,500 The beginning inventory balance is correct. However, the ending inventory figure was overstated by $23,000. Given this information, the correct...
Any advice or recommendation would be greatly appreciated You are the sport agent for a rising...
Any advice or recommendation would be greatly appreciated You are the sport agent for a rising basketball star just drafted in the first round. Your client has two offers on the table option $90 million dollars over 5 years or option $30 million signing bonus and 40 million over the next 3 yrs. What option do you recommend? How does the time value of money impact your decision?
If you could show work it would be greatly appreciated also, if possible use the template...
If you could show work it would be greatly appreciated also, if possible use the template that I have provided. Thank you, also this is all the question provides me with. SallyMay, Inc., designs and manufactures T-shirts. It sells its T-shirts to brand name clothes retailers in lots of one dozen. SallyMay's May 2013 static budget and actual results for direct inputs are as follows: Static Budget Number of T-shirt lots (1 lot1 dozen) 400 Per Lot of T-shirts: Direct...
If somebody could assist me with the following questions, that would be greatly appreciated! Thank you!...
If somebody could assist me with the following questions, that would be greatly appreciated! Thank you! 1. How can fluorescence be used to determine the concentration of a molecule? 2. Fluorescence and absorbance comparison (energy, wavelengths)? 3. Why and how we can determine quinine concentration? (include wavelengths used) 4. Why is the detector in a fluorometer placed at a specific angle to the incident beam?
I am having trouble understanding what value to use for each function. The question is: "Tom...
I am having trouble understanding what value to use for each function. The question is: "Tom plans to save $88 a month, starting today, for 22 years. Dean plans to save $88 a month for 22 years, starting one month from today. Both Tom and Dean expect to earn an average return o 5.27 percent APR on thier savings and both will make the same number of deposits. At the end of the 22 years, how much more (in $)...
Please ANSWER ALL (explanation with or without BA 2 calculator greatly appreciated) #1. What is the...
Please ANSWER ALL (explanation with or without BA 2 calculator greatly appreciated) #1. What is the value today of a money machine that will pay $3,042.00 every six months for 11.00 years? Assume the first payment is made 5.00 years from today and the interest rate is 6.00% #2. What is the value today of a money machine that will pay $4,796.00 per year for 8.00 years? Assume the first payment is made today and that there are 8.0 total...
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
Hi, I'm having some trouble with this question, especially part B. Stocks offer an expected rate...
Hi, I'm having some trouble with this question, especially part B. Stocks offer an expected rate of return of 18%, with a standard deviation of 22%. Gold offers an expected return of 10% with a standard deviation of 30%. a) In light of the apparent inferiority of gold with respect to average return and volatility, would anyone hold gold in his portfolio? (4 points) b) Assume that the correlation between Stocks and Gold is -0.5. Find the weights wS and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT