コード例 #1
0
ファイル: lab04Funcs.py プロジェクト: kerjones/cs20-S16-lab04
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
コード例 #2
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 (not isList(listOfStrings)):
       return False  

    result=[]   

    for item in listOfStrings:
       if type(item)==str:
          result= result + [len(item)]
      
    return result
コード例 #3
0
ファイル: lab04Funcs.py プロジェクト: kerjones/cs20-S16-lab04
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
コード例 #4
0
ファイル: lab04Funcs.py プロジェクト: kerjones/cs20-S16-lab04
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
コード例 #5
0
ファイル: lab04Funcs.py プロジェクト: kerjones/cs20-S16-lab04
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
コード例 #6
0
ファイル: lab04Funcs.py プロジェクト: kerjones/cs20-S16-lab04
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
コード例 #7
0
ファイル: lab04Funcs.py プロジェクト: kerjones/cs20-S16-lab04
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
コード例 #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
コード例 #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
コード例 #10
0
ファイル: lab04Funcs.py プロジェクト: tntptntp/CMPTGCS-20
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
コード例 #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
コード例 #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
コード例 #13
0
ファイル: lab04Funcs.py プロジェクト: kerjones/cs20-S16-lab04
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
コード例 #14
0
ファイル: lab04Funcs.py プロジェクト: kerjones/cs20-S16-lab04
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
コード例 #15
0
ファイル: lab04Funcs.py プロジェクト: kerjones/cs20-S16-lab04
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
コード例 #16
0
ファイル: lab04Funcs.py プロジェクト: kerjones/cs20-S16-lab04
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
コード例 #17
0
ファイル: lab04Funcs.py プロジェクト: kerjones/cs20-S16-lab04
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
コード例 #18
0
ファイル: lab04Funcs.py プロジェクト: kerjones/cs20-S16-lab04
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
コード例 #19
0
ファイル: lab04Funcs.py プロジェクト: kerjones/cs20-S16-lab04
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
コード例 #20
0
ファイル: lab04Funcs.py プロジェクト: kerjones/cs20-S16-lab04
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
コード例 #21
0
 def test_isList4(self):
     self.assertEqual(isList("foo"), False)
コード例 #22
0
 def test_isList2(self):
     self.assertEqual(isList([3]), True)
コード例 #23
0
 def test_isList3(self):
     self.assertEqual(isList([5, 10, 15, 20]), True)
コード例 #24
0
 def test_isList1(self):
     self.assertEqual( isList(3),   False)
コード例 #25
0
 def test_isList1(self):
     self.assertEqual(isList(3), False)
コード例 #26
0
 def test_isList5(self):
     self.assertEqual( isList(["John","Paul","Ringo","George"]),   True)
コード例 #27
0
 def test_isList2(self):
     self.assertEqual( isList([3]),   True)
コード例 #28
0
ファイル: test_lab02.py プロジェクト: ezzywezzy/Labs
def test_isList5():
        assert( isList(["John","Paul","Ringo","George"])==   True)
コード例 #29
0
ファイル: test_lab02.py プロジェクト: ezzywezzy/Labs
def test_isList6():
        assert( isList([])== True)
コード例 #30
0
ファイル: test_lab02.py プロジェクト: ezzywezzy/Labs
def test_isList3():
        assert( isList([5,10,15,20])==  True)
コード例 #31
0
ファイル: test_lab02.py プロジェクト: ezzywezzy/Labs
def test_isList4():
        assert( isList("foo")==   False)
コード例 #32
0
ファイル: test_lab02.py プロジェクト: ezzywezzy/Labs
def test_isList2():
        assert( isList([3])==   True)
コード例 #33
0
ファイル: test_lab02.py プロジェクト: ezzywezzy/Labs
def test_isList1():
        assert( isList(3) ==  False)
コード例 #34
0
 def test_isList4(self):
     self.assertEqual( isList("foo"),   False)
コード例 #35
0
 def test_isList5(self):
     self.assertEqual(isList(["John", "Paul", "Ringo", "George"]), True)
コード例 #36
0
 def test_isList3(self):
     self.assertEqual( isList([5,10,15,20]),   True)
コード例 #37
0
 def test_isList6(self):
     self.assertEqual(isList([]), True)
コード例 #38
0
 def test_isList6(self):
     self.assertEqual( isList([]),   True)