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

Write a function that will take a string containing only alphanumeric characters that are in lowercase...
Write a function that will take a string containing only alphanumeric characters that are in lowercase (if you think your logic requires you to use more than one argument, please go ahead). Your task is to see if the string becomes a palindrome if only one character is removed from anywhere in the string.
(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?
no dictionaries please Returns a new string which is lowercase version of the given word with...
no dictionaries please Returns a new string which is lowercase version of the given word with special characters and digits removed The returned word should not have any of the following characters: ! . ? : , ' " - _ \ ( ) [ ] { } % 0 1 2 3 4 5 6 7 8 9 tab character and new-line character >>> clean_word("co-operate.") 'cooperate' >>> clean_word("Anti-viral drug remdesivir has little to no effect on Covid patients' chances...
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 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....
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).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT