Count the number of max consecutive a's from a string. Python 3

Say the user enters:

"daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm" 

How would you find the maximum number of consecutive "a's" and how would you delete the "a" and leave only 2 of them, and not a large number of them before.

I was thinking about adding each letter to a new empty list, but I'm not sure if this is right or what to do after.

I really don't know where to start, but this is what I think about:

  • Ask the user to enter.
  • Create an empty list
  • Add each letter from input to the list

What next, I have no idea.

second edit (something on these lines):

 sentence = input("Enter your text: ") new_sentance = " ".join(sentence.split()) length = len(new_sentance) alist = [] while (length>0): alist print () 
+7
python sorting count
source share
5 answers

Starting from the input line:

 input = "daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm" 
  • To get the maximum consistent number of occurrences, you should use:

     max(len(s) for s in re.findall(r'a+', input)) 
  • To replace only the longest continuous sequence "a" s with 2 "a" s, you should use:

     maxMatch = max(re.finditer(r'a+', input), key= lambda m: len(m.group())) output = input[:maxMatch.start()] + "aa" + input[maxMatch.end():] 

    First, I get MatchObject MatchObject by checking the input string for regex a+ , then use max to get the MatchObject with the longest. Then I combine the portion of the original string before the match, the string “aa” and the portion of the original string after the match to give you the final result.

  • To replace all occurrences with more than 2 "a with s" s, you will use:

     output = re.sub(r'a{3,}', "aa", input) 
+4
source share

A lower level approach if you do not want to use regular expressions.

 def count_and_reduce(s, a): num = 0 maxnum = 0 out = '' for c in s: if c == a: num += 1 maxnum = max(num, maxnum) else: num = 0 if num <= 2: out += c return maxnum, out 
+3
source share

I saw a couple of regular expression answers in the comments and another question, so I'm going to go a different way. Just getting an account can be done in many ways.

 from itertools import groupby inp = 'daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm'; char_groups = groupby(inp, lambda char:char=='a') counts = [len(list(group)) for char, group in char_groups] # We know every other element of 'counts' is an 'a' element. # We just need to know whether to start at zero or one. # If inp starts with 'a', start at 0. Otherwise start at 1. max(counts[not inp.startswith('a')::2]) # 11 

I am sure that both of the regular expression answers I saw will replace each line of 'aa +' with two 'a'. If you want to replace the longest string 'a with' aa 'and leave the rest alone:

 char_groups = groupby(inp) counts = [(char, len(list(group))) for char, group in char_groups] max_idx = max(range(len(counts)), key=lambda i:counts[i][1] if counts[i][0]=='a' else 0) result = ''.join(char*count for char, count in counts[:max_idx]) + 'aa' + ''.join(char*count for char, count in counts[max_idx+1:]) # 'daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaafnnasm' 
+2
source share

How do i do that.

 s = "daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm" print(s) a_len = len(s) found_a_len = 0 keep_going = True while a_len>0 and keep_going: aas = "a" * a_len if aas in s: found_a_len = a_len keep_going = False a_len=a_len -1 print ("max length of a:" , found_a_len) keep_going = True while keep_going: s=s.replace("aaa","aa") if "aaa" not in s: keep_going = False print(s) 

these outputs:

 daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm max length of a: 11 daslakndlaajnjndibniaafijdnfijdnsijfnsdinifaafnnasm 

Some people may not like my coding style, but for me this code is very easy to reason about.

+2
source share

Try the following:

 import collections def runLenEncode(s): start, i = 0, 0; answer = [] while i<len(s): while i<len(s) and s[start]==s[i]: i += 1 answer.append((s[start], i-start)) start = i return answer def runLenFilter(encoding, thresholds, repLens): answer = [] for char, count in encoding: if char in thresholds and count>=thresholds[char]: count = repLens[char] answer.append(char*count) return ''.join(answer) def maxFilter(encoding, repLens): maxCounts = collections.defaultdict(int) for char, count in encoding: if char in repLens and count > maxCounts[char]: maxCounts[char] = count maxCounts = dict(maxCounts) answer = [] for char, count in encoding: if char in repLens and count==maxCounts[char]: count = repLens[char] answer.append(char*count) return ''.join(answer) if __name__ == "__main__": print('starting') s = "daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm" encoding = runLenEncode(s) print("encoding:", encoding) thresholds = {'a':3} repLens = {'a':2} decoded = runLenFilter(encoding, thresholds, repLens) print('lenFilter:', decoded) filtered = maxFilter(encoding, repLens) print("maxFilter:", filtered) print('done') 

And the conclusion :

 $ python3 myFile.py starting encoding: [('d', 1), ('a', 1), ('s', 1), ('l', 1), ('a', 1), ('k', 1), ('n', 1), ('d', 1), ('l', 1), ('a', 5), ('j', 1), ('n', 1), ('j', 1), ('n', 1), ('d', 1), ('i', 1), ('b', 1), ('n', 1), ('i', 1), ('a', 3), ('f', 1), ('i', 1), ('j', 1), ('d', 1), ('n', 1), ('f', 1), ('i', 1), ('j', 1), ('d', 1), ('n', 1), ('s', 1), ('i', 1), ('j', 1), ('f', 1), ('n', 1), ('s', 1), ('d', 1), ('i', 1), ('n', 1), ('i', 1), ('f', 1), ('a', 11), ('f', 1), ('n', 2), ('a', 1), ('s', 1), ('m', 1)] lenFilter: daslakndlaajnjndibniaafijdnfijdnsijfnsdinifaafnnasm maxFilter: daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaafnnasm done 
0
source share

All Articles