Exemplo n.º 1
0
 def test_keys(self):
     hash = HashMap()
     hash[1] = [100]
     hash[2] = [200]
     hash[3] = [300]
     hash["Hello"] = "Goodbye"
     keys = hash.keys()
     assert ("Hello" in keys)
     assert (1 in keys)
     assert (2 in keys)
     assert (3 in keys)
Exemplo n.º 2
0
class Account(object):


    # Initialize Account object with cash and hash map of stocks, where
    # stock name points to number of shares
    def __init__(self):
        self.cash = 0
        self.stocks = HashMap()


    # Compare account's cash and stocks with that of another account
    # Used in TransactionParser's reconcile() method
    def compare(self, other_acct):
        diffs = []
        other_stocks = other_acct.stocks
        all_keys = list(set(self.stocks.keys() + other_stocks.keys()))

        for key in all_keys:
            diff = self.stock_diff(key, other_stocks)
            if diff and diff != 0:
                diffs.append(key + " " + str(int(diff)))

        diffs.insert(0, "Cash " + str(self.cash_diff(other_acct.cash)))

        return "\n".join(diffs)


    def cash_diff(self, other_cash):
        return int(other_cash) - self.cash


    # Calculates differences in shares of stocks between two accounts
    def stock_diff(self, key, declared_results):
        if self.stocks[key] and declared_results[key]:
            return float(declared_results[key]) - self.stocks[key]
        elif self.stocks[key]:
            return -1 * self.stocks[key]
        elif declared_results[key] and key != "Cash":
            return float(declared_results[key])
        else:
            return None


    # Wrapper for setting a stock into self.stocks
    def set_stock(self, name, value):
        self.stocks[name] = value


    # Wrapper for getting a stock
    def get_stock(self, stock):
        return self.stocks.get(stock, 0)
Exemplo n.º 3
0
print "After doubling our number of shares in Google: " + str(hash_map)

hash_map.delete("AAPL")
print "After selling all of our shares in Apple: " + str(hash_map)

other_hash_map = HashMap()
other_hash_map["GOOG"] = 100
other_hash_map["DIS"] = 500
print "My friend recommends a different set of positions: " + str(
    other_hash_map)

hash_map.update(other_hash_map)
print "Our positions after updating with friend's recommendations: " + str(
    hash_map)

print "A list of tickers of our positions: " + str(hash_map.keys())
print "A list of the number of shares for each position: " + str(
    hash_map.values())

print "Number of buckets in our hashmap: " + str(hash_map.num_buckets())

hash_map["BRK-A"] = 50
hash_map["AMZN"] = 100
hash_map["FB"] = 150
hash_map["JNJ"] = 200
hash_map["XOM"] = 25
hash_map["GE"] = 250

print "After buying spree we now hold: " + str(hash_map)
print "The total number of buckets is now: " + str(hash_map.num_buckets())