def countEvens(listOfInts): """ given a list of ints, counts even ints in list. Otherwise, returns False. yields 0 for empty list, or list of ints with no evens in it. >>> countEvens('1') False >>> countEvens(['a','b']) False >>> countEvens([]) 0 >>> countEvens([1,2,3,4,5]) 2 >>> countEvens([1]) 0 >>> countEvens([3,2]) 1 >>> countEvens([2,3,4]) 2 >>> """ result=0 if listOfInts == []: return 0 if not isList (listOfInts): return False for i in listOfInts: if not type(i) == int: return False if i%2 == 0: result=result+1 return result
def lengthOfEach(listOfStrings): """ given list of strings, returns list of ints correponding to length of each string, otherwise False. empty list yields empty list. >>> lengthOfEach('1') False >>> lengthOfEach(['a','b']) [1, 1] >>> lengthOfEach([]) [] >>> lengthOfEach(['Go','Gauchos']) [2, 7] >>> lengthOfEach(['x','xxx','xxxx']) [1, 3, 4] >>> """ if (not isList(listOfStrings)): return False result=[] for item in listOfStrings: if type(item)==str: result= result + [len(item)] return result
def isListOfIntegers(theList): """ indicates whether value of argument is a list of only int Note: empty list should return True---it doesn't contain anything that ISN'T int theList can be anything, and it will return either True or False. >>> isListOfIntegers('Fred') False >>> isListOfIntegers(3) False >>> isListOfIntegers([3]) True >>> isListOfIntegers([3.4]) False >>> isListOfIntegers([2,3,4,5.6,7]) False >>> isListOfIntegers([2,3,'oops',5]) False >>> isListOfIntegers([2,3,4,5,6,7]) True >>> isListOfIntegers([2,3,[4]]) False >>> isListOfIntegers([]) True """ if not (isList(theList)): return False for item in theList: if not type(item) == int: return False return True
def isListOfSimpleNumeric(theList): """ indicates whether value of argument is a list of only simple numerics (int or float) Note: empty list should return True---it doesn't contain anything that ISN'T simple numeric theList can be anything, and it will return either True or False. >>> isListOfSimpleNumeric('Fred') False >>> isListOfSimpleNumeric(3) False >>> isListOfSimpleNumeric([3]) True >>> isListOfSimpleNumeric([3.4]) True >>> isListOfSimpleNumeric([2,3,4,5.6,7]) True >>> isListOfSimpleNumeric([2,3,'oops',5]) False >>> isListOfSimpleNumeric([2,3,[4]]) False >>> isListOfSimpleNumeric([]) True """ if not isList(theList): return False for item in theList: if not isSimpleNumeric(item): return False return True
def countEvens(listOfInts): """ given a list of ints, counts even ints in list. Otherwise, returns False. yields 0 for empty list, or list of ints with no evens in it. >>> countEvens('1') False >>> countEvens(['a','b']) False >>> countEvens([]) 0 >>> countEvens([1,2,3,4,5]) 2 >>> countEvens([1]) 0 >>> countEvens([3,2]) 1 >>> countEvens([2,3,4]) 2 >>> """ result = 0 if listOfInts == []: return 0 if not isList(listOfInts): return False for i in listOfInts: if not type(i) == int: return False if i % 2 == 0: result = result + 1 return result
def totalLength(listOfStrings): """ returns total length of all the strings in a list of strings, False if argument not a list, 0 for empty list """ if (not isList(listOfStrings)): return False totlen = 0 for item in listOfStrings: if type(item) == str: totlen = totlen + len(item) return totlen
def lengthOfEach(listOfStrings): """ given list of strings, returns list of ints correponding to length of each string, otherwise False. empty list yields empty list """ if (not isList(listOfStrings)): return False lenlist = [] for item in listOfStrings: if type(item) != str: lenlist = lenlist + ["not str"] else: lenlist = lenlist + [len(item)] return lenlist
def isListOfSimpleNumeric(theList): # Returns True if "theList" is a list of only ints and floats, otherwise returns False if (not isList(theList)): return False # it isn't really a list! # Now we can assume that theList really is a list # But is it a list of all numerics? # If we find even a single item that isn't numeric, we can # immediately return false. for item in theList: if not isSimpleNumeric(item): return False # If we get here and didn't return yet, then we know everything # in the list is a simple numeric! # (i.e. there isn't anything in the list that is NOT simple numeric) return True
def isListOfSimpleNumeric(theList): """ indicates whether value of argument is a list of only simple numerics (int or float) Note: empty list should return True---it doesn't contain anything that ISN'T simple numeric theList can be anything, and it will return either True or False. >>> isListOfSimpleNumeric('Fred') False >>> isListOfSimpleNumeric(3) False >>> isListOfSimpleNumeric([3]) True >>> isListOfSimpleNumeric([3.4]) True >>> isListOfSimpleNumeric([2,3,4,5.6,7]) True >>> isListOfSimpleNumeric([2,3,'oops',5]) False >>> isListOfSimpleNumeric([2,3,[4]]) False >>> isListOfSimpleNumeric([]) True """ if (not isList(theList)): return False # it isn't really a list! # Now we can assume that theList really is a list # But is it a list of all numerics? # If we find even a single item that isn't numeric, we can # immediately return false. for item in theList: if not isSimpleNumeric(item): return False # If we get here and didn't return yet, then we know everything # in the list is a simple numeric! # (i.e. there isn't anything in the list that is NOT simple numeric) return True
def totalLength(listOfStrings): """ returns total length of all the strings in a list of strings, False if argument not a list, 0 for empty list >>> totalLength('1') False >>> totalLength(['a','b']) 2 >>> totalLength([]) 0 >>> totalLength(['Go','Gauchos']) 9 >>> totalLength(['x','xxx','xxxx']) 8 """ result = 0 if listOfStrings == []: return 0 if not isList(listOfStrings): return False for i in listOfStrings: if type(i) == str: result = result + (len(i)) return result
def totalLength(listOfStrings): """ returns total length of all the strings in a list of strings, False if argument not a list, 0 for empty list >>> totalLength('1') False >>> totalLength(['a','b']) 2 >>> totalLength([]) 0 >>> totalLength(['Go','Gauchos']) 9 >>> totalLength(['x','xxx','xxxx']) 8 """ result=0 if listOfStrings == []: return 0 if not isList (listOfStrings): return False for i in listOfStrings: if type (i)==str: result= result+(len(i)) return result
def isListOfEvenIntegers(theList): """ indicates whether value of argument is a list of only even integers Note: empty list should return True---it doesn't contain anything that ISN'T an even integer theList can be anything, and it will return either True or False. >>> isListOfEvenIntegers('Fred') False >>> isListOfEvenIntegers(3) False >>> isListOfEvenIntegers([3]) False >>> isListOfEvenIntegers([4]) True >>> isListOfEvenIntegers([3.4]) False >>> isListOfEvenIntegers([2,3,4,5.6,7]) False >>> isListOfEvenIntegers([2,3,'oops',5]) False >>> isListOfEvenIntegers([2,3,4,5,6,7]) False >>> isListOfEvenIntegers([2,4,6]) True >>> isListOfEvenIntegers([2,3,[4]]) False >>> isListOfIntegers([]) True >>> """ if not (isList(theList)): return False for x in theList: if not x%2==0: return False return True
def onlyEvens(listOfInts): """ given a list of ints, return new list with only the even ones. Otherwise, return false. empty list yields empty list >>> onlyEvens('1') False >>> onlyEvens(['a','b']) False >>> onlyEvens([]) [] >>> onlyEvens([1,2,3,4,5]) [2, 4] >>> onlyEvens([1]) [] >>> onlyEvens([1,3]) [] >>> onlyEvens([3,2]) [2] >>> onlyEvens([2,3,4]) [2, 4] >>> """ result = [] if listOfInts == []: return [] if not isList(listOfInts): return False for i in listOfInts: if not type(i) == int: return False if i % 2 == 0: result.append(i) return result
def lengthOfEach(listOfStrings): """ given list of strings, returns list of ints correponding to length of each string, otherwise False. empty list yields empty list. >>> lengthOfEach('1') False >>> lengthOfEach(['a','b']) [1, 1] >>> lengthOfEach([]) [] >>> lengthOfEach(['Go','Gauchos']) [2, 7] >>> lengthOfEach(['x','xxx','xxxx']) [1, 3, 4] >>> """ if listOfStrings == []: return [] if not isList(listOfStrings): return False x = [len(s) for s in listOfStrings] return x
def lengthOfEach(listOfStrings): """ given list of strings, returns list of ints correponding to length of each string, otherwise False. empty list yields empty list. >>> lengthOfEach('1') False >>> lengthOfEach(['a','b']) [1, 1] >>> lengthOfEach([]) [] >>> lengthOfEach(['Go','Gauchos']) [2, 7] >>> lengthOfEach(['x','xxx','xxxx']) [1, 3, 4] >>> """ if listOfStrings == []: return [] if not isList (listOfStrings): return False x=[len(s) for s in listOfStrings] return x
def onlyEvens(listOfInts): """ given a list of ints, return new list with only the even ones. Otherwise, return false. empty list yields empty list >>> onlyEvens('1') False >>> onlyEvens(['a','b']) False >>> onlyEvens([]) [] >>> onlyEvens([1,2,3,4,5]) [2, 4] >>> onlyEvens([1]) [] >>> onlyEvens([1,3]) [] >>> onlyEvens([3,2]) [2] >>> onlyEvens([2,3,4]) [2, 4] >>> """ result = [] if listOfInts == []: return [] if not isList (listOfInts): return False for i in listOfInts: if not type (i)==int: return False if i%2==0: result.append(i) return result
def isListOfEvenIntegers(theList): """ indicates whether value of argument is a list of only even integers Note: empty list should return True---it doesn't contain anything that ISN'T an even integer theList can be anything, and it will return either True or False. >>> isListOfEvenIntegers('Fred') False >>> isListOfEvenIntegers(3) False >>> isListOfEvenIntegers([3]) False >>> isListOfEvenIntegers([4]) True >>> isListOfEvenIntegers([3.4]) False >>> isListOfEvenIntegers([2,3,4,5.6,7]) False >>> isListOfEvenIntegers([2,3,'oops',5]) False >>> isListOfEvenIntegers([2,3,4,5,6,7]) False >>> isListOfEvenIntegers([2,4,6]) True >>> isListOfEvenIntegers([2,3,[4]]) False >>> isListOfIntegers([]) True >>> """ if not (isList(theList)): return False for x in theList: if not x % 2 == 0: return False return True
def test_isList4(self): self.assertEqual(isList("foo"), False)
def test_isList2(self): self.assertEqual(isList([3]), True)
def test_isList3(self): self.assertEqual(isList([5, 10, 15, 20]), True)
def test_isList1(self): self.assertEqual( isList(3), False)
def test_isList1(self): self.assertEqual(isList(3), False)
def test_isList5(self): self.assertEqual( isList(["John","Paul","Ringo","George"]), True)
def test_isList2(self): self.assertEqual( isList([3]), True)
def test_isList5(): assert( isList(["John","Paul","Ringo","George"])== True)
def test_isList6(): assert( isList([])== True)
def test_isList3(): assert( isList([5,10,15,20])== True)
def test_isList4(): assert( isList("foo")== False)
def test_isList2(): assert( isList([3])== True)
def test_isList1(): assert( isList(3) == False)
def test_isList4(self): self.assertEqual( isList("foo"), False)
def test_isList5(self): self.assertEqual(isList(["John", "Paul", "Ringo", "George"]), True)
def test_isList3(self): self.assertEqual( isList([5,10,15,20]), True)
def test_isList6(self): self.assertEqual(isList([]), True)
def test_isList6(self): self.assertEqual( isList([]), True)