Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Say that the user inputs:

"daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm"

How would you go about finding the highest number of consecutive "a" and how would you remove the "a"'s and leave only 2 of them instead of the large number of them before.

I was thinking of appending each letter into a new empty list but i'm not sure if that's correct or what to do after.

I really don't know where to begin with this one but this is what i'm thinking:

  1. Ask the user for input.
  2. Create an empty list
  3. Append each letter from the input into the list

What's next I have no idea.

second edit (something along these lines):

sentence = input("Enter your text: ")
new_sentance = " ".join(sentence.split())
length = len(new_sentance)
alist = []
while (length>0):
    alist
print ()
like image 756
dkentre Avatar asked Sep 13 '13 00:09

dkentre


People also ask

How do you find the highest consecutive numbers in Python?

To get the max number, you would use max(len(s) for s in re. findall(r'a+', inputString)) . To replace all occurrences of more than 2 "a"s with 2 "a"s, you would use: output = re.

How do you count consecutive occurrences of a character in a string in python?

Given a String, extract all the K-length consecutive characters. Input : test_str = 'geekforgeeeksss is bbbest forrr geeks', K = 3 Output : ['eee', 'sss', 'bbb', 'rrr'] Explanation : K length consecutive strings extracted.

How do you check for consecutive 1 in Python?

Python makes it easy by having enumerate(seq) . This will give you a tuple (counter, value) . That way you have direct access to the counter and can use that to check the counter + 1 value as well. Or you can iterate through the loop using range (0 to len of seq - 1).


2 Answers

Starting with the input string:

input = "daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm"
  • To get the max consecutive number of occurrences, you would use:

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

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

    First, I obtain an iterable of MatchObjects by testing the input string against the regex a+, then use max to obtain the MatchObject with the greatest length. Then, I splice the portion of the original string up to the start of the match, the string "aa", and the portion of the original string after the end of the match to give you your final output.

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

    output = re.sub(r'a{3,}', "aa", input)
    
like image 138
Asad Saeeduddin Avatar answered Oct 08 '22 16:10

Asad Saeeduddin


A lower level approach if you don't 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
like image 45
japreiss Avatar answered Oct 08 '22 16:10

japreiss