Bottom Page

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
Quote
#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.
Quote
#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.
Quote
#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.
Quote
#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.
Quote
#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.
Quote
#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" 
Quote
#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.
Quote
#9
perfringo Wrote:what should happen with string 'abc'
I think 'abc' --> 'a'
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Occurence Adem 3 152 Nov-08-2019, 03:17 PM
Last Post: Adem
  Minimum size Amniote 8 424 Jul-10-2019, 02:58 PM
Last Post: nilamo
  Armstrong in minimum lines Gaurav 1 477 Sep-03-2018, 03:46 PM
Last Post: j.crater
  Python find the minimum length of string to differentiate dictionary items zydjohn 3 1,033 Mar-03-2018, 05:23 PM
Last Post: Gribouillis
  Product of maximum in first array and minimum in second Thethispointer 9 1,159 Jan-19-2018, 07:38 PM
Last Post: Thethispointer
  Can I delete single quotes in string matrix display sylas 2 1,008 Jul-22-2017, 10:18 AM
Last Post: sylas

Forum Jump:


Users browsing this thread: 1 Guest(s)