Skip to content

Instantly share code, notes, and snippets.

@tiagodavi
Last active December 28, 2023 20:46
Show Gist options
  • Save tiagodavi/16a7afa42a44cc79e0ebbbcb385ac587 to your computer and use it in GitHub Desktop.
Save tiagodavi/16a7afa42a44cc79e0ebbbcb385ac587 to your computer and use it in GitHub Desktop.
count total of subsequent string in a row
# The goal is to count the frequency of subsequent strings in a row.
def count_subsequent_str(str):
str = str.lower()
temp = {}
for i in range(len(str) - 1):
if not str[i] in temp:
temp[str[i]] = 0
if str[i] == str[i+1] and temp[str[i]] < 1:
temp[str[i]] += 2
elif str[i] == str[i+1] and temp[str[i]] > 1:
temp[str[i]] += 1
return temp
if __name__ == '__main__':
result = count_subsequent_str("trial and error")
print(result)
# {'t': 0, 'r': 2, 'i': 0, 'a': 0, 'l': 0, ' ': 0, 'n': 0, 'd': 0, 'e': 0, 'o': 0}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment