Python Forum
Delete minimum occurence in a string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Delete minimum occurence in a string (/thread-22401.html)

Pages: 1 2


Delete minimum occurence in a string - RavCOder - Nov-11-2019

Hi,
I have to delete minimum number of character in a string.
I saw that there is Counter but I don't know how to use it.
This is my code for now:
word  = "abecedario alpaca"
char = ''
word_list = []
#add a string into a list 
for c in word:
  word_list.append(c)
#print(word_list)
#a counter that check if inside a list there is a char 
count = 0
for counter in word_list: #for any elements of list --> is there char? 
  if counter == char:
    count = count + 1 #counter 

print(count)
This is text :
Quote:Given a string s consisting of n lowercase letters, you have to delete the minimum number of characters from s so that every letter in s appears a unique number of times. We only care about the occurrences of letters that appear at least once in result.
Regards,
RavCoder



RE: Delete minimum occurence in a string - jefsummers - Nov-11-2019

Key here is "unique number of times". And, you are to delete the least number of characters possible. I know how I would approach, but interested in your overall plan for the script.


RE: Delete minimum occurence in a string - Gribouillis - Nov-11-2019

Take the imaginary word "pneumonaoultramicroscoapicsailaicovolcanoconiosillls". In this word, the letters a, l, c, i appear 6 times. Let R be the resulting word after you have removed the necessary characters. In this resulting word, the letters a, l, c, i appear at most 6 times and each a different number of times. It doesn't matter which of these 4 characters appears more or less than another because it doesn't change the count of removed characters if we choose to remove 3 a's or 3 i's. So we can as well suppose that we need to remove at least 1 l, 2 c's and 3 i's.

You can repeat this procedure as long as there are characters that appear the same number of times.


RE: Delete minimum occurence in a string - jefsummers - Nov-11-2019

So a-l-c-i will be (in any order) 6-5-4-3 leaving only 2 and 1 for the remaining letters. The source word must be chosen carefully.

Bottom line - counting the incidence of the letters is just the beginning.


RE: Delete minimum occurence in a string - Gribouillis - Nov-11-2019

jefsummers Wrote:So a-l-c-i will be (in any order) 6-5-4-3 leaving only 2 and 1 for the remaining letters.
No, the procedure is repeated as long as there are letters having the same number of occurrences.


RE: Delete minimum occurence in a string - perfringo - Nov-12-2019

"you have to delete the minimum number of characters from s so that every letter in s appears a unique number of times" makes me wonder what should happen with string 'abc'


RE: Delete minimum occurence in a string - RavCOder - Nov-12-2019

I don't know exactly how to do it, but I think I have to declare the word I want to search first in
 char = "character that I want to search" 



RE: Delete minimum occurence in a string - perfringo - Nov-12-2019

Another question - is non-printable character in context of this assignment a character?

Using string in original post:

>>> import collections
>>> word  = "abecedario alpaca"
>>> counts = collections.Counter(word)
>>> counts
Counter({'a': 5, 'e': 2, 'c': 2, 'b': 1, 'd': 1, 'r': 1, 'i': 1, 'o': 1, ' ': 1, 'l': 1, 'p': 1})
How should one interpret the constraint: 'We only care about the occurrences of letters that appear at least once in result.'.


RE: Delete minimum occurence in a string - Gribouillis - Nov-12-2019

perfringo Wrote:what should happen with string 'abc'
I think 'abc' --> 'a'


RE: Delete minimum occurence in a string - jefsummers - Nov-12-2019

The assignment uses the term "letters", so although wrong to assume, I would think only letters count.
Understanding the answer and process first, then coding,
Using the string in the original post and the counts as in perfringo's post, you could have 5 a's, 2 e's, 1 c and that's it. Everything else would be deleted as the count would duplicate (alternative answer 5 a's, 2 c's, 1 e).

If we agree that this is the answer, then can advise regarding the coding.