第三个function
selectItems
This method should take a list of products and the price list for all shops as well as preferences, select a shop where the most products can be found and mark those products as products to be bought from that shop. For example,
selectItems
This method should take a list of products and the price list for all shops as well as preferences, select a shop where the most products can be found and mark those products as products to be bought from that shop. For example,
selection={‘milk’=>’tesco’}
selectItems([‘pasta’,’a4 paper’,’christmas lights’],
selection,shopPricesExample,shopPreferencesExample)
should modify selection to become
{‘milk’=>’tesco’,’pasta’ => ‘aldi’,
‘christmas lights’=> ‘aldi’}
and return [‘a4 paper’] because the most items can be bought from ‘aldi’ and ‘a4 paper’ is the only item remaining. The selectItems function can call findBestShop and use the value it returns to update the allocation of items to shops.
第四个function
This method is supposed to take a list of products, a price list for all the shops as well as shopping preferences and decide which items need to be bought from which shop. The argument selection is the hash that is supposed to store this selection. The initial value of the selection is empty {}. For example,
selectItemsRecurse – the main method
This method is supposed to take a list of products, a price list for all the shops as well as shopping preferences and decide which items need to be bought from which shop. The argument selection is the hash that is supposed to store this selection. The initial value of the selection is empty {}. For example,
selectItemsRecurse(productToBuyExample,selection,
shopPricesExample,shopPreferencesExample)
is expected to set selection to
{“milk”=>”tesco”, “bread”=>”tesco”, “corn flakes”=>”tesco”,
“pasta”=>”tesco”, “tomatoes”=>”tesco”,
“potatoes”=>”tesco”, “baked beans”=>”aldi”}
The idea behind this method is to start by removing duplicates and invalid products (using removeDuplicates and findProductsInShops) and then allocate products to the best shop using selectItems. Remaining products are to be allocated to the next shop, again by calling selectItems; what is left should be allocated again and so on.