Question: ANSWER THE FOLLOWING QUESTION IN PYTHON AND PUT THE ANSWER IN THE ORGINAL QUESTION WHERE IT IS SUPPOSED TO BE ! 1. #!/usr/bin/python3 import unittest
ANSWER THE FOLLOWING QUESTION IN PYTHON AND PUT THE ANSWER IN THE ORGINAL QUESTION WHERE IT IS SUPPOSED TO BE !
1.
#!/usr/bin/python3
import unittest
# --------------------------------------------------------------
# Return the list of totals
# --------------------------------------------------------------
def totals(L):
'''
Assume that L is a list of pairs (item, weight).
An item may occur multiple times in L.
Return a list R with the values totaled for each item, so
an item does not occur multiple times in R, but rather once with its total.
CONSTRAINT: YOU MUST USE A DICTIONARY
For example,
totals([('item2', 2)]) returns [('item2', 2)]
totals([('item2', 2), ('item2', 5)]) returns [('item2', 7)]
totals([('item2',2),('item1',3),('item2',2)]) => [('item1',3),('item2',4)]
(Note that the order of the items in the returned list does not matter.)
'''
pass
# --------------------------------------------------------------
# The Testing
# --------------------------------------------------------------
class myTests(unittest.TestCase):
def test0(self):
self.assertEqual(totals([]), [])
def test1(self):
self.assertEqual(totals([('item1',3)]), [('item1',3)])
def test2(self):
self.assertEqual(sorted(totals([('item1',3),('item2',2)])), [('item1',3),('item2',2)])
def test3(self):
self.assertEqual(sorted(totals([('item2',2),('item1',3),('item2',2)])),[('item1',3),('item2',4)])
if __name__=='__main__':
unittest.main(exit=True)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
