0

I have the following unittest questions I am trying to pass.

def test_map2(self):
    self.home = []
    self.home.append(Bed('Bedroom'))
    self.home.append(Sofa('Living Room'))
    self.home.append(Table('Bedroom'))

    mapping = map_the_home(self.home)
    self.assertTrue(isinstance(mapping['Bedroom'][0], Bed))  
    self.assertTrue(isinstance(mapping['Living Room'][0], Sofa))
    self.assertTrue(isinstance(mapping['Bedroom'][1], Table))  

Every value should be a list, with one or more Furnishing subclass instances inside.

This is my current try.

class Furnishing(object):    
    def __init__(self, room):
        self.room = room

class Sofa(Furnishing):    
    name = 'Sofa'

class Bed(Furnishing):    
    name = 'Bed'

class Table(Furnishing):    
    name = 'Table'

def map_the_home(home):    
    results = {}

    for furnitiure in home:    
        if furnitiure.room in results:                
            results[furnitiure.room] = (results[furnitiure.room],furnitiure)    
        else:    
            results[furnitiure.room] = furnitiure        
    return results

def counter(home):    
    counter_list = {}
    for line in home:    
        if line.name in counter_list:
            print(line.room,line.name)
            counter_list[line.name] = counter_list[line.name] + 1    
        else:    
            counter_list[line.name] = 1    

    for furniture in counter_list:    
        print('{0} = {1}'.format(furniture,counter_list[furniture]))

if __name__ == "__main__":    
    home = []
    home.append(Bed('Bedroom'))
    home.append(Sofa('Living Room'))
    home.append(Table('Bedroom'))
    map_the_home(home)
    counter(home)

The counter is just another part but wanted to give full code. I thought I had this using dict, but as the test says I need to have every value in a list with Furnishing subclass instances inside. Any insight would be great

1
  • The line results[furnitiure.room] = (results[furnitiure.room], furnitiure) doesn't do what you want. Commented Apr 15, 2014 at 16:48

2 Answers 2

2

The test is expecting that the result will be like this:

mapping == {'Bedroom': [bed, table], 'Living Room': [sofa]}

Whereas you create:

{'Bedroom': (bed, table), 'Living Room': sofa}

Note that the "Living Room" value isn't a container, but a single Sofa instance.

Indeed, if you had three items of furniture in a room, e.g. adding a Sofa in the "Bedroom":

{'Bedroom': ((bed, table), sofa), 'Living Room': sofa}

you would keep on nesting deeper.

The minimal fix is:

if furniture.room in results:                
    results[furniture.room].append(furniture)    
else:    
    results[furniture.room] = [furniture]

Note that using a list and tuple will give the same results, as both can be indexed (although you add (not append) to tuples); I think a list is a better fit here, but your use of tuples wasn't the source of the error.

Sign up to request clarification or add additional context in comments.

Comments

0
def map_the_home(home):
    results = dict()
    for furniture in home:
        results.setdefault(furniture.room,[]).append(furniture)
    return results

Let's walk through it in pseudocode

home = [Bed('Bedroom'), Sofa('Living Room'), Table('Bedroom')]
map_the_home(home)
results = dict()
for each piece of furniture in the home:
    append that piece of furniture to results[furniture.room]
    if results[furniture.room] doesn't exist, make it an empty list then append
return the results

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.