def __init__(self, opts, load=False): """Class to handle data management Args: opts: All the hyper-parameters of the network load: Load dataset if this is true """ self.records = {} self.opts = opts self.check_records() if load: if opts.load_images: print ' - Loading numpy raw images...' begin_time = time.time() self.t_images_data = np.load(os.path.join(opts.dataset_dir, opts.dataset, 'train.npy')) print ' - Loaded {} images in: {} seconds'.format(len(self.t_images_data) * 2, time.time() - begin_time) else: self.t_image_paths = utils.read_file_lines( os.path.join(opts.dataset_dir, opts.dataset, 'train.txt')) self.v_image_paths = utils.read_file_lines( os.path.join(opts.dataset_dir, opts.dataset, 'val.txt')) test_path = os.path.join(opts.dataset_dir, opts.dataset, 'test.txt') if utils.path_exists(test_path): self.test_image_paths = utils.read_file_lines( os.path.join(opts.dataset_dir, opts.dataset, 'test.txt'))
def read_passwords(filename): dict_passports = [] passports = [ passport.split() for passport in utils.read_file_lines(filename, '\n\n') ] for passport in passports: dict_passport = {} for passport_part in passport: key, value = passport_part.split(':') dict_passport[key] = value dict_passports.append(dict_passport) return dict_passports
def main(url: str, proxies: str, view_count: int): channel, post_id = re.search(post_url_re, url).groups() post = Post(channel, post_id) proxies = read_file_lines(proxies) if view_count == -1 or len(proxies) < view_count: view_count = len(proxies) with yaspin() as spinner: for proxy in proxies[:view_count]: session = create_session_using_proxy(f"socks5h://{proxy}") key = get_key(session, post) if add_view(session, post, key): spinner.text = f"{Fore.GREEN}{proxy}{Style.RESET_ALL}" else: spinner.text = f"{Fore.RED}{proxy}{Style.RESET_ALL}"
no_child_recursions += 1 elif adapter + interval in adapters: no_child_recursions += recurse_adapters( adapters, adapter + interval, cache) cache[adapter] = no_child_recursions return no_child_recursions def count_combinations(adapters): cache = {} return recurse_adapters(adapters, min(adapters), cache) if __name__ == '__main__': adapters = [ int(number) for number in utils.read_file_lines('input/day10.test') ] adapters = sort_and_prepare(adapters) assert get_adapter_intervals_product(adapters) == 35 assert count_combinations(adapters) == 8 adapters = [ int(number) for number in utils.read_file_lines('input/day10.test2') ] adapters = sort_and_prepare(adapters) assert get_adapter_intervals_product(adapters) == 220 assert count_combinations(adapters) == 19208 adapters = [ int(number) for number in utils.read_file_lines('input/day10.input') ]
return [ index for index, field in index_field_xref.items() if field.startswith(field_prefix) ] def get_ticket_value_product(ticket, value_indexes): ticket_values = [ int(value) for index, value in enumerate(ticket) if index in value_indexes ] return math.prod(ticket_values) if __name__ == '__main__': input_sections = utils.read_file_lines('input/day16.test', '\n\n') rules, my_ticket, nearby_tickets = parse_input_sections(input_sections) assert sum_invalid_ticket_values(rules, nearby_tickets) == 71 input_sections = utils.read_file_lines('input/day16.test2', '\n\n') rules, my_ticket, nearby_tickets = parse_input_sections(input_sections) value_indexes = get_value_indexes( rules, filter_valid_tickets(rules, nearby_tickets)) assert get_ticket_value_product(my_ticket, value_indexes) == 1716 input_sections = utils.read_file_lines('input/day16.input', '\n\n') rules, my_ticket, nearby_tickets = parse_input_sections(input_sections) print(sum_invalid_ticket_values(rules, nearby_tickets)) value_indexes = get_value_indexes( rules, filter_valid_tickets(rules, nearby_tickets), 'departure') print(get_ticket_value_product(my_ticket, value_indexes))
# -*- coding: utf-8 -*- from __future__ import division, print_function import sys from utils import read_file_lines from algos import all_offsets_approximate, write_offsets path = sys.argv[1] print(path) lines = read_file_lines(path) print(len(lines)) pattern, text, k = lines k = int(k) print('text = %d "%s...%s"' % (len(text), text[:10], text[-10:])) print('pattern = %d "%s"' % (len(pattern), pattern[:10])) print('k=%d' % k) offsets = all_offsets_approximate(text, pattern, k) out_path = path + '.offsets' print(out_path) write_offsets(out_path, offsets)
current_range = (min_val, max_val) return int(current_range[0]) def get_seat_id(boarding_pass): row = traverse_binary_tree(boarding_pass[0:7], 'F', 'B') column = traverse_binary_tree(boarding_pass[7:10], 'L', 'R') return row * 8 + column def get_seat_ids(boarding_passes): return [get_seat_id(boarding_pass) for boarding_pass in boarding_passes] def find_missing_seat_id(seat_ids): all_seat_ids = [ seat_id for seat_id in range(min(seat_ids), max(seat_ids) + 1) ] return set(all_seat_ids).difference(set(seat_ids)).pop() if __name__ == '__main__': boarding_passes = utils.read_file_lines('input/day05.test') assert max(get_seat_ids(boarding_passes)) == 820 boarding_passes = utils.read_file_lines('input/day05.input') seat_ids = get_seat_ids(boarding_passes) print(max(seat_ids)) print(find_missing_seat_id(seat_ids))
return password.count(character) >= param1 and password.count( character) <= param2 def is_valid_toboggan(param1, param2, character, password): return (password[param1 - 1] == character) != (password[param2 - 1] == character) def find_valid_passwords(policy_passwords, is_valid_func): no_valid_passwords = 0 for policy_password in policy_passwords: match = re.match(r'(\d+)-(\d+) (.): (.*)', policy_password) param1, param2, character, password = int(match.group(1)), int( match.group(2)), match.group(3), match.group(4) if is_valid_func(param1, param2, character, password): no_valid_passwords += 1 return no_valid_passwords if __name__ == '__main__': policy_passwords = utils.read_file_lines('input/day02.test') assert find_valid_passwords(policy_passwords, is_valid_sled_rental) == 2 assert find_valid_passwords(policy_passwords, is_valid_toboggan) == 1 policy_passwords = utils.read_file_lines('input/day02.input') print(find_valid_passwords(policy_passwords, is_valid_sled_rental)) print(find_valid_passwords(policy_passwords, is_valid_toboggan))
current_position += 1 if current_position in visited_positions: return (1, accumulator) elif current_position == len(instructions): return (0, accumulator) def repair_instructions(instructions): possibly_corrupted_positions = [] for position in range(0, len(instructions)): if instructions[position][0] in ['nop', 'jmp']: possibly_corrupted_positions.append(position) for possibly_corrupted_position in possibly_corrupted_positions: return_code, accumulator = execute_instructions( instructions, possibly_corrupted_position) if return_code == 0: return accumulator if __name__ == '__main__': lines = utils.read_file_lines('input/day08.test') instructions = parse_instructions(lines) assert execute_instructions(instructions) == (1, 5) assert repair_instructions(instructions) == 8 lines = utils.read_file_lines('input/day08.input') instructions = parse_instructions(lines) print(execute_instructions(instructions)[1]) print(repair_instructions(instructions))
import itertools import utils # noqa import sys def get_donors(is_donor): filtr = (FacebookUser.donor == is_donor) return filtr FILTER_FUNS_K = { 'age': lambda x: queries.age(age=[int(x.split('-')[0]), int(x.split('-')[-1])]), 'sex': lambda x: queries.sex(sex=x), 'currentcity': lambda _x: queries.currentCityInList( utils.read_file_lines(_x.split('NOT::')[-1], int), opposite=("NOT::" in _x)), 'employer': lambda _x: queries.employerInList( utils.read_file_lines(_x.split('NOT::')[-1], str), opposite=("NOT::" in _x)), 'donor': get_donors } CATEGORIES_K = { 'musical': ["album", "concert tour", "concert venue", "music", "music award", "music chart", "music video", "musical genre", "musical instrument", "musician/band", "song"], 'art': ["art", "artist", "arts/entertainment/nightlife", "arts/humanities website", "dancer",
# http://rosalind.info/problems/subs/ import string from utils import read_file_lines s, t = read_file_lines() n = 0 results = list() while True: n = string.find(s, t, n + 1) if n == -1: break results.append(n + 1) print " ".join(str(i) for i in results)
ship_y = 0 waypoint_x_offset = 10 waypoint_y_offset = 1 for instruction in instructions: action, value = instruction if action == 'F': ship_x += waypoint_x_offset * value ship_y += waypoint_y_offset * value elif action == 'L': waypoint_x_offset, waypoint_y_offset = rotate_waypoint(waypoint_x_offset, waypoint_y_offset, 360 - value) elif action == 'R': waypoint_x_offset, waypoint_y_offset = rotate_waypoint(waypoint_x_offset, waypoint_y_offset, value) else: new_waypoint_degrees = DIRECTION_DEGREES_XREF[action] waypoint_x_offset += int(math.sin(math.radians(new_waypoint_degrees)) * value) waypoint_y_offset += int(math.cos(math.radians(new_waypoint_degrees)) * value) return abs(ship_x) + abs(ship_y) if __name__ == '__main__': instructions = parse_instructions(utils.read_file_lines('input/day12.test')) assert travel(instructions) == 25 assert travel_with_waypoint(instructions) == 286 instructions = parse_instructions(utils.read_file_lines('input/day12.input')) print(travel(instructions)) print(travel_with_waypoint(instructions))
recurse_enclosing_bags(bags, STARTING_BAG, seen_bags) return (len(seen_bags)) def recurse_enclosed_bags(bags, enclosing_bag, enclosed_bags): for bag in bags[enclosing_bag]: enclosed_bags.append(bag) recurse_enclosed_bags(bags, bag, enclosed_bags) def count_enclosed_bags(bags): enclosed_bags = [] recurse_enclosed_bags(bags, STARTING_BAG, enclosed_bags) return len(enclosed_bags) if __name__ == '__main__': rules = utils.read_file_lines('input/day07.test') bags = load_bags(rules) assert count_enclosing_bags(bags) == 4 assert count_enclosed_bags(bags) == 32 rules = utils.read_file_lines('input/day07.test2') bags = load_bags(rules) assert count_enclosed_bags(bags) == 126 rules = utils.read_file_lines('input/day07.input') bags = load_bags(rules) print(count_enclosing_bags(bags)) print(count_enclosed_bags(bags))
tokens = custom_tokenize(line, wordpiece_tokenizer) token_ids = wordpiece_tokenizer.convert_tokens_to_ids(tokens) #print(tokens) #print(token_ids) return tokens, token_ids def write_output(raw_lines, tokens_file, token_ids_file): tuples = Parallel(n_jobs=1)(delayed(get_tuple)(raw_lines[i]) for i in tqdm(range(len(raw_lines)))) for i in range(len(tuples)): tokens, token_ids = tuples[i] # Write text output tokens_file.write(' '.join(tokens)) token_ids_file.write(' '.join(str(x) for x in token_ids)) tokens_file.write('\n') token_ids_file.write('\n') return if __name__ == "__main__": incorrect_lines = read_file_lines(FLAGS.input, 'incorrect lines') with open_w(FLAGS.output_tokens) as tokens_file,\ open_w(FLAGS.output_token_ids) as token_ids_file: pretty.pheader('Tokenizing Incorrect sentences') write_output(incorrect_lines, tokens_file, token_ids_file)
import utils def find_entries(entries, number_of_entries): for first_entry_index, first_entry in enumerate(entries): for second_entry_index, second_entry in enumerate( entries[first_entry_index + 1:]): if number_of_entries == 2: if first_entry + second_entry == 2020: return first_entry * second_entry elif number_of_entries == 3: for third_entry in entries[second_entry_index + 1:]: if first_entry + second_entry + third_entry == 2020: return first_entry * second_entry * third_entry if __name__ == '__main__': entries = [ int(entry) for entry in utils.read_file_lines('input/day01.test') ] assert find_entries(entries, 2) == 514579 assert find_entries(entries, 3) == 241861950 entries = [ int(entry) for entry in utils.read_file_lines('input/day01.input') ] print(find_entries(entries, 2)) print(find_entries(entries, 3))
if len(floating_bits) == 1: memory[floating_bit_address] = int(value) else: write_to_memory(memory, floating_bit_address, mask, floating_bits[1:], value) def run_initializion_program2(lines): memory = {} mask = None for line in lines: if line.startswith('mask'): mask = line[7:] else: address, value = get_address_value(line) base_address, floating_bits = apply_mask2(address, mask) write_to_memory(memory, base_address, mask, floating_bits, value) return sum(memory.values()) if __name__ == '__main__': lines = utils.read_file_lines('input/day14.test') assert run_initializion_program(lines) == 165 lines = utils.read_file_lines('input/day14.test2') assert run_initializion_program2(lines) == 208 lines = utils.read_file_lines('input/day14.input') print(run_initializion_program(lines)) print(run_initializion_program2(lines))
# -*- coding: utf-8 -*- """ https://stepic.org/lesson/Some-Hidden-Messages-are-More-Elusive-than-Others-9/step/8?course=Bioinformatics-Algorithms&unit=407 """ from __future__ import division, print_function import sys import time from utils import read_file_lines from algos import most_frequent_approximate_kmers path = sys.argv[1] print(path) lines = read_file_lines(path) print(len(lines)) text, kd = lines k, d = kd.split() k, d = int(k), int(d) print('text = %d "%s...%s"' % (len(text), text[:10], text[-10:])) print('k=%d' % k) print('d=%d' % d) t0 = time.clock() frequent_kmers = most_frequent_approximate_kmers(text, k, d, add_rc=True) t1 = time.clock() print(' '.join(frequent_kmers)) print('time=%.1f sec' % (t1 - t0))
# OK, next bus current_bus_id_index += 1 else: # Not ok, start with the first bus again but skip the product of all bus ids currently in the correct sequence first_bus_departure += math.prod( get_int_bus_ids(bus_ids[0:current_bus_id_index])) current_timestamp = first_bus_departure current_bus_id_index = 1 current_timestamp += 1 return first_bus_departure if __name__ == '__main__': earliest_departure, bus_ids = parse_bus_data( utils.read_file_lines('input/day13.test')) bus_departure, departing_bus_id = get_earliest_bus(earliest_departure, bus_ids) assert (bus_departure - earliest_departure) * departing_bus_id == 295 assert find_sequence_of_buses(bus_ids) == 1068781 assert find_sequence_of_buses([17, 'x', 13, 19]) == 3417 assert find_sequence_of_buses([67, 7, 59, 61]) == 754018 assert find_sequence_of_buses([67, 'x', 7, 59, 61]) == 779210 assert find_sequence_of_buses([67, 7, 'x', 59, 61]) == 1261476 assert find_sequence_of_buses([1789, 37, 47, 1889]) == 1202161486 earliest_departure, bus_ids = parse_bus_data( utils.read_file_lines('input/day13.input')) bus_departure, departing_bus_id = get_earliest_bus(earliest_departure, bus_ids) print((bus_departure - earliest_departure) * departing_bus_id)
result.pop() result[-1]="n't" ''' def split_and_convert_to_ints(words_uncorrected,edits): words_uncorrected = words_uncorrected.split(' ') edits = edits.split(' ')[0:len(words_uncorrected)] edits = list(map(int, edits)) return words_uncorrected, edits if __name__=="__main__": corrected = [] pretty.pheader('Reading Input') edits = read_file_lines(config.INPUT_EDITS) #uncorrected = read_file_lines(config.INPUT_UNCORRECTED) words_uncorrected = read_file_lines(config.INPUT_UNCORRECTED_WORDS) if len(edits) != len(words_uncorrected): pretty.fail('FATAL ERROR: Lengths of edits and uncorrected files not equal') exit() pretty.pheader('Splitting and converting to integers') if not DO_PARALLEL: for i in tqdm(range(len(edits))): edits[i] = list(map(int, edits[i].split(' '))) #uncorrected[i] = list(map(int, uncorrected[i].split(' '))) words_uncorrected[i] = words_uncorrected[i].split(' ')
import sys from tree import fill_govern_tag, op_tags, find_pot_aspects, noun_tags, count_node_aspects, decompse_sent from utils import read_file_lines, convert_lines_to_trees, write_file_lines, Aspect # Decompose sentences with aspects by the aspects as pivots to decompose the sentence. if __name__ == "__main__": files = ['data/semeval15/train', 'data/semeval15/dev', 'data/semeval15/test', 'data/semeval16/train', 'data/semeval16/dev', 'data/semeval16/test'] for file in files: lines = read_file_lines(file + "_parse") sents = list() for line in lines: line_splitted = line.split(u'||||') tree = convert_lines_to_trees([line_splitted[0]])[0] aspects = list() for aspect in line_splitted[1:]: aspect_splitted = aspect.split(u'<->') aspects.append(Aspect(None, aspect_splitted[0], aspect_splitted[1])) # Count the number of aspects governed by each node. count_node_aspects(tree, aspects) """ Find which nodes govern opinions (i.e. a node governs an opinion if one of its leafs decendants contains an adjective or a verb). """ fill_govern_tag(tree, op_tags, aspects) # Decompose the sentence to several fragments, where each fragment is linked to an aspect. decompse_sent(tree, aspects) label = tree.label sent_string = tree.phrase + u"<->6"
# http://rosalind.info/problems/hamm/ from utils import read_file_lines hamm = len(filter(lambda x: x[0] != x[1], zip(*read_file_lines()))) print(hamm)
import os import pickle import sys from models import BiLSTM from utils import aspects_sent_convert, read_file_lines, write_file_lines from import_dy import dy from random_inits import np # Use a sentence level classifier to automatically label sentences. if __name__ == "__main__": label_files = sys.argv[1].split(',') lines = list() for label_file in label_files: lines += read_file_lines(label_file) model = dy.ParameterCollection() W2I = pickle.load(open("models/SentenceLevelBiLSTM" + "/Model.meta", "rb")) network = BiLSTM(1, 300, 150, W2I, model) network.load_model("models/SentenceLevelBiLSTM/Model") new_samples = list() for i in range(0, len(lines), 1000): print str(i) + " Sentences Labeled" samples = aspects_sent_convert(lines[i:i + 1000]) for sample in samples: dy.renew_cg() sent = sample.sent h = network(sent[0]) dist = network.classify(h)
def traverse_map(tree_map, x_speed, y_speed): encountered_trees = 0 current_x = 0 for current_y in range(0, len(tree_map), y_speed): if tree_map[current_y][current_x % len(tree_map[0])] == '#': encountered_trees += 1 current_x += x_speed return encountered_trees if __name__ == '__main__': tree_map = utils.read_file_lines('input/day03.test') assert traverse_map(tree_map, 3, 1) == 7 slopes_product = traverse_map(tree_map, 1, 1) slopes_product *= traverse_map(tree_map, 3, 1) slopes_product *= traverse_map(tree_map, 5, 1) slopes_product *= traverse_map(tree_map, 7, 1) slopes_product *= traverse_map(tree_map, 1, 2) assert slopes_product == 336 tree_map = utils.read_file_lines('input/day03.input') print(traverse_map(tree_map, 3, 1)) slopes_product = traverse_map(tree_map, 1, 1) slopes_product *= traverse_map(tree_map, 3, 1) slopes_product *= traverse_map(tree_map, 5, 1) slopes_product *= traverse_map(tree_map, 7, 1) slopes_product *= traverse_map(tree_map, 1, 2)
import utils def count_yes_answers(groups): return sum(len(set(''.join(group))) for group in groups) def count_unanimous_answers(groups): return sum(len(set(group[0]).intersection(*group)) for group in groups) if __name__ == '__main__': groups = [ group.split('\n') for group in utils.read_file_lines('input/day06.test', '\n\n') ] assert count_yes_answers(groups) == 11 assert count_unanimous_answers(groups) == 6 groups = [ group.split('\n') for group in utils.read_file_lines('input/day06.input', '\n\n') ] print(count_yes_answers(groups)) print(count_unanimous_answers(groups))