Question

Using Python, Write a function to return all the common characters in two input strings. The...

Using Python, Write a function to return all the common characters in two input strings. The return must be a list of characters and only unique characters will be returned. For example, given 'abcdefg' and 'define', the function will return ['d', 'e', 'f'] (note e only appears once). (Hint: Use the in operator.)

Homework Answers

Answer #1

Solution :

We can do this program in very simple manner using set operations of python.if we consider both time and space complexities this solution is the very efficient .

Code:

input_string1=input("Enter the String 1: ") #taking first string from user
input_string2=input("Enter the String 2: ") #taking second string from user
set1=set(input_string1) #convert the input_string1 to set datatype
set2=set(input_string2) #convert the input_string2 to set datatype
final_output=list(set1 & set2) #apply intersection operation between set1 and set2 using & operator and store into list
final_output.sort() #sort the charcaters in alphabetical order to get the desired output
print ("The common character in both strings are : ") #print common characters are
print(final_output) #print final list

Code and Output Screenshots:

Note : if you have any queries please post a comment thanks a lot..always available to help you...

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list strings and the second...
Write a Python function that takes two parameters: the first a list strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False.
Write a python program that will perform text analysis on an input text using all of...
Write a python program that will perform text analysis on an input text using all of the following steps: 1. Take the name of an input file as a command line argument. For example, your program might be called using python3 freq.py example1 to direct you to process a plain text file called example. More information is given on how to do this below. 2. Read the contents of the file into your program and divide the text into a...
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except...
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except that it only accepts the specified type of input. The function takes two arguments: r prompt: str r type: int, float, str The function will keep prompting for input until correct input of the specified type is entered. The function returns the input. If the input was specified to be a number ( float or int), the value returned will be of the correct...
Write a python function called unique words that takes a phrase as an input string and...
Write a python function called unique words that takes a phrase as an input string and returns a list of the unique words found in that phrase. The words should be listed in alphabetical order.
For each of the following Python programs P and input strings I, give the output P(I),...
For each of the following Python programs P and input strings I, give the output P(I), using the formal definition of P(I) given, and employing any reasonable reference computer C: Definition of P(I), the output of a Python program. Let P be a Python program with respect to a reference computer system C. Suppose P has main function M, and let I be a string of ASCII characters that will be used as input to P. The output of P...
Please write the python code for a function called called count_all_words that takes a phrase as...
Please write the python code for a function called called count_all_words that takes a phrase as an argument and counts each unique word in the phrase. The function should return a list of lists, where each sub-list is a unique [word, count] pair. (See example below.) Hint: A well-written list comprehension can solve this in a single line of code, but this approach is not required.
Write a python function isogram(S) that returns True if and only if no character appears more...
Write a python function isogram(S) that returns True if and only if no character appears more than once in the input string S.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT