Python Forum
Delete minimum occurence in a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete minimum occurence in a string
#1
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
Reply
#2
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.
Reply
#3
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.
Reply
#4
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.
Reply
#5
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.
Reply
#6
"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'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
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" 
Reply
#8
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.'.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
perfringo Wrote:what should happen with string 'abc'
I think 'abc' --> 'a'
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 1,257 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
Thumbs Up [SOLVED] Find last occurence of pattern in text file? Winfried 4 2,171 Aug-13-2021, 08:21 PM
Last Post: Winfried
  How to get indices of minimum time difference Mekala 1 1,467 Nov-10-2020, 11:09 PM
Last Post: deanhystad
  How to get index of minimum element between 3 & 8 in list Mekala 2 1,757 Nov-10-2020, 12:56 PM
Last Post: DeaD_EyE
  Delete lines from a string that contains a keyword JellyCreeper6 4 1,481 Oct-30-2020, 10:46 AM
Last Post: JellyCreeper6
  Finding MINIMUM number in a random list is not working Mona 5 2,025 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  Occurence Adem 3 1,782 Nov-08-2019, 03:17 PM
Last Post: Adem
  Minimum size Amniote 8 2,598 Jul-10-2019, 02:58 PM
Last Post: nilamo
  Armstrong in minimum lines Gaurav 1 1,518 Sep-03-2018, 03:46 PM
Last Post: j.crater
  Watson Personality Insight: minimum number of words kiton 0 2,167 Apr-28-2018, 03:28 PM
Last Post: kiton

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020