def bucket_sort(collection: Union, key=None): if len(collection) == 0: return collection if key is None: key = identity collection = list(collection) result = [] min_value, max_value, _, _ = min_max(collection, key) bucket_count = np.floor(np.log(max_value - min_value)) + 1 step = get_step(min_value, max_value, bucket_count) buckets = [[] for _ in range(int(bucket_count))] for item in collection: k = call(key, item) bucket = _get_bucket(k, min_value, step, bucket_count) buckets[int(bucket)].append(item) for bucket in buckets: part = sorted(bucket, key=key) result.extend(part) return result
def main(input_file): PREAMBLE_LENGTH = 25 numbers = [] invalid_number = None with open(input_file) as f: while True: line = f.readline().strip() if not line: break number_under_test = int(line) if invalid_number is None and len(numbers) >= PREAMBLE_LENGTH: # We have enough numbers to start testing window = numbers[(-1 * PREAMBLE_LENGTH):].copy() logging.debug( "Number under test: {}".format(number_under_test)) logging.debug(window) if not sum_of_two(number_under_test, window): invalid_number = number_under_test logging.debug( "Invalid number {} found.".format(invalid_number)) break # We don't have to consider any numbers past this point, nor this number # TODO: Confirm this is valid. It worked but i'm not sure if this is true # Confirmed invalid assumption. Consider the malicious example 1,2,3,13,8,... with preamble 3 numbers.append(number_under_test) logging.debug(numbers) found_range = None for i in range(len(numbers)): range_sum = 0 end = i + 2 # At least 2 numbers in the contiguous range while range_sum < invalid_number and end < len(numbers): checking = numbers[i:end] range_sum = sum(checking) logging.debug("Checking {} Sum: {}".format(checking, range_sum)) if range_sum == invalid_number: found_range = checking logging.debug("Weakness range found: {}".format(found_range)) break end += 1 if found_range is not None: break smallest, largest = util.min_max(found_range) logging.debug("Smallest: {} Largest: {}".format(smallest, largest)) answer = smallest + largest logging.info("Answer: {}".format(answer))
def main(input_file): PREAMBLE_LENGTH = 25 numbers = [] invalid_number = None with open(input_file) as f: while True: line = f.readline().strip() if not line: break number_under_test = int(line) if invalid_number is None and len(numbers) >= PREAMBLE_LENGTH: # We have enough numbers to start testing window = numbers[(-1*PREAMBLE_LENGTH):].copy() logging.debug("Number under test: {}".format(number_under_test)) logging.debug(window) if not sum_of_two(number_under_test, window): invalid_number = number_under_test logging.debug("Invalid number {} found.".format(invalid_number)) numbers.append(number_under_test) logging.debug(numbers) found_range = None for i in range(len(numbers)): range_sum = 0 end = i + 2 while range_sum < invalid_number and end < len(numbers): checking = numbers[i:end] range_sum = sum(checking) logging.debug("Checking {} Sum: {}".format(checking, range_sum)) if range_sum == invalid_number: found_range = checking logging.debug("Weakness range found: {}".format(found_range)) break end += 1 if found_range is not None: break smallest, largest = util.min_max(found_range) logging.debug("Smallest: {} Largest: {}".format(smallest, largest)) answer = smallest + largest logging.info("Answer: {}".format(answer))
def browse_ms(company): print("Morning star") driver.get(url.morningstar) time.sleep(3) driver.find_element_by_xpath( '//*[@id="ctl00_ucHeader_txtQuote_txtAutoComplete"]').click() company_search = driver.find_element_by_xpath( '//*[@id="ctl00_ucHeader_txtQuote_txtAutoComplete"]') company_search.send_keys(company) time.sleep(3) driver.find_element_by_xpath('//*[@id="ui-id-1"]/li[1]').click() time.sleep(4) driver.find_element_by_xpath( '//*[@id="tabs"]/div/mds-button-group/div/slot/div/mds-button[2]/label/input' ).click() time.sleep(4) debt_equity = "Debt/Equity = " + str( driver.find_element_by_xpath( '//*[@id="siteContent"]/div[2]/div/div/div[1]/div/div/div[2]/div/div[2]/div/div[2]/div[2]/div/div[2]/ul/li[8]/div/div[2]' ).text) driver.find_element_by_xpath( '//*[@id="siteContent"]/div[2]/div/div/div[1]/div/div/div[2]/div/div[2]/div/div[2]/div[2]/div/div[2]/div[1]/a' ).click() time.sleep(4) driver.switch_to_window(driver.window_handles[1]) fcf = util.find_fcf(driver) + "\n" operating_margin_str = util.operating_margin(driver) + "\n" range_opm = util.min_max(operating_margin_str) + "\n" gross_margin_str = util.gross_margin(driver) + "\n" range_gpm = util.min_max(gross_margin_str) + "\n" final_str = debt_equity + "\n" + operating_margin_str + range_opm + gross_margin_str + range_gpm + fcf print(final_str) print(final_str, file=f)
def plot_feature_importance(feature_importances): feature = [ 'lattice type', 'space group', 'nspecies', 'natoms', 'volume/atom', 'volume/cell', r'$\rho$', 'atomic number', 'mendeleev number', 'period', 'group', 'mass', 'density', 'valence', 'radii', '$r_{cov}$', '$r_{vdw}$', 'electron affinity', 'electron negativity', 'ionization energy', '$T_b$', '$T_m$', 'molar volume', 'thermal conductivity', 'orbital exponent', 'polarizability', 'global hardness', 'electrophilicity', r'$\Delta H_{atomic}$', 'fusion enthalpy', 'vaporization enthalpy', 'binding energy' ] pos = range(len(feature_importances)) sorted_index = np.argsort(feature_importances) fig, ax1 = plt.subplots(figsize=(7, 8)) ax1.xaxis.grid(True, linestyle='-.', which='major', color='grey') ax1.tick_params(labelsize=12) # plt.grid(True, axis='x',which='major', ls='-.') sorted_feature = feature_importances[sorted_index] min_max_color = min_max(sorted_feature) min_max_color = np.power(min_max_color, 1 / 2) print(min_max_color) bar_color = [ tuple([0.1803921568627451, 0.3411764705882353, 0.5019607843137255, i]) for i in min_max_color ] # bar_color = [tuple([0.49019607843137253, 0.6745098039215687, 0.8, i]) for i in min_max_color] ax1.barh(pos, sorted_feature, align='center', color=bar_color) FP = FontProperties(family='Times New Roman', size=14) plt.yticks(pos, np.array(feature)[sorted_index], fontproperties=FP) plt.xticks(fontproperties=FP) plt.xlabel('Importance', size=18, fontdict={'family': 'Times New Roman'}) SAVE = False if SAVE: plt.savefig('./fig/feature_importance.png', dpi=600, bbox_inches='tight') plt.show()
for i in range(1, 4995): if os.path.exists(f'data/hot_entry/masuda_{i}.json'): hot_df = pd.read_json(f'data/hot_entry/masuda_{i}.json') hot_concat.append(hot_df) hot_concat_df = pd.concat(hot_concat) not_hot_concat = [] for i in range(2, 5001): if os.path.exists(f'data/entry/masuda_{i}.json'): not_hot_df = pd.read_json(f'data/entry/masuda_{i}.json') not_hot_concat.append(not_hot_df) not_hot_concat_df = pd.concat(not_hot_concat) all_concat_df = pd.concat([hot_concat_df, not_hot_concat_df]) all_concat_df.bookmark_count = util.min_max( all_concat_df.bookmark_count.tolist()) all_concat_df.num_of_sentences = util.min_max( all_concat_df.num_of_sentences.tolist()) all_concat_df.num_of_comma = util.min_max(all_concat_df.num_of_comma.tolist()) all_concat_df.num_of_period = util.min_max( all_concat_df.num_of_period.tolist()) all_concat_df.num_of_exclamation_mark = util.min_max( all_concat_df.num_of_exclamation_mark.tolist()) all_concat_df.num_of_question_mark = util.min_max( all_concat_df.num_of_question_mark.tolist()) all_concat_df.length_of_text = util.min_max( all_concat_df.length_of_text.tolist()) all_concat_df.distance_btw_commas = util.min_max( all_concat_df.distance_btw_commas.tolist()) all_concat_df.kanji_content_rate = util.min_max( all_concat_df.kanji_content_rate.tolist())