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.
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 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...
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...
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,...
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter...
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter his/her favorite English saying, then counts the number of vowels in that (note that the user may type the saying using any combination of upper or lower case letters). Example: Enter your favorite English saying: Actions speak LOUDER than words. Number of wovels: 10
Reminder, remember to code, evaluate, save, execute (run), and check the Python program for possible errors....
Reminder, remember to code, evaluate, save, execute (run), and check the Python program for possible errors. Remember to create the sales.txt file with the following data (1000, 2000, 3000, 4000, 5000). Tip, the sales.txt file must be in the same directory as the read_sales_text_file.py Python program. def main(): sales_file = open('sales.txt', 'r') for line in sales_file: amount = float(line) print('$', format(amount, '.2f')) sales_file.close() main() Output after you successfully run your code: $ 1000.00 $ 2000.00 $ 3000.00 $ 4000.00 $...
( USE C++ ) The program prompts the user to enter a word. The program then...
( USE C++ ) The program prompts the user to enter a word. The program then prints out the word with letters in backward order. For example, if the user enter "hello" then the program would print "olleh" show that it works .
Write a Python program using functions and mainline logic which prompts the user to enter a...
Write a Python program using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a file. It should then display the following data to back to the user: The list of integers The lowest number in the list The highest number in the list The total sum of all the numbers in the list The average number in the list At a minimum, the numbers should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT