""" Author: Sohie Lee Consulted: Purpose: Lab 6, Practice with lists of strings Filename: listPractice1.py """ # importing these lists from files in the lab06 folder # so we can use them for testing from wordLists import * from sampleLists import * from vocabulary import englishwords from songLyrics import * def wordsStartWithA(words): """ Returns a count of how many words in the given list start with 'A' """ count = 0 # Accumulator for w in words: if w[0] == 'a': count = count + 1 return count def averageLength(words): """ Returns the average word length for the given list of words """ sum = 0.0 # floating point for word in words: sum += len(word) return round(sum/len(words), 2) def countYou(words): """ Returns a count of how many times the word "you" appears in the given list of words Note: This version will count both lower-case and upper-case instances of the word (e.g., 'You' 'YOU', and 'yoU' will all be counted). """ count = 0 for word in words: if word.lower() == 'you': count = count + 1 return count def wordsWithLess(words): """ Returns a list of words that contain the letters "less" """ results = [] for word in words: if 'less' in word: results.append(word) return results def testWordsWithLess(theList): print (wordsWithLess(theList)) def word1ContainsLettersOfWord2(word1, word2): """ Returns Boolean if all the letters of word2 exists in word1 Helper function used in wordsContainAllChars """ for letter in word2: if letter not in word1: return False return True def wordsContainAllChars(words, string): """ Returns a list of words that contains all the letters in the given `string` """ keeper = [] for w in words: if word1ContainsLettersOfWord2(w,string): keeper.append(w) return keeper def longestWords(words): """ Returns the longest words in a given list Version 1.0 Takes two for loops to produce the longest words list """ # First, determine length of longest words... # Assume 0 to start lengthOfLongest = 0 for word in words: if len(word) > lengthOfLongest: lengthOfLongest = len(word) # Then, create list of words that have longest length... results = [] for word in words: if (len(word) == lengthOfLongest) and (word not in results): results.append(word) return results def longestWords(wlist): """ Returns the longest words in a given list Version 2.0 Takes a single for loop to produce the longest words list """ longest = 0 # the length of the longest word seen so far keepers = [] # the list of the longest words for w in wlist: if len(w) > longest: # found a new longest word longest = len(w) keepers = [] keepers.append(w) elif len(w) == longest and (word not in keepers): # found a word that matches the longest length that is not already in the list keepers.append(w) return keepers # Testing code below here def test(): print ("\n\nTest wordsStartWithA:") print (wordsStartWithA(smallWordList)) # 65 print (wordsStartWithA(loveStoryLyricList)) # 27 print (wordsStartWithA(englishwords)) # 3685 print (wordsStartWithA(beautifulLyricList)) # 17 print (wordsStartWithA(imSoFancyLyricList)) # 17 print ("\n\nTest averageLength:") print (averageLength(imSoFancyLyricList)) # 4.06 print (averageLength(beautifulLyricList)) # 4.03 print (averageLength(beforeHeCheatsLyricList)) # 4.27 print (averageLength(loveStoryLyricList)) # 3.87 print (averageLength(thriftShopLyricList)) # 4.44 print (averageLength(iGetByWithALittleHelpLyricList)) # 3.66 print (averageLength(tinyWordList)) # 5.7 print (averageLength(smallWordList)) # 6.08 print (averageLength(englishwords)) # 8.37 print ("\n\nTest countYou:") print (countYou(["you", "probably", "think", "this", "song", "is", "about", "you"])) # 2 print (countYou(imSoFancyLyricList)) # 17 print (countYou(beautifulLyricList)) # 43 print (countYou(beforeHeCheatsLyricList)) # 1 print (countYou(loveStoryLyricList)) # 16 print (countYou(thriftShopLyricList)) # 5 print (countYou(iGetByWithALittleHelpLyricList)) # 10 print ("\n\nTest wordsWithLess:") print (wordsWithLess(['careless', 'careful'])) # ['careless'] print (wordsWithLess(smallWordList)) # ['painless'] print (wordsWithLess(englishwords)) # Many words ['ageless', ..., 'worthlessness'] print (len(wordsWithLess(englishwords))) # 401 print (wordsWithLess(beforeHeCheatsLyricList)) # [] print ("\n\nTest wordsContainAllChars:") print (wordsContainAllChars(["ab", "abc", "cba", "abracadabra", 'xyz'], "abc")) # ['abc', 'cba', 'abracadabra'] print (wordsContainAllChars(smallWordList, "pam")) # 10 words: ['amorphous', ..., 'temptation'] print (wordsContainAllChars(loveStoryLyricList, "love")) # ['love', 'love', 'love', 'love', 'love', 'love'] print (wordsContainAllChars(thriftShopLyricList, "ough")) # ['bought', 'bought', 'bought', 'through', 'dough', 'through'] print (wordsContainAllChars(englishwords, "evrngast")) # 29 words: ['advertising', ... ,'vegetarians'] print ("\n\nTest longestWords:") print (longestWords(["apple", "bananna", "pear", "coconut", "orange"])) # ['bananna', 'coconut'] print (longestWords(imSoFancyLyricList)) # ['department', 'Chandelier', 'expensive,'] print (longestWords(beautifulLyricList)) # ['overwhelmed', 'desperately'] print (longestWords(beforeHeCheatsLyricList)) # ['bleached-blond'] print (longestWords(loveStoryLyricList)) # ['http://www.elyrics.net'] print (longestWords(thriftShopLyricList)) # ['hand-me-downs?'] print (longestWords(iGetByWithALittleHelpLyricList)) # ['Ah-ah-ah-ah-ah-ah-ah-ah-ah'] print (longestWords(englishwords)) # ['electroencephalographs']