def _write_data_frame_to_csv_file(data_frame, filename, mode='w', header=True): try: file_path = get_file_path(__file__, f'../database/{filename}.csv') data_frame.to_csv(file_path, index=False, mode=mode, header=header) except FileNotFoundError: raise FailedToReadFromCsvException( 'Failed to read from csv, file not found. ')
def _get_data_frame_from_csv_file(filename) -> DataFrame: try: file_path = get_file_path(__file__, f'../database/{filename}.csv') return pd.read_csv(file_path) except FileNotFoundError: raise FailedToReadFromCsvException( 'Failed to read from csv, file not found. ')
def compare_files(filename_one, filename_two): """ Receives filenames as parameters, compares the list of lines received Returns the tuple containing the plagiarism percentage """ if utils.check_file( utils.get_file_path(filename_one)) and utils.check_file( utils.get_file_path(filename_two)): a, b = utils.extract_files(filename_one, filename_two, True) write_to_file(a, "1") write_to_file(b, "2") file_one = open(utils.get_file_path('type_three_dump_1.py')) file_two = open(utils.get_file_path('type_three_dump_2.py')) if filename_one.split(".")[1] == "py": try: results = pycode_ast.detect([file_one.read(), file_two.read()]) except: print('Error: Non working python code') return 60, 60 for _, ast_list in results: total_count = sum(diff_info.total_count for diff_info in ast_list) plagiarism_count = sum(diff_info.plagiarism_count for diff_info in ast_list) file_one_plagiarism_percentage = utils.get_plagiarism_percentage( plagiarism_count, total_count) return file_one_plagiarism_percentage, file_one_plagiarism_percentage else: try: text_one, text_two = utils.extract_files( filename_one, filename_two, True) c_utility(text_one, 1) c_utility(text_two, 2) return type_zero.compare_files("type_three_dump_1.py", "type_three_dump_2.py") except: print("Error: Non working c/cpp files") return 70, 70 else: print("Error file format not supported")
def compare_files(filename_one, filename_two): """ Receives filenames as parameters, compares the list of lines received Returns the tuple containing the plagiarism percentage """ if utils.check_file( utils.get_file_path(filename_one)) and utils.check_file( utils.get_file_path(filename_two)): file_one_tokens, file_two_tokens = [], [] file_one_clean, file_two_clean = utils.extract_files( filename_one, filename_two, True) print(file_one_clean) print(file_two_clean) if filename_one.split(".")[1] == "py": write_to_file(file_one_clean, "1") write_to_file(file_two_clean, "2") token_generator('./dataset/type_two_dump_1.py', file_one_tokens) token_generator('./dataset/type_two_dump_2.py', file_two_tokens) # print("yyyyyyyyyyy") else: file_one_tokens = c_token_generator(filename_one) file_two_tokens = c_token_generator(filename_two) # print(file_one_tokens) # print(file_two_tokens) ignore_a = '' string_a = ",".join(str(e) for e in file_one_tokens) string_b = ",".join(str(e) for e in file_two_tokens) number_of_lines_plagiarised = list( match(string_a, ignore_a, string_b, ignore_a, 6)[0])[2] # print(string_a) # print(string_b) # print(match(string_a, ignore_a, string_b, ignore_a, 3)[0]) file_two_plagiarism_score = utils.get_plagiarism_percentage( number_of_lines_plagiarised * 2, len(string_a) + len(string_b)) return file_two_plagiarism_score, file_two_plagiarism_score # return type_one.compare_files('type_two_dump_1.py', 'type_two_dump_2.py') else: print("Error file format not supported")
def compare_files(filename_one, filename_two): """ Receives filenames as parameters, compares the list of lines received Returns the tuple containing the plagiarism percentage """ if utils.check_file(utils.get_file_path(filename_one)) and utils.check_file(utils.get_file_path(filename_two)): file_one = open(utils.get_file_path(filename_one)) file_two = open(utils.get_file_path(filename_two)) file_one_tokens, file_two_tokens = [], [] file_one_clean, file_two_clean = utils.extract_files(filename_one, filename_two, True) if filename_one.split(".")[1] == "py": write_to_file(file_one_clean) file_one = open(utils.get_file_path('type_two_dump.py')) token_generator(file_one, file_one_tokens) write_to_file(file_two_clean) file_two = open(utils.get_file_path('type_two_dump.py')) token_generator(file_two, file_two_tokens) else: file_one_tokens = c_token_generator(filename_one) file_two_tokens = c_token_generator(filename_two) counter_one = Counter(file_one_tokens) counter_two = Counter(file_two_tokens) plagiarism_counter = counter_one & counter_two number_of_lines_plagiarised = sum(plagiarism_counter.values()) file_one_plagiarism_percentage = utils.get_plagiarism_percentage(number_of_lines_plagiarised * 2, sum(counter_one.values()) + sum(counter_two.values())) return file_one_plagiarism_percentage, file_one_plagiarism_percentage else: print("Error file format not supported")
def upload(): session_id = str(uuid.uuid4()) if request.method == "POST": session_id = session["uid"] f = request.files.get("file") file_path = get_file_path(session_id) f.save(file_path) os.system(f"./scripts/copy_js.sh {session_id}") sleep(1) pipeline = Path(file_path).read_text() model = Model(session_id=session_id, pipeline=pipeline) database.db.session.add(model) database.db.session.commit() session["uid"] = str(uuid.uuid4()) return render_template("cover.html", session_id=session["uid"])
def pipeline(name): print(name) print(request.url) _pipe_exists = check_if_exists(name) if not _pipe_exists: pipe = Model.query.filter_by(session_id=name).first() if not pipe: print("lolno") return redirect(url_for("upload")) else: print(pipe.pipeline) file_path = get_file_path(name) Path(file_path).write_text(pipe.pipeline) os.system(f"./scripts/copy_js.sh {name}") sleep(1) url = request.url return render_template("pipe.html", name=name, url=url)
def compute_checksum(spreadsheet: List[List[int]]) -> int: max_min = [(max(row), min(row)) for row in spreadsheet] return sum(max_value - min_value for max_value, min_value in max_min) def compute_evenly_divisible(spreadsheet: List[List[int]]) -> int: values = [extract_division_values(row) for row in spreadsheet] return sum(dividend // divisor for dividend, divisor in values) def extract_division_values(row: List[int]) -> Tuple[int, int]: for divisor in row: for dividend in row: if dividend != divisor and dividend % divisor == 0: return dividend, divisor if __name__ == '__main__': input_file_path = get_file_path('corrupted_checksum_1.txt') with open(input_file_path) as f: raw_input = f.read() puzzle_input = parse_input(raw_input) checksum_output = compute_checksum(puzzle_input) print(f'Result for Part 1: {checksum_output}') evenly_divisible_output = compute_evenly_divisible(puzzle_input) print(f'Result for Part 2: {evenly_divisible_output}')
return [parse_row(row) for row in puzzle_input] def parse_row(row): splitted_row = row.split('->') first_fragment = splitted_row[0].split(' ') name, weight = first_fragment[0], int(first_fragment[1][1:-1]) if len(splitted_row) > 1: sub_names = [n.strip() for n in splitted_row[1].split(',')] else: sub_names = [] return name, weight, sub_names if __name__ == '__main__': input_file = get_file_path('recursive_circus.txt') with open(input_file) as f: raw_puzzle_input = [line.strip() for line in f.readlines()] puzzle_input = parse_input(raw_puzzle_input) root_program = find_tree_root(puzzle_input) print(f'Result for Part 1: {root_program}') unbalanced_node, wrong_weight, correct_weight = get_unbalanced_sub_node_correct_weight( puzzle_input) print( f'Result for Part 2: node {unbalanced_node} has weight {wrong_weight}, should be {correct_weight}' )
def c_token_generator(filename): tok = custom_tokenizer.Tokenizer(utils.get_file_path(filename)) results = tok.full_tokenize() return results
def main(radius, origin_lat, origin_lon, customer_file): # create location object of origin to compare distance with other location origin = Location('dublin', origin_lat, origin_lon) customers = get_customers(customer_file) filter_criteria = FilterCriteria.get_filter(0, radius) invited_customers = get_customer_within_radius(customers, origin, filter_criteria) fields = ['user_id', 'name'] # fields that need to be printed of customers report(invited_customers, fields) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-radius', type=float, default=RADIUS, help='Radius which will be used as a filter criteria') parser.add_argument('-latitude', type=float, default=ORIGIN_LATITUDE, help='Latitude of origin from where distance will be calculated') parser.add_argument('-longitude', type=float, default=ORIGIN_LONGITUDE, help='Longitude of origin from where distance will be calculated') parser.add_argument('file', type=str, help='File path which contains data of customers') args = parser.parse_args() file_path = get_file_path(args.file) main(args.radius, args.latitude, args.longitude, file_path)
max_value_idx = memory_banks.index(max_value) memory_banks[max_value_idx] = 0 current_bank_idx = max_value_idx + 1 while max_value > 0: current_bank_idx = wrap_index(current_bank_idx, memory_banks) memory_banks[current_bank_idx] += 1 max_value -= 1 current_bank_idx += 1 return memory_banks def parse_input(raw_in): return [int(x) for x in raw_in.split('\t')] if __name__ == '__main__': input_file = get_file_path('memory_reallocation.txt') with open(input_file) as f: raw_puzzle_input = f.readline() puzzle_input = parse_input(raw_puzzle_input) reallocation_before_loop, final_configuration = cycles_before_infinite_loops( puzzle_input) print(f'Result for Part 1: {reallocation_before_loop}') reallocation_before_loop, final_configuration = cycles_before_infinite_loops( final_configuration) print(f'Result for Part 1: {reallocation_before_loop - 1}')
def process_instruction(acc, instruction): registers = acc[-1] apply, apply_if = instruction updated_registers = apply(registers) if apply_if(registers) else registers acc.append(updated_registers) return acc def parse_input(unparsed_input): return [parse_row(row) for row in unparsed_input] def parse_row(row): target_register, op, value, _, compared_register, compare_op, compared_value = row.split(' ') return (target_register, op, int(value)), (compared_register, compare_op, int(compared_value)) if __name__ == '__main__': file_path = get_file_path('registers.txt') with open(file_path) as f: raw_puzzle_input = [line.strip() for line in f.readlines()] instructions = [eval_row(row) for row in parse_input(raw_puzzle_input)] max_value, absolute_max = get_max_values(instructions) print(f'Result for Part 1: {max_value}') print(f'Result for Part 2: {absolute_max}')
is_garbage = False counter = 0 for c in stream: if c == '>': is_garbage = False continue if c == '<': if is_garbage: counter += 1 is_garbage = True continue if is_garbage: counter += 1 return counter if __name__ == '__main__': # Avoid RecursionError with the default limit (1000) setrecursionlimit(5000) file_path = get_file_path('stream_processing.txt') with open(file_path) as f: puzzle_input = f.readline().strip() group_n = count_groups(puzzle_input) print(f'Result for Part 1: {group_n}') garbage_count = count_garbage(puzzle_input) print(f'Result for Part 2: {garbage_count}')
def is_passphrase_valid(passphrase): words = passphrase.split() return len(set(words)) == len(words) def is_passphrase_valid_anagrams(passphrase): words = passphrase.split() return contain_anagrams(words) is False def contain_anagrams(words): sorted_words = [''.join(sorted(w)) for w in words] return len(set(sorted_words)) != len(sorted_words) if __name__ == '__main__': input_file_path = get_file_path('passphrases.txt') with open(input_file_path) as f: passphrases = [line.strip() for line in f.readlines()] valid_passphrases = sum(1 for passphrase in passphrases if is_passphrase_valid(passphrase)) print(f'Result for Part 1: {valid_passphrases}') valid_passphrases = sum(1 for passphrase in passphrases if is_passphrase_valid_anagrams(passphrase)) print(f'Result for Part 2: {valid_passphrases}')