Question

In: Computer Science

Develop Python implementation of combinatorial algorithm which will find all possible Anagrams of user input. Hint:...

  • Develop Python implementation of combinatorial algorithm which will find all possible Anagrams of user input. Hint: use backtracking algorithm.
  • Example:
  • Input = ROWAN UNIVERSITY
  • Output (298 possibilities):
  • YOUR WINTERS VAIN
  • OAT RUINS WIN VERY
  • ROAR TUNES WIN IVY
  • IRONY UNWISER VAT
  • TIE IN SUN ROW VARY…

Solutions

Expert Solution

Anagram : It is a string which can be formed rearranging characters of the string.

Approach :

1.For each element at i we will swap it with all other elements after if it doesnot contain duplicates.

We will reswap for every element after index i and backtract it.

Below is implementation of above approach :

def single_occur(s, start, curr): 
    for i in range(start, curr): 
        if s[i] == s[curr]: 
            return 0
    return 1
# Store all anagrams
anagrams=[]

def findPermutations(cur_string, index, n): 
    if index == n: 
        s=""
        for i in cur_string:
            s+=i
        anagrams.append(s)
        return
 
    for i in range(index, n): 
        # Proceed further for str[i] if and only if 
        # occur only one time in between s[index] and s[i]
        # if it occur only one time swap index with i
        # and recur and then swap them again to backtrack
        check = single_occur(cur_string, index, i) 
        if check: 
            temp = cur_string[index]
            cur_string[index]=cur_string[i]
            cur_string[i]=temp
            findPermutations(cur_string, index + 1, n)
            temp = cur_string[index]
            cur_string[index]=cur_string[i]
            cur_string[i]=temp  
string = list(input())
n = len(string)
findPermutations(string, 0, n)
cnt=len(anagrams)
print("Total anagrams =",cnt)
for i in anagrams:
    print(''.join(i))

Below is screenshot of code along with output


Related Solutions

PYTHON Find anagrams of a word using recursion. This a possible run of your program: Enter...
PYTHON Find anagrams of a word using recursion. This a possible run of your program: Enter a word or empty string to finish: poster The word poster has the following 6 anagrams: presto repost respot stoper topers tropes
Write a design algorithm and a python program which asks the user for the length of...
Write a design algorithm and a python program which asks the user for the length of the three sides of two triangles. It should compute the perimeter of the two triangles and display it. It should also display which triangle has the greater perimeter. If both have the same perimeter it should display that the perimeters are the same. Formula: Perimeter of triangleA = (side1A + side2A +side3A) See the 2 sample outputs. Enter side1 of TriangleA: 2 Enter side2...
3. Write a Java program that discover all anagrams of all wordslisted in the input file,...
3. Write a Java program that discover all anagrams of all wordslisted in the input file, “dict.txt”. An anagram of a work is a rearrangement of its letters into a new legal word. Your program should do the following: a. Read in the given “dict.txt” file and sort it in each word’s canonical form. The canonical form of a word contains the same letters as the original word, but in sorted order b. Instead of putting the “dict.txt” in the...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
Write a python programme in a Jupyter notebook which asks the user to input the number...
Write a python programme in a Jupyter notebook which asks the user to input the number of radioactive nuclei in a sample, the number at a later time, and the elapsed time. The programme should calculate and display the decay constant and the half-life. The decay is described by the equations: ? = ? ?−?? ?0 ?1/2 = ln (2) . Test your programme a sample that contains 4.00x108 nuclei initially and 1.57x107 nuclei after 150 seconds. SO THEREFORE A...
Develop a python program that prompts the user to take up the quiz (which has choices)...
Develop a python program that prompts the user to take up the quiz (which has choices) with limited attempts and limited time. Suppose if the user answered wrong then they have to get numer of attempts(limited) Quiz should have questions on General knowldge & multiple choices for those questions
IN PYTHON create a python program that accepts input from the user in the following sequence:...
IN PYTHON create a python program that accepts input from the user in the following sequence: 1. Planet Name 2. Planet Gravitational Force(g) for data, use the planets of our solar system. The data input is to be written in 2 separate lists, the names of which are: 1. planetName 2. planet GravitationalForce(g) A third list is required that will store the weight of a person with mass of 100kg, the formula of which is: W=mg(where m is mass of...
In this assignment you will be implementing a function that will find all single-word anagrams given...
In this assignment you will be implementing a function that will find all single-word anagrams given a single word and a list of all words. In the starter code you will the starting source file anagram.cpp and the list of words wordlist.txt. You will need to implement the anagram() function which takes a string for the word to find anagrams of as the first parameter and a vector of strings containing a list of all words as the second parameter....
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After...
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After division, each process sorts its part of the array using an efficient algorithm. Then, the subarrays are merged into larger sorted subarrays. b. Analyze the communication and computation times if the number of processes is equal to the number of array elements, n. c. Repeat Part b if the number of processes is less than the number of array elements. Assume that the computation...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT