Beispiel #1
0
def lzw_decompress(archive: str) -> None:
    filenames = read_lzwfile_header(archive)
    codes_list = split_after(read_lzwfile_codes(archive, CODE_BITSIZE),
                             lambda x: x == VIRTUAL_EOF)
    decoder = LZWDecoder(CODE_BITSIZE)
    for filename, codes in zip(filenames, codes_list):
        decoder.decode_file(filename, codes)
Beispiel #2
0
def split_df_by_punct(df):
    l = list(
        split_after(
            df.to_records(index=False).tolist(), lambda x: x[0][-1] == "."))
    fixed = []
    for sublist in l:
        if sublist[-1][0] == ".":
            fixed.append(sublist[:-1])
        else:
            sublist[-1] = (list(sublist[-1])[0][:-1], *sublist[-1][1:])
            fixed.append(sublist)
    return fixed
Beispiel #3
0
def mask_maker(tokenized_text):
    #by description, this should be tokenized_text
    z = np.zeros(MAX_LEN)
    position = 0
    count = 1

    for sent in list(mit.split_after(tokenized_text, lambda x: x == '[SEP]')):
        sent_len = len(sent)
        z[position:position + sent_len + 1] = count
        position += sent_len
        count += 1

    return z
Beispiel #4
0
    def process_rule(self, idx, vals):
        list_vals = list(split_after(vals, lambda x: x == '|'))
        temp = ''
        rule = []

        for list_val in list_vals:

            temp = "".join(list_val)
            temp = temp.replace('|', '')
            temp = temp.split()

            rule.append(temp)

        self._rule_dictionary[idx] = rule
Beispiel #5
0
def mask_maker(tokenized_text):
    """
    Since the XLNet tokenizer works weird, I'm going to split after '[', and only take [:-1], 
    because the last is just part of CLS, and doesn't need a mask
    """

    #by description, this should be tokenized_text
    z = np.zeros(MAX_LEN)
    position = 0
    count = 1

    token_groups = list(mit.split_after(tokenized_text,
                                        lambda x: x == ']'))[:-1]

    for sent in token_groups:
        sent_len = len(sent)
        z[position:position + sent_len + 1] = count
        position += sent_len
        count += 1

    return z
Beispiel #6
0
def main(input, part):
    # Iterator of lines
    lines = map(lambda x: x.strip(), input.readlines())
    # Iterator of integers
    integers = list(map(int, lines))
    # integers = [28, 33, 18, 42, 31, 14, 46, 20, 48, 47, 24, 23, 49, 45, 19, 38, 39, 11, 1, 32, 25, 35, 8, 17, 7, 9, 4, 2, 34, 10, 3]
    # integers = [16, 10, 15, 5, 1, 11, 7, 19, 6, 12, 4]
    integers = sorted(integers)

    min_jolts = 0
    max_jolts = integers[-1] + 3
    diff_integers = chain([min_jolts], integers, [max_jolts])
    differences = list(map(lambda a, b: b - a,
                           *unzip(pairwise(diff_integers))))

    if part == "1":
        differences = Counter(differences)
        print(differences[1] * differences[3])
    if part == "2":
        # We do not have to consider all possible combinations, as all the
        # combinations will contain certain sequences, namely all sequences
        # will passthrough 3-difference numbers, and thus the sequences on
        # either side of these 3-difference numbers are independent.
        #
        # Thus we can split the problem into several subproblems, one on either
        # side of the 3-difference numbers.
        @apply
        def difference_is_3(index, value):
            """Check if the current index is a 3-difference number."""
            return differences[index] == 3

        # Iterator of subproblems (lists seperated by 3-difference numbers)
        # Each element in the lists are (index, value)
        subproblems = split_after(enumerate(integers), difference_is_3)
        # Map each element in the lists to just value (drop index)
        subproblems = map(lambda element: list(unzip(element)[1]), subproblems)
        # Find number of possible combinations for each block
        sub_counts = map(find_arrangements, subproblems)
        # Multiple the values for all the blocks to get a total
        print(prod(sub_counts))
Beispiel #7
0
    def extract_positive(
        self, journey: Journey
    ) -> Generator[Union[ExampleIndices, None], None, None]:

        segments = more_itertools.split_after(
            journey, lambda session: session.conversion)

        for segment in segments:
            if segment[-1].conversion:

                def in_window(session: Session) -> bool:
                    return session.date + self.lookback_window_days > segment[
                        -1].date

                def date_action(session: Session) -> Tuple[int, int]:
                    return session.date - segment[
                        -1].date + self.lookback_window_days - 1, session.action

                yield sorted(
                    set(
                        date_action(session) for session in segment
                        if in_window(session)))
            else:
                yield
