fork download
  1. # your code goes here# Lab01, CS 8
  2. # [type your name here]
  3. # [type your partner's name here if you worked with someone]
  4.  
  5. def isMultiple(num1, num2):
  6. ''' Function that takes two integers (num1) and (num2).
  7. Returns True if num2 is a multiple of num1, and returns
  8. False otherwise (you may assume num2 >= num1 for this function).
  9. * If num1 is equal to 0, your function should return None.
  10. '''
  11. # COMPLETE YOUR FUNCTION DEFINITION HERE
  12.  
  13. assert isMultiple(1,1) == True
  14. assert isMultiple(1,10) == True
  15. assert isMultiple(2,3) == False
  16. assert isMultiple(4,16) == True
  17. assert isMultiple(4,7) == False
  18. assert isMultiple(0,3) == None
  19. assert isMultiple(3,0) == True
  20.  
  21. def incrementKeyCount(D, key):
  22. ''' Function that takes in a Dictionary object (D) containing KEY:VALUE
  23. pairs where the VALUE is a count representing the number of times the KEY
  24. is incremented.
  25. * If the parameter key does not exist in D, then a new KEY:VALUE
  26. pair is created in D with key:1 (the first time the key is incremented).
  27. * If the parameter key does exist in D, then the key's VALUE is
  28. incremented by 1.
  29. * Note that since dictionaries are mutable, this function does
  30. not return a value.
  31. * Consider using the in operator to check if a key exists in a Dictionary
  32. * Assert examples below illustrate correct behavior for this function
  33. '''
  34. # COMPLETE YOUR FUNCTION DEFINITION HERE
  35.  
  36. dict1 = {}
  37. assert len(dict1) == 0
  38. assert incrementKeyCount(dict1, "CS8") == None
  39. assert len(dict1) == 1
  40. assert ("CS8" in dict1) == True
  41. assert dict1["CS8"] == 1
  42. assert incrementKeyCount(dict1, "CS8") == None
  43. assert len(dict1) == 1
  44. assert dict1["CS8"] == 2
  45. assert incrementKeyCount(dict1, "MATH3A") == None
  46. assert len(dict1) == 2
  47. assert ("MATH3A" in dict1) == True
  48. assert dict1["MATH3A"] == 1
  49.  
  50. def computeGrade(percentage):
  51. ''' Return the corresponding letter grade string based on the value of
  52. percentage using the following scale:
  53. [100 - 90]: 'A'
  54. (90 - 80] : 'B'
  55. (80 - 70] : 'C'
  56. (70 - 60] : 'D'
  57. (60 - 0] : 'F'
  58. * If percentage is not a number type (int or float) OR if percentage is
  59. outside the range of [100 - 0], return an empty string ("").
  60. * Note, you can check if percentage is an int with
  61. type(percentage) == int, and a float with type(percentage) == float
  62. '''
  63. # COMPLETE YOUR FUNCTION DEFINITION HERE
  64.  
  65. assert computeGrade(90) == "A"
  66. assert computeGrade("80") == ""
  67. assert computeGrade(80) == "B"
  68. assert computeGrade(79.9) == "C"
  69. assert computeGrade(64) == "D"
  70. assert computeGrade(60) == "D"
  71. assert computeGrade(56) == "F"
  72. assert computeGrade(101) == ""
  73. assert computeGrade(-1) == ""
  74. assert computeGrade(100) == "A"
  75. assert computeGrade(0) == "F"
  76.  
  77. # Definition of a Book namedtuple object used for the following
  78. # function below.
  79. from collections import namedtuple
  80.  
  81. def updateBookPrice(percentIncrease, bookObject):
  82. ''' Function that takes in a namedtuple Book object (bookObject)
  83. and a positive float (percentIncrease), and returns a new namedtuple
  84. Book object with the same values of bookObject except the price
  85. is increased by percentIncrease (inflation is real!)
  86. '''
  87. # COMPLETE YOUR FUNCTION DEFINITION HERE
  88.  
  89. Book = namedtuple("Book", ["title", "author", "price"])
  90. b1 = Book("Title1", "Author1", 10)
  91. b2 = Book("Title2", "Author2", 15)
  92. b3 = Book("Title3", "Author3", 50)
  93. assert updateBookPrice(0, b1) == Book("Title1", "Author1", 10)
  94. assert updateBookPrice(0.2, b1) == Book("Title1", "Author1", 12)
  95. assert updateBookPrice(1.1, b1) == Book("Title1", "Author1", 21)
  96. assert updateBookPrice(0.1, b2) == Book("Title2", "Author2", 16.5)
  97. assert updateBookPrice(0.5, b3) == Book("Title3", "Author3", 75)
Success #stdin #stdout 0.02s 7524KB
stdin
Standard input is empty
stdout
Standard output is empty