Last active
November 11, 2021 13:38
-
-
Save RaMSFT/cee80833495a371717413752d8b4a2a3 to your computer and use it in GitHub Desktop.
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
| import re | |
| import timeit | |
| def count_vowels_loop_over_vowels(text): | |
| vowel_count = 0 | |
| for vowel in vowels_list: | |
| count = text.count(vowel) | |
| vowel_count += count | |
| return vowel_count | |
| def count_vowels_loop_over_string(text): | |
| vowel_count = 0 | |
| for char in text: | |
| if char in vowels_list: | |
| vowel_count += 1 | |
| return vowel_count | |
| def count_vowels_map(text): | |
| return sum([*map(text.count, vowels_list)]) | |
| def count_vowels_list_comprehension(text): | |
| return sum([text.count(x) for x in 'aeiou']) | |
| def count_vowels_regex(text): | |
| return len(re.findall('a|e|i|o|u', text)) | |
| def count_vowels_regex_sub(text): | |
| total_text_length = len(text) | |
| vowel_replaced_string = re.sub("[aeiou]", "", text) | |
| replaced_string_length = len(vowel_replaced_string) | |
| return total_text_length - replaced_string_length | |
| def count_vowels_regex_subn(text): | |
| return re.subn('[aeiou]', "", text)[1] | |
| vowels_list = ['a', 'e', 'i', 'o', 'u'] | |
| fp = open('input_text.txt','r') | |
| user_input = fp.read().replace('\n','').lower() | |
| total_char = (len(user_input)) | |
| print(f"Function Name,Execution Time,Total Char, Number of Vowels") | |
| function_ist = ['count_vowels_loop_over_vowels','count_vowels_map','count_vowels_list_comprehension', 'count_vowels_regex','count_vowels_regex_sub', | |
| 'count_vowels_regex_subn','count_vowels_loop_over_string'] | |
| for function_name in function_ist: | |
| start_time = timeit.default_timer() | |
| result = eval(f"{function_name}('{user_input}')") | |
| end_time = timeit.default_timer() | |
| print(f"{function_name},{end_time - start_time},{total_char},{result}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment