Reputation: 81
I want to to display a number or an alphabet which appears mostly consecutive in a given string or numbers or both.
Example:
s= 'aabskeeebadeeee'
output: e appears 4 consecutive times
I thought about set the string then and for each element loop the string to check if equal with element set element if so count =+1 and check if next to it is not equal add counter value to list with same index as in set, if is add counter value to li list if value is bigger than existing.
The problem is error index out or range although I think I am watching it.
s = 'aabskeeebadeeee'
c = 0
t = list(set(s)) # list of characters in s
li=[0,0,0,0,0,0] # list for counted repeats
print(t)
for x in t:
h = t.index(x)
for index, i in enumerate(s):
maximus = len(s)
if i == x:
c += 1
if index < maximus:
if s[index +1] != x: # if next element is not x
if c > li[h]: #update c if bigger than existing
li[h] = c
c = 0
else:
if c > li[h]:
li[h] = c
for i in t:
n = t.index(i)
print(i,li[n])
print(f'{s[li.index(max(li))]} appears {max(li)} consecutive times')
Upvotes: 2
Views: 608
Reputation: 31640
This answer was posted as an edit to the question Check how many consecutive times appear in a string by the OP Ziggy Witkowski under CC BY-SA 4.0.
I did not want to use any libraries.
s = 'aabskaaaabadcccc' lil = tuple(set(s)) # Set a characters in s to remove duplicates and then make a tuple li=[0,0,0,0,0,0] # list for counted repeats, the index of number repeats for character # will be equal to index of its character in a tuple for i in lil: #iter over tuple of letters c = 0 #counter h= lil.index(i) #take an index for letter in s: #iterate ove the string characters if letter == i: # check if equal with character from tuple c += 1 # if equal Counter +1 if c > li[lil.index(letter)]: # Updated the counter if present is bigger than the one stored. li[lil.index(letter)] = c else: c=0 continue m = max(li) for index, j in enumerate(li): #Check if the are characters with same max value if li[index] == m: print(f'{lil[index]} appears {m} consecutive times')
Output:
c appears 4 consecutive times a appears 4 consecutive times
Upvotes: 0
Reputation: 4980
Alternatively, you can leverage the power of itertools.groupby()
to approach this type of problem (for quick counting for similar items in groups
. [Note, this can be applied to some broader cases, eg. numbers]
from itertools import groupby
>>> char_counts = [str(len(list(g)))+k for k, g in groupby(s)]
>>> char_counts
['2a', '1b', '1s', '1k', '3e', '1b', '1a', '1d', '4e']
>>> max(char_counts)
'4e'
# you can continue to do the rest of splitting, or printing for your needs...
>>> ans = '4e' # example
>>> print(f' the most frequent character is {ans[-1]}, it appears {ans[:-1]} ')
Output: the most frequent character is e, it appears 4
Upvotes: 0
Reputation: 22370
Here is an O(n)
time, O(1)
space solution, that breaks ties by returning the earlier seen character:
def get_longest_consecutive_ch(s):
count = max_count = 0
longest_consecutive_ch = previous_ch = None
for ch in s:
if ch == previous_ch:
count += 1
else:
previous_ch = ch
count = 1
if count > max_count:
max_count = count
longest_consecutive_ch = ch
return longest_consecutive_ch, max_count
s = 'aabskeeebadeeee'
longest_consecutive_ch, count = get_longest_consecutive_ch(s)
print(f'{longest_consecutive_ch} appears {count} consecutive times in {s}')
Output:
e appears 4 consecutive times in aabskeeebadeeee
Upvotes: 2
Reputation: 521279
Regex offers a concise solution here:
inp = "aabskeeebadeeee"
matches = [m.group(0) for m in re.finditer(r'([a-z])\1*', inp)]
print(matches)
matches.sort(key=len, reverse=True)
print(matches[0])
This prints:
['aa', 'b', 's', 'k', 'eee', 'b', 'a', 'd', 'eeee']
eeee
The strategy here is to find all islands of similar characters using re.finditer
with the regex pattern ([a-z])\1*
. Then, we sort the resulting list descending by length to find the longest sequence.
Upvotes: 0