""" Author: Sohie Lee Consulted: Purpose: Lab 6, More list practice with filtering and mapping Filename: listPractice2.py """ from sampleLists import * def filterDrop(needle, haystack): """ Given a list of words (`haystack`), returns a list of words excluding the `needle` """ results = [] for word in haystack: if word != needle: results.append(word) return results def filterLength(maxLength, wordList): """ Given a list of pairs, returns a list where the sum of the length of both names is less than maxLength """ results = [] for word in wordList: if (len(word[0]) + len(word[1])) <= maxLength: results.append(word) return results def mapPluralize(list): """ Given a list of words, returns list of those words pluralized Does not handle special cases like "kiss" """ results = [] for s in list: s = s + "s" results.append(s) return results def test(): print ("\n\nTest filterDrop:") print (filterDrop('James', skyfall)) # ['M','Q','Moneypenny','JamesBond','Bond','Silva','Patrice'] print (filterDrop('Q', skyfall)) # ['M','Moneypenny','James','JamesBond','Bond','Silva','Patrice'] print ("\n\nTest filterLength:") print (filterLength(8,roster)) # [['Yoyo','Ma'],['Sohie','Lee']] print (filterLength(10,roster)) # [['Yoyo','Ma'],['Sohie','Lee'],['Jean','Herbst'],['Santa','Claus']] print (filterLength(12,roster)) # [['Yoyo','Ma'],['Sohie','Lee'],['Jean','Herbst'], # ['Brian','Tjaden'],['Santa','Claus'],['Happy','Camper'], # ['Harry','Styles'],['Taylor','Swift']] print ("\n\nTest mapPluralize:") print (mapPluralize(['assignment'])) # ['assignments'] print (mapPluralize(['donut','muffin','bagel'])) # ['donuts','muffins','bagels'] print (mapPluralize(['tree','witch','kiss','moose', 'alpaca'])) # ['trees','witchs','kisss', 'mooses','alpacas']