ó
?úźWc           @   se  d  Z  d d l Z d d l Z d   Z d   Z d   Z d   Z e d k rad d d g Z i d d 6d	 d
 6Z	 i d	 d 6d d
 6d d 6Z
 i d d 6d d 6Z e j d e	  Z e j d e
  Z e j d e  Z e e e g Z i d d 6d d 6d d  6d d! 6d d" 6d d# 6Z e j e e  Z d Ge GHx. d$ D]# Z d Ge Gd Ge e e e  GHq7Wn  d S(%   s°  
Here's the intended output of this script, once you fill it in:

Welcome to shop1 fruit shop
Welcome to shop2 fruit shop
Welcome to shop3 fruit shop
Orders: [('apples', 1.0), ('oranges', 3.0), ('limes', 2.0)]
At gas price 1 the best route is: ['shop1', 'shop2', 'shop3']
At gas price 3 the best route is: ['shop1', 'shop3']
At gas price 5 the best route is: ['shop2']
At gas price -1 the best route is: ['shop2', 'shop1', 'shop3']
i˙˙˙˙Nc         C   sË   g  } t  | j    } xR | D]J } g  | D] } | j   ^ q, } | j |  |  r | t |  7} q q Wd \ } }	 xK | D]C }
 | j |  |
 |  } | d k sł | | k  r | |
 } }	 q q W|	 S(   s  
        orderList: List of (fruit, numPound) tuples
        fruitTown: A Town object
        gasCost: A number representing the cost of going one mile
    Returns a list of shops in the order that is the optimal route to take when
    buying the fruit in the orderList
    N(   NN(   t   getAllSubsetst   getShopst   getNamet   allFruitsCarriedAtShopst   getAllPermutationst   Nonet   getPriceOfOrderOnRoute(   t	   orderListt	   fruitTownt   gasCostt   possibleRoutest   subsetst   subsett   shopt   namest   minCostt	   bestRoutet   routet   cost(    (    s   shopAroundTown.pyt   shopAroundTown   s    c         C   sP   |  s
 g  Sg  t  |  d  D] } |  d g | ^ q } t  |  d  } | | S(   si   
        lst: A list
    Returns the powerset of lst, i.e. a list of all the possible subsets of lst
    i   i    (   R    (   t   lstt   restt	   withFirstt   withoutFirst(    (    s   shopAroundTown.pyR    4   s
    .c         C   s|   |  s
 g  St  |   d k r  |  Sg  } xO t t  |    D]; } |  | } |  |  |  | } | t | t |   7} q9 W| S(   sG   
        lst: A list
    Returns a list of all permutations of lst
    i   (   t   lent   ranget   prependToAllR   (   R   t   allPermutationst   it   itemt   withoutItem(    (    s   shopAroundTown.pyR   ?   s    
c         C   s   g  | D] } |  g | ^ q S(   s   
        item: Any object
        lsts: A list of lists
    Returns a copy of lsts with item prepended to each list contained in lsts
    (    (   R   t   lstsR   (    (    s   shopAroundTown.pyR   O   s    t   __main__t   applesg      đ?t   orangesg      @t   limesg       @g      @t   shop1t   shop2t   shop3i   t   homei   g      @s   Orders:i   i   s   At gas prices   the best route is:(   s   applesg      đ?(   s   orangesg      @(   s   limesg       @(   R'   s   shop1(   R'   s   shop2(   R'   s   shop3(   s   shop1s   shop2(   s   shop1s   shop3(   s   shop2s   shop3(   i   i   i   i˙˙˙˙(   t   __doc__R   t   townR   R    R   R   t   __name__t   orderst   dir1t   dir2t   dir3t	   FruitShopR$   R%   R&   t   shopst	   distancest   TownR   t   price(    (    (    s   shopAroundTown.pyt   <module>   s4   				

	