def sum_path(): n = 1 pos = (0, 0) side_length = 1 sum_dict = {(0, 0): 1} step_fns = it.cycle([ lambda x: (x[0] + 1, x[1]), lambda x: (x[0], x[1] + 1), lambda x: (x[0] - 1, x[1]), lambda x: (x[0], x[1] - 1) ]) step_fn = next(step_fns) rotation_break_seq = set() while True: if is_odd_square(n - 1): step_fn = next(step_fns) side_length += 2 delta_seq = [ side_length - 2, side_length - 2 + 1, side_length - 2 + 1 ] rotation_break_seq = cc.pipe(it.accumulate([n] + delta_seq), cc.drop(1), set) elif n in rotation_break_seq: step_fn = next(step_fns) sum_dict[pos] = neighbors_sum(pos, sum_dict) yield (pos, sum_dict[pos]) pos = step_fn(pos) n += 1
def odd_square_lower_bound_and_ring(num): if num < 1: return (None, None) ring = it.count(start=1) odd_squares = (x for x in it.count() if is_odd_square(x)) odd_squares_lower, odd_squares_upper = it.tee(odd_squares) odd_squares_upper = cc.drop(1, odd_squares_upper) for square_tuple in zip(odd_squares_lower, odd_squares_upper, ring): if square_tuple[0] <= num <= square_tuple[1]: return (square_tuple[0], square_tuple[2])
def join_flatten(nr_common_cols: int, unflattened_list: list) -> Any: """Removes the common columns of the second list after a join operation on two lists Concatenates the two lists after removal :param nr_common_cols: :param unflattened_list: :return: """ return map( compose(list, concat, juxt(first, compose(drop(nr_common_cols), second))))(unflattened_list)
def process(workbook: Workbook, content: str) -> list: """Process SP-Frontend-Ports worksheet :param workbook: :param content: :return: """ worksheet_name = 'SP-Frontend-Ports' worksheet = workbook.get_sheet_by_name(worksheet_name) headers = list( concat([ get_parser_header(PORT_TMPL), get_parser_header(SPPORTSPEED_TMPL)[3:], ])) RowTuple = namedtuple('RowTuple', headers) # pylint: disable=invalid-name build_header(worksheet, headers) cmd_port_out = run_parser_over(content, PORT_TMPL) cmd_spportspeed_out = run_parser_over(content, SPPORTSPEED_TMPL) common_columns = (0, 1, 2) common_columns_getter = itemgetter(*common_columns) cmd_merged_out = join(common_columns_getter, cmd_port_out, common_columns_getter, cmd_spportspeed_out) rows = map(compose(list, concat, juxt(first, compose(drop(3), second))))(cmd_merged_out) rows = check_empty_arrays(list(unique(rows, key=common_columns_getter))) final_col, final_row = 0, 0 for row_n, row_tuple in enumerate(map(RowTuple._make, rows), 2): for col_n, col_value in \ enumerate(row_tuple._asdict().values(), ord('A')): cell = worksheet['{}{}'.format(chr(col_n), row_n)] cell.value = str.strip(col_value) style_value_cell(cell) set_cell_to_number(cell) final_col = col_n final_row = row_n sheet_process_output(worksheet, 'SPFrontendPortsTable', 'SP-Frontend-Ports', final_col, final_row) return rows
def get_position(num): odd_square_lower_bound, ring = odd_square_lower_bound_and_ring(num) if odd_square_lower_bound is None: return (None, None) if num == 1: return (0, 0) if is_odd_square(num): mag = int(sqrt(odd_square_lower_bound)) - ring + 1 return (mag, -mag) base_position = get_position(odd_square_lower_bound) base_position = (base_position[0] + 1, base_position[1]) base_seq = odd_square_lower_bound + 1 side_length = base_position[0] * 2 + 1 step_fns = [ lambda x: (x[0], x[1] + 1), lambda x: (x[0] - 1, x[1]), lambda x: (x[0], x[1] - 1), lambda x: (x[0] + 1, x[1]) ] delta_seq = [side_length - 2, side_length - 2 + 1, side_length - 2 + 1] rotation_break_seq = cc.pipe(it.accumulate([base_seq] + delta_seq), cc.drop(1), set) position = base_position while base_seq < num: position = step_fns[0](position) base_seq += 1 if base_seq in rotation_break_seq: step_fns = step_fns[1:] return position
#!/usr/bin/env python import cytoolz.curried as cc from pprint import pprint as pp import sys data_input = sys.stdin.read().replace('\n', '') data_input += data_input[0] answer = cc.pipe( ((x for x in data_input), (x for x in cc.drop(1, data_input))), lambda x: zip(*x), cc.filter(lambda x: x[0] == x[1]), cc.map(lambda x: int(x[0])), sum) pp(answer)
def process(workbook: Any, content: str) -> Any: """Process Storage-Groups worksheet :param workbook: :param content: :return: """ worksheet_name = 'Storage-Groups' worksheet = workbook.get_sheet_by_name(worksheet_name) headers = list(concat([ get_parser_header(PORT_TMPL), get_parser_header(STORAGEGROUP_TMPL)[3:], ])) RowTuple = namedtuple('RowTuple', headers) # pylint: disable=invalid-name build_header(worksheet, headers) cmd_storagegroup_out = run_parser_over(content, STORAGEGROUP_TMPL) cmd_port_out = run_parser_over(content, PORT_TMPL) common_columns = (0, 1) server_names_grouped = compose( valmap( compose(list, set, map(last))), groupby( itemgetter(*common_columns)) )(cmd_port_out) cmd_port_relevant = map( juxt( compose(first, first), compose(second, first), second) )(server_names_grouped.items()) common_columns_getter = itemgetter(*common_columns) cmd_merged_out = join( common_columns_getter, cmd_port_relevant, common_columns_getter, cmd_storagegroup_out) cmd_merged_out = sorted(cmd_merged_out) rows = list(map( compose( list, concat, juxt( first, compose( drop(3), second))) )(cmd_merged_out)) portcmd = {(array, grp) for array, grp, *other in rows} strgp = {(array, grp) for array, grp, *other in cmd_storagegroup_out} no_server_groups = strgp - portcmd storage_list = list(filter( lambda storage_gr: any( fnmatch(str((storage_gr[0], storage_gr[1])), str(ctrlServer)) for ctrlServer in no_server_groups), cmd_storagegroup_out)) storage_list = check_empty_arrays( list(unique(storage_list + rows, key=itemgetter(0, 1)))) final_col, final_row = 0, 0 for row_n, row_tuple in enumerate(map(RowTuple._make, storage_list), 2): for col_n, col_value in \ enumerate(row_tuple._asdict().values(), ord('A')): cell = worksheet['{}{}'.format(chr(col_n), row_n)] if isinstance(col_value, str): cell.value = str.strip(col_value) else: cell.alignment = Alignment(wrapText=True) cell.value = '\n'.join(col_value) style_value_cell(cell) set_cell_to_number(cell) final_col = col_n final_row = row_n sheet_process_output( worksheet, 'StorageGroupsTable', 'Storage-Groups', final_col, final_row) return [[lun_map[0], lun_map[1], lun_map[4]] for lun_map in storage_list]
#!/usr/bin/env python import cytoolz.curried as cc import itertools as it from pprint import pprint as pp import sys data_input = sys.stdin.read().replace('\n', '') data_input_midpt = cc.pipe(data_input, it.cycle, cc.drop(int(len(data_input) / 2))) answer = cc.pipe(zip(data_input, data_input_midpt), cc.filter(lambda x: x[0] == x[1]), cc.map(lambda x: int(x[0])), sum) pp(answer)