Beispiel #8
0
def part_one(data_input):
    vals = list(split_after(data_input, lambda x: x == ''))
    rule_str = vals[0][0:-1]
    sequences = vals[1]

    print('hello')

    total_passed = 0

    rule = Rules()

    for rule_input in rule_str:
        rule_idx = re.findall("(\\d+)(?=:)", rule_input)[0]
        rule_val = re.findall("(?<=:\\s)[\\d\\sa-zA-Z|\\\"]+",
                              rule_input)[0].replace("\"", "")

        rule.process_rule(int(rule_idx), rule_val)

    for sequence in sequences:
        # print(total_passed)
        if rule.validate_sequence(sequence):
            total_passed += 1

    print("Part One: ", total_passed)
Beispiel #9
0
import random
from more_itertools import split_after

print("input your desired size of the list:")
size1 = input("> ")
int_size1 = int(size1)

i = 0
lst = []
while i < int_size1:
    ran = random.randint(0, 1)
    lst.append(ran)
    i = i + 1
lst2 = list(split_after(lst, lambda x: x < 1))
len_lst2 = len(lst2)

k = 0
a = 0
for j in lst2:
    sum_lst2 = sum(lst2[k])
    if sum_lst2 > 1:
        a = a + 1
    k = k + 1
    if k == len_lst2:
        print(lst, "=>", a)
Beispiel #10
0
def prepare_data( data_input ) :
    players    = list( split_after( data_input, lambda x: x == '' ) )
    player_one = [int( numeric_string ) for numeric_string in players[0][1:-1]]
    player_two = [int( numeric_string ) for numeric_string in players[1][1:]]

    return player_one, player_two
Beispiel #11
0
    def run_pipeline(self,
                     up_to: str = None,
                     force_rerun: List[str] = None) -> PipelineResult:
        """ Execute the tasks in the pipeline.

        :param str up_to: If supplied, execute the pipeline only up to this task.
        :param List[str] force_rerun: Optionally force the listed tasks to be executed.
        :return: The final pipeline state.
        :rtype: PipelineResult
        """

        previous_result: PipelineResult = self.load_pipeline(self.store_path)
        result = PipelineResult()
        self._tasks_reused.clear()
        self._tasks_executed.clear()

        for task_name in list(
                split_after(self.execution_order, lambda x: x == up_to))[0]:
            logger.debug(f'Starting executions of {task_name}')
            task_node = self.task_graph.nodes.get(task_name, None)
            if not task_node:
                raise PipelineConfigError(
                    f'Dependency on nonexistent task: {task_name}')
            task = task_node['task']
            args = PipelineResult()
            dependencies_succeeded = True
            for dependency in (task.task_def.depends_on or []):
                dependency = dependency.split('.')[0]
                args.task_results[dependency] = result.task_results[dependency]
                if result.task_results[
                        dependency].status == TaskStatus.FAILURE:
                    dependencies_succeeded = False
                    break

            if dependencies_succeeded:
                if task.task_def.pure and task_name not in (force_rerun or []) and \
                        self.reuse_inputs(task_name, previous_result, args):
                    logger.debug(f'Reusing previous results of {task_name}')
                    self._tasks_reused.add(task_name)
                    output = previous_result.task_results[task_name]
                    marker = Fore.YELLOW + u'\u2014' + Fore.WHITE
                else:
                    args_dict = self.build_args_dict(task, args)
                    try:
                        logger.debug(
                            f'Calling function to execute {task_name}')
                        output = self.invoke_task(task, **args_dict)
                        output.status = TaskStatus.SUCCESS
                        marker = Fore.GREEN + u'\u2714' + Fore.WHITE
                        self._tasks_executed.add(task_name)
                    except Exception as ex:
                        import traceback
                        print(Fore.RED)
                        traceback.print_exc()
                        print(Fore.WHITE)
                        logger.error(
                            f'Caught exception executing {task_name}: {ex}')
                        output = TaskResult(status=TaskStatus.FAILURE,
                                            error=str(ex))
                        marker = Fore.RED + u'\u2718' + Fore.WHITE

                print(Fore.WHITE + Style.BRIGHT + f'[{marker}] {task_name}')

                result.task_results[task_name] = output
                result.task_inputs[task_name] = args

                result = self.merge_pipeline_results(previous_result, result)
                self.cache_result(task_name, result)

        return result