Created
October 27, 2016 05:37
-
-
Save jjlumagbas/c7ea7f7b0e05012e4006656b0133b1a0 to your computer and use it in GitHub Desktop.
Examples of folds in Python
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
| # | |
| # fold | |
| def sum_nums(lst): | |
| sum = 0 | |
| for el in lst: | |
| sum = sum + el | |
| return sum | |
| def sum_strs(lst): | |
| sum = "" | |
| for el in lst: | |
| sum = sum + el | |
| return sum | |
| print(sum_nums([1, 2, 3])) | |
| print(sum_strs(["A", "B", "C"])) | |
| init = 0 | |
| def f(a, b): | |
| return a + b | |
| def fold(lst): | |
| acc = init | |
| for el in lst: | |
| acc = f(acc, el) | |
| return acc | |
| print(fold([1, 2, 3])) | |
| def ave_grade(lst): | |
| acc = 0 | |
| for el in lst: | |
| acc = f(acc, el) | |
| return acc / len(lst) | |
| print(ave_grade([80, 92, 65, 72])) # 77.25 | |
| # [fold] Create a function that finds the minimum of a list of numbers | |
| def min_of_2(a, b): | |
| if (a < b): | |
| return a | |
| else: | |
| return b | |
| def min_num(lst): | |
| acc = lst[0] | |
| for el in lst: | |
| acc = min_of_2(acc, el) | |
| return acc | |
| print(min_num([80, 92, 65, 72])) # 65 | |
| print(min_num(["A", "B", "C"])) # "A" | |
| # fold] Create a function that counts the number of vowels in a string | |
| def to_lower(ch): | |
| return ch.lower() | |
| def is_vowel(upper_ch): | |
| ch = to_lower(upper_ch) | |
| return ch == "a" or ch == "e" or ch == "i" or ch == "o" or ch == "u" | |
| def count_vowel(count, ch): | |
| if (is_vowel(ch)): | |
| return count + 1 | |
| else: | |
| return count | |
| def num_vowels(lst): | |
| count = 0 | |
| for el in lst: | |
| count = count_vowel(count, el) | |
| return count | |
| print(num_vowels("Jheno Astillero Mollejon")) # 9 | |
| # create a function that counts the number of gmail.com email addresses in a list | |
| def count_gmail(count, email): | |
| if (email[-10:] == "@gmail.com"): | |
| return count + 1 | |
| else: | |
| return count | |
| def num_gmail(lst): | |
| count = 0 | |
| for email in lst: | |
| count = count_gmail(count, email) | |
| return count | |
| print(num_gmail([ "[email protected]", "[email protected]", "[email protected]" ])) | |
| # Determine whether every name in the list begins with J | |
| def begins_with_j(acc, el): | |
| return acc and (el[0] == 'j' or el[0] == 'J') | |
| # if (acc == False): | |
| # return False | |
| # elif (el[0] == 'j' or el[0] == 'J'): | |
| # return True | |
| # else: | |
| # return False | |
| def all_begins_with_j(lst): | |
| acc = True | |
| for el in lst: | |
| acc = begins_with_j(acc, el) | |
| return acc | |
| print(all_begins_with_j(["jheno", "joshua", "janice"])) | |
| print(all_begins_with_j(["jheno", "nisha", "janice"])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment