Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python Code Help PLease Restrictions: No importing other modules or using contai

ID: 3716074 • Letter: P

Question

Python Code Help PLease

Restrictions: No importing other modules or using contains(), index(), or try-except.

combine_mapping(mapping1, mapping2): This function accepts two encoding dictionaries (mapping1 and mapping2) and returns a combined mapping if (and only if) they can be merged into a valid mapping (there are no duplicated keys). If the mappings cannot be merged, this function should return None.

Test Cases:

def test_combine_mapping_01(self): mapping1a': 'a combined-'a': 'a','b':'b' result combine_mapping (mapping1, mapping2) self.assertEqual(combined, result) def test_combine_mapping_02(self): mappinglapple':'orange'fruit': 'banana' h mapping2= { 'Fredd':'horses', 'David','kitten', 'Alexi':'gerbil' } combined={ 'apple' :"orange', 'fruit ':'banana', 'Fredd':'horses', 'David':'kitten', result combine_mapping (mapping1, mapping2) self.assertEqual(combined, result) 'Alexi': 'gerbil' } def test_combine_mapping_03(self): mappingl- 1 1 mapping2once':'the 'upon': 'end' combinedonce':'the,'upon': 'end'h result combine_mapping (mapping1, mapping2) self.assertEqual(combined, result) def test_combine_mapping 04 (self) # different length keys, invalid mapping created mapping1apples': 'oranges', 'fruits': 'bananas'h mapping2kiwi': 'nectors' ) result = combine-mapping (mapping!, mapping2) self.assertTrue (result is None) # different length values, invalid mapping created mappingl- once':'the,'upon': 'end' mapping2 Andy": April', 'Mark': 'March') result combine_mapping (mapping1, mapping2) self.assertTrue (result is None) def test_combine_mapping_05(self): # duplicate values mapping1once':'the 'upon': 'end' mapping2once': 'the 'apple':'Orange' result combine_mapping (mapping1, mapping2) self.assertTrue (result is None) def test_combine_mapping 06(self) # overlapping keys, can't create valid mapping mappingl-t '1':'a', '2': 'b, '3':'c '4':'d', result combine_mapping (mapping1, mapping2) self.assertTrue (result is None)

Explanation / Answer

Hey, So I have updated the code for Python3 and tested it too. Now it should run fine for you. Get back if you still face issues

import unittest

def combine_mapping(mapping1,mapping2):
if len(mapping1)==0 and len(mapping2)==0:
return {}
elif len(mapping1)!=0:
keylen = len(list(mapping1.keys())[0])
vallen = len(mapping1[list(mapping1.keys())[0]])
else:
keylen = len(list(mapping2.keys())[0])
vallen = len(mapping2[list(mapping2.keys())[0]])

res = {}

for key in mapping1:
if not key in res:
if len(key)==keylen and len(mapping1[key])==vallen:
res[key] = mapping1[key]
else:
return None
else:
return None

for key in mapping2:
if not key in res:
if len(key)==keylen and len(mapping2[key])==vallen:
res[key] = mapping2[key]
else:
return None
else:
return None
return res


class tests(unittest.TestCase):

def test_combine_mapping_01(self):
mapping1 = {'a':'a'}
mapping2 = {'b':'b'}
combined = {'a':'a','b':'b'}
result = combine_mapping(mapping1,mapping2)
self.assertEqual(combined,result)

def test_combine_mapping_02(self):
mapping1 = {'apple':'orange','fruit':'banana'}
mapping2 = {'Fredd':'horses','David':'kitten','Alexi':'gerbil'}
combined = {'apple':'orange','fruit':'banana','Fredd':'horses','David':'kitten','Alexi':'gerbil'}
result = combine_mapping(mapping1,mapping2)
self.assertEqual(combined,result)

def test_combine_mapping_03(self):
mapping1 = {}
mapping2 = {'once':'the','upon':'end'}
combined = {'once':'the','upon':'end'}
result = combine_mapping(mapping1,mapping2)
self.assertEqual(combined,result)

def test_combine_mapping_04(self):
#different length keys, invalid mapping created
mapping1 = {'apple':'oranges','fruit':'bananas'}
mapping2 = {'kiwi':'nectors'}
result = combine_mapping(mapping1,mapping2)
self.assertTrue(result is None)

# different length values, invalid mapping created
mapping1 = {'once':'the','upon':'end'}
mapping2 = {'Andy':'April','Mark':'March'}
result = combine_mapping(mapping1,mapping2)
self.assertTrue(result is None)

def test_combine_mapping_05(self):
# duplicate values
mapping1 = {'once':'the','upon':'end'}
mapping2 = {'once':'the','appl':'Ora'}
result = combine_mapping(mapping1,mapping2)
self.assertTrue(result is None)

def test_combine_mapping_06(self):
mapping1 = {'1':'a','2':'b','3':'c','4':'d'}
mapping2 = {'5':'e','3':'f','6':'g'}
result = combine_mapping(mapping1,mapping2)
self.assertTrue(result is None)


if __name__=="__main__":
unittest.main()