Skip to content

Instantly share code, notes, and snippets.

@jjlumagbas
Created October 24, 2016 04:35
Show Gist options
  • Save jjlumagbas/c2f733a19f4a57f78e81ac7ef6ddca2a to your computer and use it in GitHub Desktop.
Save jjlumagbas/c2f733a19f4a57f78e81ac7ef6ddca2a to your computer and use it in GitHub Desktop.
Examples for map and filter
#
#
# fold
# map
# create a duplicate of the given list
def dup(lst):
result = []
for el in lst:
result = result + [el]
return result
print(dup([1, 2, 3]))
print(dup(["A", "B", "C"]))
# double each element of the list
def doubleEach(lst):
result = []
for el in lst:
result = result + [el * 2]
return result
print(doubleEach([1, 2, 3]))
print(doubleEach(["A", "B", "C"]))
# given a string, converts it to uppercase
def to_upper(ch):
return chr(ord(ch) - 32)
print(to_upper("a"))
print(to_upper("b"))
print(to_upper("c"))
print(to_upper("d"))
def uppercase(lst):
result = ""
for el in lst:
result = result + to_upper(el)
return result
print(uppercase("hello"))
print(uppercase("world"))
# given a list of names in lowercase,
# produce a list of names with the
# first character capitalized
def upper_first_ch(name):
return to_upper(name[0]) + name[1:]
print(upper_first_ch("jheno")) # Jheno
def cap_names(lst):
result = []
for el in lst:
result = result + [upper_first_ch(el)]
return result
print(cap_names(["jheno", "joshua", "janice"]))
def initials(lst):
result = []
for el in lst:
result = result + [el[0]]
return result
def initials2(lst):
result = ""
for el in lst:
result = result + el[0]
return result
print(initials2([ "Jedaiah", "Joel", "Lumagbas" ])) # "JJL"
# create a list of powers of two,
# given a list of exponents
def powers(lst):
result = []
for el in lst:
result = result + [2 ** el]
return result
print(powers([1, 2, 3])) # [2, 4, 8]
# filter
def dup_filter(lst):
result = []
for el in lst:
if (True):
result = result + [el]
return result
print(dup_filter([1, 2, 3]))
print(dup_filter(["A", "B", "C"]))
# filter all of the negative numbers in a list
def negs(lst):
result = []
for el in lst:
if (el < 0):
result = result + [el]
return result
print(negs([-1, 3, -6, 0, 1]))
# given a string, return the string without vowels
def is_vowel(ch):
return ch == "a" or ch == "e" or ch == "i" or ch == "o" or ch == "u"
def no_vowels(lst):
result = ""
for el in lst:
if (not is_vowel(el)):
result = result + el
return result
print(no_vowels("tumbler"))
# filters out all names that begin with "J"
def starts_with_j(name):
return name[0] == "J"
def no_j(lst):
result = []
for el in lst:
if (not starts_with_j(el)):
result = result + [el]
return result
print(no_j(["Nisha", "Monique", "Janice", "Joshua", "Dennis", "Jheno"]))
# ["Nisha", "Monique", "Dennis"]
# produces a list of powers of two
# given a list of exponents
# ignoring negative exponents
def powers_positive(lst):
result = []
for el in lst:
if (el >= 0):
result = result + [2 ** el]
return result
print(powers_positive([-1, 3, -6, 0, 1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment