Question

In: Computer Science

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 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.

Constraints

  • words will contain between 1 and 50 elements, inclusive
  • Each element of words contains between 1 and 50 characters, inclusive
  • Each element of words will consist of lowercase letters 'a' -- 'z' only
  • All elements of words will be distinct

Check your code with these examples:

------------------------------------------
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.

------------------------------------------

words = ["a"]

Returns: 1

------------------------------------------

words = ["z"]

Returns: 1

------------------------------------------

words = ["abcd","abac","aabc","bacd"]

Returns: 2

One of the maximum anagram-free subsets is ["abcd","abac"].

------------------------------------------
words: ["aa","aaaaa","aaa","a","bbaaaa","aaababaa"]

Returns: 6

Note that strings of different length cannot be anagrams of each other.

------------------------------------------

words = ["creation","sentence","reaction","sneak","star","rats","snake"]

Returns: 4

Solutions

Expert Solution

Program Code Screenshot



Program Sample Console Input/Output Screenshot



Program Code to Copy

# Algorithm
# Sort each word by characters
# sort the word array
# remove all duplicates from the word array
def largestAnagramSubset(words):
    # Sort each word by characters
    for i in range(len(words)):
        words[i] = ''.join(sorted(words[i]))

    # sort words
    words.sort()

    # remove duplicates
    cur = 1
    while(cur < len(words)):
        if(words[cur] == words[cur-1]):
            words.pop(cur)
        else:
            cur += 1

    return len(words)

# demo


print("words = [\"abcd\", \"abdc\", \"dabc\", \"bacd\"]\nReturns:",
      largestAnagramSubset(["abcd", "abdc", "dabc", "bacd"]), "\n")

print("words = [\"a\"]\nReturns:", largestAnagramSubset(["a"]), "\n")

print("words = [\"z\"]\nReturns:", largestAnagramSubset(["z"]), "\n")

print("words = [\"abcd\",\"abac\",\"aabc\",\"bacd\"]\nReturns:",
      largestAnagramSubset(["abcd", "abac", "aabc", "bacd"]), "\n")

print("words = [\"aa\",\"aaaaa\",\"aaa\",\"a\",\"bbaaaa\",\"aaababaa\"]\nReturns:",
      largestAnagramSubset(["aa", "aaaaa", "aaa", "a", "bbaaaa", "aaababaa"]), "\n")

print("words = [\"creation\",\"sentence\",\"reaction\",\"sneak\",\"star\",\"rats\",\"snake\"]\nReturns:",
      largestAnagramSubset(["creation", "sentence", "reaction", "sneak", "star", "rats", "snake"]), "\n")

Related Solutions

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...
Write a script that will first initialize a string variable that will store x and y...
Write a script that will first initialize a string variable that will store x and y coordinates of a point in the form ‘x 3.1 y 6.4’. Then, use string manipulating functions to extract the coordinates and plot them. ANS % create a string variable of the form 'x 3.1 y 6.4' % extract the x and y coordinates and plot str = 'x 2.3 y 4.5'; [letter rest] = strtok(str); [x rest] = strtok(rest); [letter rest] = strtok(rest); y...
Write the following Python script: Problem Statement We want to know how many different people have...
Write the following Python script: Problem Statement We want to know how many different people have eaten at a restaurant this past week. The parameter meals has strings in the format "name:restaurant" for a period of time. Sometimes a person eats at the same restaurant often. Return the number of different people who have eaten at the eating establishment specified by parameter restaurant. For example, "John Doe:Moes" shows that John Doe ate one meal at Moes. Write function howMany that...
Write a Flask app in which a Python script prompts the user to enter a string...
Write a Flask app in which a Python script prompts the user to enter a string and then that string, as entered, gets displayed on an otherwise blank HTML file, called output.html. Show the Python/Flask code and the html code of the output.html file, one below the other as part of your answer.
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 the following Python script: Imagine you live in a world without modules in Python! No...
Write the following Python script: Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it...
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string...
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string expressed as -rwxrwxrwx and prints out whether or not theobject is a directory, file or link. The string is read in from standard input. Convert String (Part 2) Modify the script so its able to print out the octal permission which the string represents along with the file type. This is in addition to part 1. Convert String (Part 3) For the script in...
How do I write a script for this in python in REPL or atom, NOT python...
How do I write a script for this in python in REPL or atom, NOT python shell Consider the following simple “community” in Python . . . triangle = [ ["top", [0, 1]], ["bottom-left", [0, 0]], ["bottom-right", [2, 0]], ] This is the calling of function. >>> nearestneighbor([0, 0.6], triangle, myeuclidean) 'top' The new point is (0, 0.6) and the distance function is Euclidean. Now let’s confirm this result . . . >>> myeuclidean([0, 0.6], [0, 1]) 0.4 >>> myeuclidean([0,...
Write a python script to solve the 4-queens problem using. The code should allow for random...
Write a python script to solve the 4-queens problem using. The code should allow for random starting, and for placed starting. "The 4-Queens Problem[1] consists in placing four queens on a 4 x 4 chessboard so that no two queens can capture each other. That is, no two queens are allowed to be placed on the same row, the same column or the same diagonal." Display the iterations until the final solution Hill Climbing (your choice of variant)
This problem requires python(Project: Evaluate Word Problems) Write a script that enables the user to enter...
This problem requires python(Project: Evaluate Word Problems) Write a script that enables the user to enter mathematical word problems like "two times three" and " seven minus five", then use string processing to break apart the string into the numbers and the operation and return the result. So “two times three” would return 6 and “seven minus five would return 2. To keep things simple, assume the user enters only the words for the numbers 0 through 9 and only...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT