Last active
July 13, 2022 13:28
-
-
Save TERNION-1121/d0e19fde5033cd94b2c8b4bb89a8d1a7 to your computer and use it in GitHub Desktop.
Algorithm that would take one string and one list ( of single characters ) from the user and would display the all occurrences of all the characters(given in the list) in the string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_indices(s, lst): | |
| indice = list() | |
| indices = list() | |
| toReturn = '' | |
| for char in lst: | |
| for i in range(len(s)): | |
| if s[i] == char: | |
| indice.append(i) | |
| indices.append(indice) | |
| indice = [] | |
| for i in range(len(lst)): | |
| toReturn+=f"Appearances of '{lst[i]}': " | |
| for index in indices[i]: | |
| toReturn+=f'{index}, ' | |
| toReturn = toReturn[:-2] + '\n' | |
| return toReturn[:-1] | |
| # Test | |
| print(get_indices('lol you comedy me? you better not as i am a chad', ['o', 'e', 'a'])) | |
| # Output | |
| # Appearances of 'o': 1, 5, 9, 20, 31 | |
| # Appearances of 'e': 11, 16, 24, 27 | |
| # Appearances of 'a': 34, 39, 42, 46 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment