Beispiel #1
0
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
Beispiel #3
0
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
Beispiel #4
0
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
Beispiel #5
0
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
Beispiel #6
0
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
Beispiel #7
0
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
Beispiel #8
0
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
Beispiel #9
0
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
Beispiel #10
0
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
Beispiel #11
0
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
Beispiel #12
0
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
Beispiel #13
0
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
Beispiel #14
0
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
Beispiel #15
0
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
Beispiel #16
0
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
Beispiel #17
0
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
Beispiel #18
0
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
Beispiel #19
0
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
Beispiel #20
0
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)
Beispiel #24
0
 def test_isList1(self):
     self.assertEqual( isList(3),   False)
 def test_isList1(self):
     self.assertEqual(isList(3), False)
Beispiel #26
0
 def test_isList5(self):
     self.assertEqual( isList(["John","Paul","Ringo","George"]),   True)
Beispiel #27
0
 def test_isList2(self):
     self.assertEqual( isList([3]),   True)
Beispiel #28
0
def test_isList5():
        assert( isList(["John","Paul","Ringo","George"])==   True)
Beispiel #29
0
def test_isList6():
        assert( isList([])== True)
Beispiel #30
0
def test_isList3():
        assert( isList([5,10,15,20])==  True)
Beispiel #31
0
def test_isList4():
        assert( isList("foo")==   False)
Beispiel #32
0
def test_isList2():
        assert( isList([3])==   True)
Beispiel #33
0
def test_isList1():
        assert( isList(3) ==  False)
Beispiel #34
0
 def test_isList4(self):
     self.assertEqual( isList("foo"),   False)
 def test_isList5(self):
     self.assertEqual(isList(["John", "Paul", "Ringo", "George"]), True)
Beispiel #36
0
 def test_isList3(self):
     self.assertEqual( isList([5,10,15,20]),   True)
 def test_isList6(self):
     self.assertEqual(isList([]), True)
Beispiel #38
0
 def test_isList6(self):
     self.assertEqual( isList([]),   True)