Exemplo n.º 1
0
def test_frequencies():
    assert (frequencies(["cat", "pig", "cat", "eel",
                        "pig", "dog", "dog", "dog"]) ==
            {"cat": 2, "eel": 1, "pig": 2, "dog": 3})
    assert frequencies([]) == {}
    assert frequencies("onomatopoeia") == {"a": 2, "e": 1, "i": 1, "m": 1,
                                           "o": 4, "n": 1, "p": 1, "t": 1}
Exemplo n.º 2
0
def _get_ext_freqs(
    files: Generator[github.ContentFile.ContentFile, None, None]
) -> Dict[str, int]:
    """Returns frequencies of file extension in `files`"""
    file_list = [file.name for file in files]
    return itertoolz.frequencies(
        filter(lambda x: x != '', map(get_ext, file_list)))
Exemplo n.º 3
0
def solve(data, selector=max):
    phrase = ""
    for i in range(data.shape[1]):  # iterate column-wise
        freq = frequencies(data[:, i])  # letter frequencies
        top = selector(freq.items(), key=itemgetter(1))[0]  # key with highest/lowest value
        phrase += top

    return phrase
Exemplo n.º 4
0
 def class_counts(self):
     '''
     Returns
     -------
     dict
         mapping {class_name:class_count} values
     '''
     # y_col = self.columns[1]
     return frequencies(self.data[self.y_col])
Exemplo n.º 5
0
def part1(rooms):
    result = 0

    for r in rooms:
        freq = frequencies(r["name"].replace("-", ""))  # letter frequencies
        freq = sorted(freq.items(), key=lambda e: (-e[1], e[0]))  # sort by counts first, and then by letter
        hash_ = ''.join(map(lambda e: e[0], freq[:5]))  # calculate expected hash

        if hash_ == r["hash"]:
            result += r["id"]

    return result
Exemplo n.º 6
0
def print_table(decoded: list,
                header: str = "Prediction summary",
                print: bool = True) -> Table:
    """Prints table summary of predicted labels"""
    table = Table(show_header=True, title=header)
    table.add_column("Class", )
    table.add_column("Count")
    table.add_column("Percentage")
    total = len(decoded)
    frequencies = itertoolz.frequencies(decoded)
    for is_last_element, var in core.signal_last(frequencies.items()):
        key, value = var
        count = value
        percentage = round((count / total) * 100, 2)
        if is_last_element:
            table.add_row(key, str(count), f"{percentage}", end_section=True)
            table.add_row("Total", str(total), "")
        else:
            table.add_row(key, str(count), f"{percentage}")
    if print:
        console.print(table)
    return table
Exemplo n.º 7
0
def ex_three(s):
    f = frequencies(s)
    return len(valfilter(lambda x: x == 3, f)) > 0
Exemplo n.º 8
0
def ex_two(s):
    f = frequencies(s)
    return len(valfilter(lambda x: x == 2, f)) > 0
Exemplo n.º 9
0
For example, if you see the following box IDs:

    abcdef contains no letters that appear exactly two or three times.
    bababc contains two a and three b, so it counts for both.
    abbcde contains two b, but no letter appears exactly three times.
    abcccd contains three c, but no letter appears exactly two times.
    aabcdd contains two a and two d, but it only counts once.
    abcdee contains two e.
    ababab contains three a and three b, but it only counts once.

Of these box IDs, four of them contain a letter which appears exactly twice,
and three of them contain a letter which appears exactly three times.
Multiplying these together produces a checksum of 4 * 3 = 12.

What is the checksum for your list of box IDs?
"""
from toolz.itertoolz import frequencies


two_letters = 0
three_letters = 0

for line in open('input.txt'):
    letter_counts = set(frequencies(line.strip()).values())
    if 2 in letter_counts:
        two_letters +=1
    if 3 in letter_counts:
        three_letters += 1

print(two_letters * three_letters)
Exemplo n.º 10
0
def count_stuffs(sequence, k):
    mers = kmers(sequence, k)
    return itertoolz.frequencies(mers)
Exemplo n.º 11
0
def count_codons(sequence, mol_type):
    """Returns the count of codons from a fasta file with CDS from a genome."""
    codons = my_codons(sequence, mol_type)
    return itertoolz.frequencies(codons)
Exemplo n.º 12
0
#!/usr/bin/env python3

from toolz.itertoolz import accumulate, frequencies

with open('input', 'r') as f:
    inp = f.readline().strip().split(',')


def total_movement(counts):
    straight = counts['nw'] - counts['se']
    left = counts['sw'] - counts['ne']
    right = counts['n'] - counts['s']
    straight += left
    right -= left

    return abs(straight + right)


print(total_movement(frequencies(inp)))

counts = {dir: 0 for dir in ['n', 'nw', 'ne', 's', 'sw', 'se']}


def single_count(counts, direction):
    counts[direction] += 1
    return counts.copy()


print(max(map(total_movement, accumulate(single_count, inp, counts))))
Exemplo n.º 13
0
def count_diffs(adaptor_chain: List[int]) -> Dict[int, int]:
    def calc_diff(ab: Tuple[int]) -> int:
        a, b = ab
        return b - a
    diffs = map(calc_diff, sliding_window(2, adaptor_chain))
    return frequencies(diffs)
Exemplo n.º 14
0
def count_subsatrings(sequence, k):
    """Returns the count of substrings of k length from a string"""
    mers = kmers(sequence, k)
    return itertoolz.frequencies(mers)