Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Demonstrations of map and filter

# Some useful helper functions
def first(list):
  return list[0]

def rest(list):
  return list[1:]

def empty(list):
  return (len(list) == 0)

def add(a, b):
  return a+b

# A list to run tests on.
# (BTW, all the cheeses mentioned in the Monty Python Cheese-shop sketch)
cheeses = ["Bel Paese", "Boursin", "Bresse-Bleu", "Brie",
"Caerphilly", "Caithness", "Camenbert", "Carre-de-L'Est", "Cheddar",
"Cheshire", "Czechoslovakian Sheep's Milk Cheese",
"Danish Blue", "Danish Fynbo", "Dorset Blue Vinney", "Double Gloucester",
"Edam", "Emmental",
"Gorgonzola", "Gouda",
"Ilchester",
"Lancashire", "Limberger", "Liptauer",
"Mozzarella",
"Norwegian Jarlsberger",
"Parmesan", "Perle de Champagne", "Pippo Creme", "Pont-l'Eveque", "Port Salut",
"Red Windsor", "Roquefort", 
"Sage Derby", "Saint-Paulin", "Savoyard", "Smoked Austrian", "Stilton",
"Tilsit",
"Venezuelan Beaver Cheese",
"Wensleydale", "White Stilton"]

nonCheeses = ["The Single Most Popular Cheese in the World", "Not Around These Parts"]

#-----------------------------------------------------------------------------

# Produce a list that has as many elements as there are in the list of cheeses
# but with the lengths of the names, not the names themselves.
# So, if the first elements of cheeses are ["Bel Paese", "Boursin",....]
# The resulting list should start [9, 7, ....]

def demoMap(yesOrNo):
  if yesOrNo == 1:
    return stringLengthsUsingMap(cheeses)
  else:
    return stringLengthsUsingLoop(cheeses)

def stringLengthsUsingLoop(list):
  listOfLengths = []
  for s in list:
    listOfLengths.append(len(s))
  return listOfLengths

def stringLengthsUsingMap(list):
  return map(len, list)

#-----------------------------------------------------------------------------

# Produce a filtered list containing only those cheeses with multi-word names.

def demoFilter(yesOrNo):
  if yesOrNo == 1:
    return multiWordsUsingFilter(cheeses)
  else:
    return multiWordsUsingLoop(cheeses)

def multiWordsUsingLoop(list):
  multiWordList = []
  for s in list:
    if containsSeparator(s):
      multiWordList.append(s)
  return multiWordList

def multiWordsUsingFilter(list):
  return filter(containsSeparator, list)

# Helper functions...
def containsSeparator(s):
  return (hasSpace(s) or hasApostrophe(s) or hasHyphen(s))

def hasSpace(s):
  return s.find(' ') != -1

def hasApostrophe(s):
  return s.find("'") != -1

def hasHyphen(s):
  return s.find('-') != -1

#-----------------------------------------------------------------------------

# The total number of characters in a list of words
def demoReduce(yesOrNo):
  if yesOrNo == 1:
    return totalNumCharactersUsingReduce(cheeses)
  else:
    return totalNumCharactersUsingLoop(cheeses)

def totalNumCharactersUsingLoop(wordList):
  totalNumOfChars = 0
  for word in wordList:
    totalNumOfChars += len(word)
  return totalNumOfChars

def comesBefore(a, b):
  return (a <= b)

def isSortedUsingReduce(list):
  return reduce(comesBefore, list)


Link to this Page