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 a string, such as x = ‘itm330’, write a Python program to count the number...
Given a string, such as x = ‘itm330’, write a Python program to count the number of digits and the number of letters in it. For example, in ‘itm330’, there are 3 letters and 3 digits. Hint: Very similar to page 11 on the slides. To check if a character c is a digit, use c.isdigit(). If c is a digit, c.isdigit() will be a True.
Need a c++ program to generate a random string of letters between 8 and 16 characters.
Need a c++ program to generate a random string of letters between 8 and 16 characters.
Write a program that prints out the characters of a string each on a line and...
Write a program that prints out the characters of a string each on a line and counts these characters.  (Do not use the length function len() ).
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"
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”
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT