Question

In: Computer Science

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

Solutions

Expert Solution

Anagram: Anagram of a word is another word which is formed by rearranging the letters of same word.

So poster will have 6! anagrams including itself.

def getstring(lst):
   return ''.join(lst)

def print_anagram(lst, left, right):
   if left==right:
       print(getstring(lst))
   else:
       for i in range(left,right+1):
           lst[left], lst[i] = lst[i], lst[left]
           print_anagram(lst, left+1, right)
           lst[left], lst[i] = lst[i], lst[left]

input_string = str(input('Enter a word or empty string to finish: '))
size = len(input_string)
lst = list(input_string)
print('The word ',input_string,' has the following anagrams:')
print_anagram(lst, 0, size-1)


Related Solutions

Write a program in python that implements quicksort, first using recursion and then without recursion.
Write a program in python that implements quicksort, first using recursion and then without recursion.
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…
For this project, you are going to write a program to find the anagrams in a...
For this project, you are going to write a program to find the anagrams in a given dictionary for a given word. If two words have the same letters but in different order, they are called anagrams. For example, “listen” and “silent” are a pair of anagrams. **JAVA** First let’s focus on “LetterInventory.java”. Its purpose is to compute the canonical “sorted letter form” for a given word. 1. Implement the constructor, which takes the String input word. You can assume...
Write, save, and run a Python program Ask the user to enter a number of grams....
Write, save, and run a Python program Ask the user to enter a number of grams. Your program converts that to whole tones, then whole kilograms and whatever is left is in grams. It prints the entered grams , tones , kilograms and the remaining grams.      4 marks for i in range(0,10): print(i) 2    what is wrong in the above python code? display your explanation using print statement. mark 3    insert the following comments into your program of part...
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....
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
The coding for this program to run as described on Python: Your (turtle) program must include:...
The coding for this program to run as described on Python: Your (turtle) program must include: include import turtle on a line after the comments so you can use the various Turtle-related objects and methods. Create a turtle named after your favorite ice cream flavor. Make sure the name has no punctuation and consists only of letters, numbers and the underscore character. Write your python program so your turtle is constantly moving and can be turned left and right using...
In your python program, ask the user to enter the annual income of an employee and...
In your python program, ask the user to enter the annual income of an employee and the years of experience. Pass these data to a function. The function decides if an employee does qualify for a loan or does not, then it prints a message based on the following rules: If the income is equal or greater than $40000, the function checks if the employee’s years of experience is greater than 4, if so the employee gets $6000 loan; otherwise,...
Solving Problems Using Recursion (Python): To solve the problem, you have to use recursion and cannot...
Solving Problems Using Recursion (Python): To solve the problem, you have to use recursion and cannot use for or while loops to solve the problems as well as not using global variables. 1. Create a function that takes a positive integer and returns it with neighboring digits removed. Do not convert the integer to a list. Ex. Input = [5555537777721] Output = [53721]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT