def build_attack_rec_wrong(self, n, c, network): if n == 0 or c == 0: s = Solution(network, [], 0) self.solutions[n][c] = s return s asset_sym = self.id_to_sym[n] if self.solutions[n][c]: return self.solutions[n][c] elif self.weights[n] > c: s = self.build_attack(n - 1, c, copy.deepcopy(network)) self.solutions[n][c] = s return s else: prev_solutions = {} max_result = self.build_attack(n - 1, c, copy.deepcopy(network)) prev_solutions[1] = self.build_attack(n - 1, c - self.weights[n], copy.deepcopy(network)) prev_solutions[2] = self.build_attack(n, c - self.weights[n], copy.deepcopy(network)) order = Sell( asset_sym, self.min_order_percentage * network.assets[asset_sym].daily_volume) for i in range(1, 2): net2 = prev_solutions[i].network net2.submit_sell_orders([order]) net2.clear_order_book() value = net2.count_margin_calls() if value > max_result.value: actions = copy.copy(prev_solutions[i].actions) actions.append(order) max_result = Solution(net2, actions, value) self.solutions[n][c] = max_result return max_result
def attack(self, n, network, orders_list): if n == 0: net2 = copy.deepcopy(network) net2.submit_sell_orders(orders_list) net2.clear_order_book() funds = net2.get_funds_in_margin_calls() cost = sum([ o.num_shares * network.assets[o.asset_symbol].zero_time_price for o in orders_list ]) value = len(funds) for i in range(1, value + 1): if i not in self.solutions or cost < self.solutions[i].cost: self.solutions[i] = Solution(network, orders_list, value, funds, cost) return asset_sym = self.id_to_sym[n] orders_list2 = copy.copy(orders_list) self.attack(n - 1, network, orders_list2) for i in range(1, self.max_order_num + 1): num_shares = int( floor(i * self.min_order_percentage * network.assets[asset_sym].daily_volume)) order = Sell(asset_sym, num_shares) orders_list2 = copy.copy(orders_list) orders_list2.append(order) self.attack(n - 1, network, orders_list2) return
def build_attack(self, n, c, network): if n == 0 or c == 0: s = Solution(network, [], 0, [], 0) self.solutions[n][c] = s return s asset_sym = self.id_to_sym[n] if c in self.solutions[n]: return self.solutions[n][c] elif self.weights[n] > c: s = self.build_attack(n - 1, c, copy.deepcopy(network)) self.solutions[n][c] = s return s else: max_assets_orders = min(self.max_order_num, (int)(floor(c / self.weights[n]))) max_result = self.build_attack(n - 1, c, copy.deepcopy(network)) for i in range(1, max_assets_orders + 1): prev_solution = self.build_attack(n - 1, c - i * self.weights[n], copy.deepcopy(network)) num_shares = floor(i * self.min_order_percentage * network.assets[asset_sym].daily_volume) if not num_shares: continue order = Sell(asset_sym, num_shares) net2 = prev_solution.network net2.submit_sell_orders([order]) net2.clear_order_book() funds = net2.get_funds_in_margin_calls() value = len(funds) cost = prev_solution.cost + order.num_shares * network.assets[ order.asset_symbol].zero_time_price if value > max_result.value or (value == max_result.value and cost < max_result.cost): actions = copy.copy(prev_solution.actions) actions.append(order) max_result = Solution(net2, actions, value, funds, cost) self.solutions[n][c] = max_result return max_result
def test_gen_optimal_attacks(self): a1 = AssetFundNetwork.Asset(price=10, daily_volume=10, symbol='a1') a2 = AssetFundNetwork.Asset(price=20, daily_volume=10, symbol='a2') f1_margin_call = lambda assets: assets['a1'].price <= 8 f2_margin_call = lambda assets: assets['a2'].price <= 19 f3_margin_call = lambda assets: assets['a2'].price <= 19 mi_calc = MockMarketImpactTestCalculator({ 'a1': { 5: 9, 10: 8 }, 'a2': { 5: 19, 10: 18 } }) f1 = MockFund('f1', a1, f1_margin_call) f2 = MockFund('f2', a2, f2_margin_call) f3 = MockFund('f3', a2, f3_margin_call) network = AssetFundNetwork.AssetFundsNetwork(funds={ 'f1': f1, 'f2': f2, 'f3': f3 }, assets={ 'a1': a1, 'a2': a2 }, mi_calc=mi_calc, limit_trade_step=False) solver = SingleAgentESSolver(network, 0.5, 2) actual_solutions = solver.gen_optimal_attacks() solution_2 = Solution(network, [Sell('a2', 5)], 2, ['f2', 'f3'], 100) solution_3 = Solution(network, [Sell('a1', 10), Sell('a2', 5)], 3, ['f1', 'f2', 'f3'], 200) expected_solutions = {1: solution_2, 2: solution_2, 3: solution_3} self.assertEqual(expected_solutions, actual_solutions)
def gen_optimal_attacks(self): solutions = {} # portfolios = self.get_all_attack_portfolios(self.network.assets, len(self.network.assets)) attacks = self.action_mgr.get_possible_attacks() for (order_set, cost) in attacks: net2 = copy.deepcopy(self.network) net2.submit_sell_orders(order_set) net2.clear_order_book() funds = net2.get_funds_in_margin_calls() value = len(funds) for i in range(1, value + 1): if i not in solutions or cost <= solutions[i].cost: solutions[i] = Solution(self.network, order_set, value, funds, cost) return solutions
# if(diff == 1 or diff == -1): # diff_arr.append(diff) # else: # diff_arr.append(0) # window_start_index = window_start_index + 1 # window_end_index = window_end_index + 1 # first_index = 0 # end_index = len(diff_arr) # second_index = first_index + 1 # if(diff_arr[first_index] == 0): # first_index = first_index + 1 # if(diff_arr[end_index] == 0): # end_index = end_index - 1 # sum_num = 0 # while(first_index <= end_index): # if(sum_num != 0 and diff_arr[first_index] == diff_arr[second_index]): # sum_num = sum_num + nums[first_index] return diff_arr tt = [[1, 9, 8, 5], [4, 5, 6], [1, 2, 1, 2, 3, 3, 2], [3, 2, 4], [1, 2, 1]] # tt = [[1, 9, 8, 5]] sol = Solution() sol.run(find_shunshine, tt)
from common import Solution # Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. # You must implement a solution with a linear runtime complexity and use only constant extra space. def single_number(nums): xored = 0 for num in nums: xored = xored ^ num return xored tt = [[2, 2, 1], [4, 1, 2, 1, 2], [1]] sol = Solution() sol.run(single_number, tt)
current_index = current_index - 1 while (carry != 0): if (current_index < 0): break current_num = nums[current_index] if (current_num == 9): nums[current_index] = 0 else: nums[current_index] += 1 carry = 0 current_index -= 1 if (carry == 1): nums = [ int(newNum) for newNum in list("1" + "".join([str(num) for num in nums])) ] return nums tt = [[1, 2, 3], [4, 3, 2, 1], [0], [9], [9, 9], [8, 9], [9, 9, 9, 9], [9, 8, 9]] sol = Solution() sol.run(add_one, tt)
from common import Solution # Given an integer array nums, move all 0's to the end of it while maintaining # the relative order of the non-zero elements. # Note that you must do this in-place without making a copy of the array. def move_zeroes(nums): pos = 0 for i in range(len(nums)): el = nums[i] if el != 0: nums[pos], nums[i] = nums[i], nums[pos] pos += 1 return nums tt = [[0, 1, 0, 3, 12], [0], [9, 8, 0, 5, 0, 2]] sol = Solution() sol.run(move_zeroes, tt)
# Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. from common import Solution def find_duplicate(nums): found_map = {} len_arr = len(nums) found_duplicate = False start_index = 0 while (start_index < len_arr): if (not found_map.get(nums[start_index], False)): found_map[nums[start_index]] = True else: return True start_index = start_index + 1 return found_duplicate tt = [[1, 2, 3, 1], [1, 2, 3, 4], [1, 1, 1, 3, 3, 4, 3, 2, 4, 2]] sol = Solution() sol.run(find_duplicate, tt)
# intersection. Each element in the result must appear as many times as it # shows in both arrays and you may return the result in any order. def find_intersection(arrs): num1, num2 = arrs intersection = [] first_count_map = dict() second_count_map = dict() # Go through num1 and count each element using first_count_map for num in num1: first_count_map[num] = first_count_map.get(num, 0) + 1 for num in num2: second_count_map[num] = second_count_map.get(num, 0) + 1 for key in first_count_map: count_of_key_in_num2 = second_count_map.get(key, 0) if (count_of_key_in_num2): intersection = intersection + [key] * ( count_of_key_in_num2 if count_of_key_in_num2 < first_count_map[key] else first_count_map[key]) return intersection tt = [[1, 2, 2, 1], [2, 2]], [[4, 9, 5], [9, 4, 9, 8, 4]] sol = Solution() sol.run(find_intersection, tt)
if (len(nums) > 1): if (k > len(nums)): k = k % len(nums) reverse(nums, first_index, last_index) reverse(nums, 0, k - 1) reverse(nums, k, last_index) # len_arr = len(arr) # curr_index = 0 # new_index = -1 # temp = arr[curr_index] # location_map = {} # while(not location_map.get(curr_index)): # new_index = (curr_index + k) % len_arr # # arr[curr_index] = arr[new_index] # temp2 = temp # temp = arr[new_index] # arr[new_index] = temp2 # location_map[curr_index] = True # curr_index = new_index # return arr tt = [([1, 2, 3, 4, 5, 6, 7], 1), ([-1, -100, 3, 99], 2), ([-1], 3), ([1, 2], 3)] sol = Solution() sol.run(rotate, tt)