On 11/17/18 6:55 PM, Harry Oneill wrote: > Hello there everyone i hope you are all doing fantastic. > > Im currently working on a little program that is designed to do a basic > function to help me to trade efficiently within the game Star Citizen: > > #1 take two inputs ( location and destination ) > #2 Find what product is best to buy at location to sell at destination > #3 Print the results > > currently i have figured out how to get python to take the two inputs and > provide me with the lowest buy price and highest sell price. > > However the information i want to produce is what has the largest profit > margin possible between the two locations so i need to compare what items are > available in both locations and then see what one has the largest price > difference then display that items name. > > Below is the code i have currently made for this. > > > Any input would be hugely appreciated. > > Kind regards, > > Harry O'Neill
Harry, sounds like a fun little project. Thanks for asking it in a way that makes it easy to read! I'll give you a couple of ideas to think about, others may have completely different thoughts. == First off, think about how to represent the data you have. 1. while it is true that the item and the price can be considered a tuple, and then you can put those in a list, any time in Python (and in a number of other programming languages) you have what look like mapping a key to a data value, you should think dictionary - then you access the price by indexing by the goods string. An example of just quickly manipulating what you already have into dictionaries: Buying_Levski = {'Agricultural Supply': 1.11, 'Aluminum': 1.20, 'Hydrogen': 0.98, 'Iodine': 0.38, 'Quartz': 1.37, 'Waste': 0.005,} the price of buying Hydrogen on Levski is thus Buying_Levksi['Hydrogen'], which is a lot more readable than a numeric index into a tuple which is itself obtained by a numeric index into a list. Lists *do* have the advantage of being easy to sort, but bear with me. 2. storing the data pertinent to an endpoint by the name of the endpoint means each investigation has to hardcode the name of those endpoint variables. You probably want to think of a data structure that will let you fish out the pricing dictionary by endpoint name and transaction type (buy or sell). === Second, let's rethink how you can extract what you want from this data. 1. From an endpoint pair - say Buy/Levski and Sell/Olisar, you need the common items. No point in looking further at items that can't both be bought at the source and sold at the destination. Fortunately, Python sets are really good at this, but first you have to extract only the item names from your dictionary, since that comparison is based only on good names. Something like this: def common(curr, other): return set(curr.keys()).intersection(set(other.keys())) aka build a set using the keys from the first argument, and call the intersection method on that set giving it the set built from the keys of the second argument. Which you can call this way to see that it's working: possibles = common(Buying_Levski, Selling_Olisar) print("common Levski -> Olisar", possibles) (you can certainly solve this in other ways than using sets) 2. From the common items, figure out which provides the greatest difference between the buy price and sell price. We'll presume, because you didn't mention it, that there is no volume component to this and that these prices are all for one unit and units consume the same amount of carrying capacity in your vessel. So one way to tackle this might be to build a new dictionary containing the items and their price difference. Given our previous idea of having a set which contains the common items: prices = {k: Selling_Olisar[k] - Buying_Levski[k] for k in possibles} That's pretty concise... it's called a "dictionary comprehension" if you haven't seen those before, and means loop through "possibles" saving the possibles item as the key and the difference between sell price and buy price as the value. Note1: see how irritating it is to have to keep referring to the data structures by name this way? Note2: you'll probably get a surprise when you do this - floating point numbers don't always represent "cleanly", so the subtractions might give you intesting-looking values if you print them out. There are answers for that, too. Given that dictionary, what is the key corresponding to the largest value - the good you want to buy? That's a little tricky with a dictionary, but with some manipulation, or constructing the proper sort function as an argument to the max() function, it can be done. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor