Question

In: Computer Science

Write the following Python code: A string X is an anagram of string Y if X...

Write the following Python code:

A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb".

A set of strings is anagram-free if it contains no pair of strings which are anagrams of each other. Given a set of strings words, return the size of its largest anagram-free subset. Note that the entire set is considered a subset of itself.

Examples:

  1. words = ["abcd","abdc","dabc","bacd"]
    
    Returns: 1
    
    All of the strings in words are anagrams of each other, so no two of them can simultaneously belong to an anagram-free set.
  2. words = ["abcd","abac","aabc","bacd"]
    
    Returns: 2
    
    One of the maximum anagram-free subsets is ["abcd","abac"].
  3. words = ["aa","aaaaa","aaa","a","bbaaaa","aaababaa"]
    Returns: 6
    
    Note that strings of different length cannot be anagrams of each other.
  4. words = ["creation","sentence","reaction","sneak","star","rats","snake"]
    Returns: 4

Solutions

Expert Solution

CODE: Python Programming Language

def anagram(S):
    def sort(word):
        chars = list(word)
        chars.sort()
        return ''.join(chars)
    subset = set()
    for word in S:
        for i in [i for i in S if i != word]:
            str = sort(word)
            if str not in subset or str != sort(i):
                subset.add(str)
    return len(subset)
#Test Case1
words = ["abcd","abdc","dabc","bacd"]
print(words)
result = anagram(words)
print(result)

#Test case 2
words = ["abcd","abac","aabc","bacd"]
print(words)
result = anagram(words)
print(result)

#Test Case 3
words = ["aa","aaaaa","aaa","a","bbaaaa","aaababaa"]
print(words)
result = anagram(words)
print(result)

#Test Case 4
words = ["creation","sentence","reaction","sneak","star","rats","snake"]
print(words)
result = anagram(words)
print(result)

============================================================================

SCREENSHOT OF THE CODE:

============================================================================

OUTPUT:

Thank you.


Related Solutions

Write the following Python script: Problem Statement A string X is an anagram of string Y...
Write the following Python script: Problem Statement A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which...
Write code in Python that does the following : An anagram is when the order of...
Write code in Python that does the following : An anagram is when the order of a word can be changed to create a new word (evil, live, and vile for example). Using the dictionary provided, find all of the anagram groups. Which words contain the most anagrams? How large is this group? Here is another example: Alert, alter, and later have 3 in their group. Use the provided dict.txt as your word list, solution must also finish in less...
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
Please provide Python code that does the following: 3) Write a program that takes a string...
Please provide Python code that does the following: 3) Write a program that takes a string as input, checks to see if it is comprised entirely of letters, and if all those letters are lower case. The output should be one of three possible messages: Your string is comprised entirely of lower case letters. Your string is comprised entirely of letters but some or all are upper case. Your string is not comprised entirely of letters. Your program may NOT:...
Python. Write a code that asks the user to enter a string. Count the number of...
Python. Write a code that asks the user to enter a string. Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total. You may need to write 5 different if statements, one for each vowel. Enter a string: mouse mouse has 3 different vowels
PLEASE USE PYTHON CODE Compute the zero of the function y(x) from the following data: x...
PLEASE USE PYTHON CODE Compute the zero of the function y(x) from the following data: x = 0.2, 0.4, 0.6, 0.8, 1.0 y = 1.150, 0.855, 0.377, -0.266, -1.049 Use inverse interpolation with the natural cubic spline
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the given wordlist  An anagram dictionary has each word with the letters sorted alphabetically creating a “key”.
Write PYTHON CODE to answer the following question: Consider the following data: x = [0, 2,...
Write PYTHON CODE to answer the following question: Consider the following data: x = [0, 2, 4, 6, 9, 11, 12, 15, 17, 19] y = [5, 6, 7, 6, 9, 8, 8, 10, 12, 12] Using Python, use least-squares regression to fit a straight line to the given data. Along with the slope and intercept, compute the standard error of the estimate and the correlation coefficient. Best fit equation y = ___ + ___ x Standard error, Sy/x =...
write a python code that Returns a string composed of characters drawn, in strict alternation, from...
write a python code that Returns a string composed of characters drawn, in strict alternation, from s1 and s2. If one string is longer than the other, the excess characters are added to the end of the string as shown in the examples below #Example 1 - blend("ape", "BANANA") returns "aBpAeNANA" #Example 2 - blend("BOOT", "gold") returns "BgOoOlTd"
1.   What is the output of the following code: string s1 = “X”; string s2 =...
1.   What is the output of the following code: string s1 = “X”; string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”; cout << s2; 2.   What is the output of the following code: string s1 = “X”; string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”; cout << s[0] + s[3]; 3.   What is the output of the following code: string s1 = “X”; string...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT