Question

In: Computer Science

IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...

IN PYTHON

Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once.

Suggested Approach

  • Used two nested for loops.
  • The first for loop iterates from 0 to range(len(input_str)).
  • The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop is that the outer loop already checks the current character. In the nested inner loop, you want to check the remaining characters in the input string.
  • If the current character of the outer loop matches the character of the nested loop AND the character is not already IN the duplicate list, append it to the list
  • nhantran

    Sample Output 1

    List of duplicate chars: ['n', 'a']

    Sample Input 2

    pneumonoultramicroscopicsilicovolcanoconiosis

    Sample Output 2

    List of duplicate chars: ['p', 'n', 'u', 'm', 'o', 'l', 'r', 'a', 'i', 'c', 's']

    Sample Input 3

    pulchritudinous

    Sample Output 3

    List of duplicate chars: ['u', 'i']

    Sample Input 4

    orden

    Sample Output 4

    List of duplicate chars: []

Solutions

Expert Solution

Here is the Python code to the given question.

Four sample outputs are added at the end.

Code:


inputString=input("Enter the string:")    #stores input string entered by the user

duplicateList=[]    #creates empty list to store duplicate chars

for i in range(len(inputString)):     #runs loop from i=0 to last element of string
    
    for j in range(i+1,len(inputString)):      #runs loop from j=i to last element of string
        
        if inputString[i]==inputString[j] and inputString[i] not in duplicateList:     #checks if two characters are equal and duplicate char is not already present in duplicateList
            
            duplicateList.append(inputString[i])      #append the present char to duplicateList
            
            break      #exit from inner loop
    
print("List of duplicate chars: ",duplicateList)     #prints the duplicate list

Sample Output-1:

Sample Output-2:

Sample Output-3:

Sample Output-4:


Related Solutions

Write a Python program, phone.py, that inputs a string of characters which represent a vanity telephone...
Write a Python program, phone.py, that inputs a string of characters which represent a vanity telephone number, e.g., 800-MYPYTHON, and prints the all numeric equivalent, 800-69798466. You should implement this using a loop construct to process each character from left to right. Build a new string that is the all numeric equivalent and then print the string.
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal...
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal to n, such that the digit to the right is greater than the left digit (ai+1 > ai). E.g. if n=3 (123,124,125,……129,234,…..789)
Using python: Given a string x, write a program to check if its first character is...
Using python: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
Write a C program with a struct that contains a string of 12 characters and two...
Write a C program with a struct that contains a string of 12 characters and two integers: number1 and number2. Read all 3 values in from the keyboard using scanf. Print the string and the sum of the two integers using the fully qualified struct names.
write a python code that Returns a string composed of characters drawn, in strict alternation, from...
write a python code that Returns a string composed of characters drawn, in strict alternation, from s1 and s2. If one string is longer than the other, the excess characters are added to the end of the string as shown in the examples below #Example 1 - blend("ape", "BANANA") returns "aBpAeNANA" #Example 2 - blend("BOOT", "gold") returns "BgOoOlTd"
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
Given a string, write a method called removeRepthat returns another string where adjacent characters that are...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are the same have been reduced to a single character. Test the program by calling the method from the main method. For example: removeRep(“yyzzza”) à “yza” removeRep(“aabbbccd”) à “abcd” removeRep(“122333”) à “123”
In c++, using stack structure, write a program that will take a sequence of characters (string)...
In c++, using stack structure, write a program that will take a sequence of characters (string) and determine whether it is a palindrome. Use the linked version of the stack.
Write a C++ program which reads a string, less than 10 characters long. This string represents...
Write a C++ program which reads a string, less than 10 characters long. This string represents an integer expressed in roman numbers. Let a function convert the number from roman to arabic form (i.e., our standard digits). Let then the main program writes out both forms. The roman numbers are written according to: M = 1000, D = 500, C =100, L=50, X=10, V=5, I=1. Examples: LXXXVII = 87 CCXIX = 219 MCCCLIV = 1354 MMDCLXXIII = 2673
Given a string of at least 3 characters as input, if the length of the string...
Given a string of at least 3 characters as input, if the length of the string is odd return the character in the middle as a string. If the string is even return the two characters at the midpoint. -------------------------------------------------------------- public class Class1 { public static String midString(String str) {     //Enter code here } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT