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

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"
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)
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
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 } }
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Python Write a program that will analyse the string input and print “accept” or “reject” based...
Python Write a program that will analyse the string input and print “accept” or “reject” based on the pattern given Accept if it fulfils the following conditions -String length 9 -3 small alphabet (3 lowercase letter) -3 digits -3 big alphabet (3 uppercase letters) -1st alphabet should be a capital -Last alphabet should be a number -Two consecutive alphabets can't be small Reject if any of the conditions is absent So i want it to accept or reject my input,...
Python Assume s is a string of numbers. Write a program that prints the longest substring...
Python Assume s is a string of numbers. Write a program that prints the longest substring of s in which the numbers occur in ascending order and compute the average of the numbers found. For example, if s = '561984235272145785310', then your program should print: Longest substring in numeric ascending order is: 14578 Average: 5 In the case of ties, print the first substring. For example, if s = '147279', then your program should print Longest substring in numeric ascending...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT