Problems: Write a Mapping class that represents a 1-1 mapping between items. A 1
ID: 3662678 • Letter: P
Question
Problems:
Write a Mapping class that represents a 1-1 mapping between items. A 1-1 mapping is like a dictionary, but it has key1:key2 pairs (instead of key:value pairs). You can look up either key. For example, if m[1] is equal to 2, then m[2] is equal to 1. Your implementation MUST inherit from (subclass) dict. Basically, whenever you create key11:key2 pair, you also make the corresponding key2:key1 pair.
>>> m = Mapping()
>>> m[1]=2
>>> m
Mapping({1: 2, 2: 1})
>>> m[1]
2
>>> m[2]
1
>>> m[3]=4
>>> m
Mapping({1: 2, 2: 1, 3: 4, 4: 3})
>>> m[5]=5
>>> m
Mapping({1: 2, 2: 1, 3: 4, 4: 3, 5: 5})
>>> m[5]
5
>>> m.pop(1)
>>> m
Mapping({3: 4, 4: 3, 5: 5})
>>> m.pop(5)
>>> m
Mapping({3: 4, 4: 3})
>>> m = Mapping( {1:2,2:3,4:4})
>>> m
Mapping({2: 3, 3: 2, 4: 4})
>>>
Implementation notes: You MUST inherit from (subclass dict). You will get some functionality for free.
Write the class first without worrying about self mapping keys (m[5]=5). Once you get that to work, worry about self mapping keys (notes below).
__repr__ - see test code for output
__getitem__ - get this for free! No need to implement.
pop()
when a key is popped you, must also pop the key it maps to
For self mapping keys, you need to make sure that this does not create an error!!!!! Don’t worry about this until you get everything else working.
__setitem__
given key1:key2 make sure you also add key2:key1
before adding the key1:key2 and key2:key1 pairs, you should check whether either key1 or key2 exists already. If so, they should be popped before adding.
__init__ you should be able to construct
An empty Mapping
A Mapping constructed from a given dictionary, see test runs towards bottom.
Explanation / Answer
As of .net3.5+ instead of using a Dictionary you can use a Lookup from the Linq namespace: using System; using System.Collections.Generic; using System.Linq; namespace LinqLookupSpike { class Program { static void Main(String[] args) { // init var orderList = new List(); orderList.Add(new Order(1, 1, 2010, true));//(orderId, customerId, year, isPayed) orderList.Add(new Order(2, 2, 2010, true)); orderList.Add(new Order(3, 1, 2010, true)); orderList.Add(new Order(4, 2, 2011, true)); orderList.Add(new Order(5, 2, 2011, false)); orderList.Add(new Order(6, 1, 2011, true)); orderList.Add(new Order(7, 3, 2012, false)); // lookup Order by its id (1:1, so usual dictionary is ok) Dictionary orders = orderList.ToDictionary(o => o.OrderId, o => o); // lookup Order by customer (1:n) // would need something like Dictionary orderIdByCustomer ILookup byCustomerId = orderList.ToLookup(o => o.CustomerId); foreach (var customerOrders in byCustomerId) { Console.WriteLine("Customer {0} ordered:", customerOrders.Key); foreach (var order in customerOrders) { Console.WriteLine(" Order {0} is payed: {1}", order.OrderId, order.IsPayed); } } // the same using old fashioned Dictionary Dictionary orderIdByCustomer; orderIdByCustomer = byCustomerId.ToDictionary(g => g.Key, g => g.ToList()); foreach (var customerOrders in orderIdByCustomer) { Console.WriteLine("Customer {0} ordered:", customerOrders.Key); foreach (var order in customerOrders.Value) { Console.WriteLine(" Order {0} is payed: {1}", order.OrderId, order.IsPayed); } } // lookup Order by payment status (1:m) // would need something like Dictionary orderIdByIsPayed ILookup byPayment = orderList.ToLookup(o => o.IsPayed); IEnumerable payedOrders = byPayment[false]; foreach (var payedOrder in payedOrders) { Console.WriteLine("Order {0} from Customer {1} is not payed.", payedOrder.OrderId, payedOrder.CustomerId); } } class Order { // key properties public Int32 OrderId { get; private set; } public Int32 CustomerId { get; private set; } public Int32 Year { get; private set; } public Boolean IsPayed { get; private set; } // additional properties // private List _items; public Order(Int32 orderId, Int32 customerId, Int32 year, Boolean isPayed) { OrderId = orderId; CustomerId = customerId; Year = year; IsPayed = isPayed; } } } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.