Question

In: Computer Science

You will be given a string, containing both uppercase and lowercase alphabets(numbers are not allowed). You...

You will be given a string, containing both uppercase and lowercase alphabets(numbers are not allowed). You have to print all permutations of string with the added constraint that you can’t change the uppercase alphabets positions. Respond in Python please

Solutions

Expert Solution

val = input("Enter your String: ")  #taking the input string

result = []                # A list to store all the permutation of string without considering uppercase letter position
  
def permute(data, i, length):    # permute function to calculate all permutation
    if i == length:  
        result.append(''.join(data) ) 
    else:  
        for j in range(i, length):    # this method is bactracking approach to calculate permutation of string
            # swap 
            data[i], data[j] = data[j], data[i]    # swap
            permute(data, i + 1, length)                 # recusively call the function until all position is swaped
            data[i], data[j] = data[j], data[i]    # bactracking 
permute(list(val), 0, len(val)) 

res = []           # another list to store only those permutation which uppercase letter position does not changed
for s in result:      # loop throgh all string of the permutation list
    n = len(s)
    flag=0          # a flag variable to check if the String uppercase letter position does not changed with the given input string
    for i in range(0, n) :
        if (s[i]>='A' and s[i]<='Z') and s[i]!=val[i]: # if letter is uppercase and does not match position then flag=1 
           flag=1  # if flag==1 nit store that string in output result list
           break
    if flag==0:      
        if (s not in res):   # check if that string already present in the lis or not
            res.append(s)
        
for s in res:  # print all the string of output list res
    print(s)

Time complexity of this algorithm is O(n*n!) where n is the length of the string

Output:


Related Solutions

(a) How many passwords can you make with 8 characters using Uppercase, Lowercase, digits using at...
(a) How many passwords can you make with 8 characters using Uppercase, Lowercase, digits using at least one Uppercase, at least one Lowercase and at least one digit? (b) How many numbers between 1 and 1,000,000 (inclusive) are divisible by at least one of 3,4 and 5? (c) How many numbers between 1 and 1,000,000 (inclusive) are divisible by at least one of 12, 14, 15?
Suppose you are given a string containing only the characters ( and ). In this problem,...
Suppose you are given a string containing only the characters ( and ). In this problem, you will write a function to determine whether the string has balanced parentheses. Your algorithm should use no more than O (1) space beyond the input; any algorithms that use space not in O (1) will receive half credit at most. Any solutions that always return true (or always return false) or otherwise try to game the distribution of test cases will receive zero...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." In C, Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program in C to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume the length...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program in C language to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters....
Write a python program that asks the user to enter a string containing numbers separated by...
Write a python program that asks the user to enter a string containing numbers separated by commas, e.g., s = '1.23,2.4,3.123', Your program should then calculate and print the sum of the numbers entered. Hint: you need to iterate over the string searching for the commas, i.e. their index. The first number is obtained by slicing between the start of the string and the index of the first comma. The second number is between the last comma and the next...
You are given a sample containing a hydrocarbon and an amine - both compounds are very...
You are given a sample containing a hydrocarbon and an amine - both compounds are very insoluble in water and very soluble in diethyl ether. Describe how you could efficiently separate these two compounds using extraction methods; clearly explain why your procedure should give a good separation of the components (i.e., both compounds isolated in good yield and purity).
Language C: Suppose you are given a file containing a list of names and phone numbers...
Language C: Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT