Beispiel #1
0
def buddyStrings(A, B):
    """
	:type A: str
	:type B: str
	:rtype: bool
	"""
    ca = C(A)
    cb = C(B)
    if ca != cb:
        return False
    d = 0
    for i in range(len(A)):
        d += (A[i] != B[i])
    if d == 0:
        flag = 0
        for key in ca:
            if ca[key] > 1:
                flag = 1
                break
        if flag:
            return True
        return False

    if d == 2:
        return True
    return False
def CreateUnigramNLTK(dataset):
    ug = []
    counter = C()

    ### Get a count of all words, so we only use the words appearing more than once
    for sents in dataset:
        for words in sents:
            counter += C(words)  #create counter of words

    counter = {k: counter[k]
               for k in counter
               if counter[k] > 1}  #take only words appearing more than once

    ## Create unigram set
    for i in range(len(dataset)):
        bag = C()
        for d in dataset[i]:
            bag += C(d)

        features = {}
        for word in counter:
            features['contains(%s)' % word.lower()] = (word.lower() in bag)

        ug.append([features, targets[i]])

    return ug
def analyzeTheWorld(items, fName):
    featMap = {}
    cmds = C({})
    for v, cnt in items:
        x = "TheWorld " + v
        featMap.setdefault('TheWorld ' + fName, set()).add((v, x))
        cmds += C({x: cnt})
    return [cmds, featMap]
Beispiel #4
0
 def pair(l):  #takes values
     pair_values = list(i for i in C(l) if C(l)[i] == 2)
     if 2 in C(l).values():
         return True, pair_values, [
             item for item in l if item not in set(pair_values)
         ]
     else:
         return False
Beispiel #5
0
 def three(l):  #takes values
     pair_values = list(i for i in C(l) if C(l)[i] == 3)
     if 3 in C(l).values():
         return True, pair_values[0], [
             item for item in l if item not in set(pair_values)
         ]
     else:
         return False
Beispiel #6
0
def test_to_taxons():
    labels = [
        Label("if", [S(1, 1), S(1, 1), S(2, 5)]),
        Label("comparison_operator:Lt", [S(1, 1), S(3, 3), S(2, 2)]),
    ]
    assert t.to_taxons(labels) == [
        ("flow/conditional", C({S(1, 1): 2, S(2, 5): 1})),
        ("test/inequality", C({S(2, 2): 1, S(3, 3): 1, S(1, 1): 1})),
    ]
Beispiel #7
0
def possible_rings(rings):
    yield C()
    for k in rings:
        yield C({"Cost": k[0], "Damage": k[1], "Armor": k[2]})
    for k in rings:
        kk = C({"Cost": k[0], "Damage": k[1], "Armor": k[2]})
        for j in rings:
            if j != k:
                yield C({"Cost": j[0], "Damage": j[1], "Armor": j[2]}) + kk
Beispiel #8
0
def solution(participant, completion):
    answer = ''
    C_p = C(participant)
    C_c = C(completion)
    C_a = C_p - C_c

    for k, v in C_a.items():
        if v != 0:
            answer = k

    return answer
Beispiel #9
0
def reorderedPowerOf2(N):
    """
	:type N: int
	:rtype: bool
	"""
    num_str = str(N)
    for idx in range(30):
        a = str(2**idx)
        if C(a) == C(num_str):
            return True
    return False
Beispiel #10
0
 def numSpecialEquivGroups(self, A):
     s = set()
     for a in A:
         x = C(a[::2])
         y = C(a[1::2])
         x_new = immu(x)
         y_new = immu(y)
         print(x, "    ", x_new)
         print(y, "    ", y_new)
         s.add((x_new, y_new))
         print()
         print(s)
         print()
     return len(s)
Beispiel #11
0
def bazinga():
	a,b=map(int, si.readline().split())
	d = C(list(si.readline().strip()))
	for k,v in d.items():
		if v > b :	return 'NO'
	else:
		return 'YES'
Beispiel #12
0
    def _calc_density(self, x, y):
        # determine the x and y cells within which each individual's
        # x and y coordinates fall (the self.x_edge and self.y_edge
        # corrections determine the correct cells whether the grid
        # includes cells centered on the landscape edges,
        # i.e. self.x_edge or y_edge = True, or not)
        x_cells = (x - self.x_edge *
                   (self.window_width) / 2.) // self.window_width + self.x_edge
        y_cells = (y - self.y_edge *
                   (self.window_width) / 2.) // self.window_width + self.y_edge

        # get cell strings from indivudals' cells
        cells = _make_cell_strings(y_cells, x_cells, self.dim_om)
        # and get Counter dict of the cell strings
        counts = C(cells)

        # use the itemgetter attribute to get the count for each cell
        grid_counts = self.grid_counter(counts)
        # reshape them into an ndarray
        grid_counts = np.reshape(grid_counts, self.gi.shape)
        # and divide the array values by the appropriate
        # grid-cell areas to get the densities
        grid_dens = grid_counts / self.areas

        return grid_dens
Beispiel #13
0
def count_k_most_words(para,k):
	new = para.split()
	count = C(new)
	
	res = reversed(count.most_common(k))
	for x in res:
		print(x[0], ':' ,x[1])
Beispiel #14
0
def p1():
    ans = []
    for line in open("ac2020.2.txt"):
        limits, char, key = line.split()
        ll, ul = map(int, limits.split('-'))
        ch = char[0]
        ans.append(ll <= C(key)[ch] <= ul)
    return sum(ans)
 def uniqueOccurrences(self, arr: List[int]) -> bool:
     hashmap = C(arr)
     lis = set()
     c = 0
     for k, v in hashmap.items():
         c += 1
         lis.add(v)
     if len(lis) == c:
         return True
Beispiel #16
0
def solution(clothes):
    answer = 1
    cloth_li = []
    for i in clothes:
        cloth_li.append(i[1])
    cloth_num = C(cloth_li)
    for type in cloth_num.keys():
        answer *= cloth_num[type] + 1
    answer -= 1
    return answer
def palindromeRearrangin(a):
    b = list(C(a).values())

    hit = 0

    for i in b:
        if (i % 2) != 0:
            hit += 1
            if hit == 2: return False
    return True
Beispiel #18
0
 def __init__(self, docs):
     self.vocabs = {}
     self.vocab = C()
     self.sizes = {}
     self.number = {}
     for doc, target, num in docs:
         self.number[target] = num
         f = open(doc, 'r')
         l = f.read()
         w = l.split(',')
         self.vocabs[target] = C()
         for i in w:
             self.vocabs[target].update(
                 {i.split("\'")[1]: int(i.split()[-1])})
         self.sizes[target] = sum(self.vocabs[target].values())
         self.vocab.update(self.vocabs[target])
         f.close()
     self.size = len(self.vocab.keys())
     self.total = sum(self.number.values())
Beispiel #19
0
def dice_score(throw):
	d=dict(C(throw))
	p=0
	for x in d:
		if x==1:
			p+=divmod(d[x], 3)[0]*1000+divmod(d[x], 3)[1]*100
		elif x==5:
			p+=divmod(d[x], 3)[0]*500+divmod(d[x], 3)[1]*50
		else:
			p+=divmod(d[x], 3)[0]*x*100
	return p
Beispiel #20
0
def solve_part2(data, weapons, armor, rings):
    user = C({'Hp': 100, 'Damage': 0, 'Armor': 0, 'Cost': 0})
    cost = float("-inf")
    for w in possible_weapons(weapons):
        uw = user + w
        for a in possible_armor(armor):
            ua = uw + a
            for r in possible_rings(rings):
                ur = ua + r
                if not user_wins(ur, data.copy()):
                    cost = max(cost, ur["Cost"])
    return cost
    def mostCommonWord_2(self, paragraph: str, banned: List[str]) -> str:
        """
		1. use regex: substutute any character which is not word with space
		2. Counter.most_common
		"""
        words = [
            word for word in re.sub(r'[^\w]', ' ', paragraph).lower().split()
            if word not in banned
        ]

        counts = C(words)
        return counts.most_common(1)[0][0]
Beispiel #22
0
Datei: d11.py Projekt: smehan/AOC
def solve(input, f=0, s=None, u=lambda s, d, k: k in s and {k: -1} or {d: 1}):
    for d in input.strip().split(','):
        s = s or C()
        f = max(f, sum(s.values()))
        s += u(s, d, w[d][0])
        for b, a, c in (v[1:] for v in w.values() if {*v[2:]} <= {*+s}):
            m = min(s[a], s[c])
            s -= {
                a: m,
                b: -m,
                c: m
            }
    d = sum(s.values())
    return d, max(f, d)
def CreateBOW(counter, dataset, targets, presfreq='freq'):
    bow = []

    ## Create bag of words
    for i in range(len(dataset)):
        rep = [targets[i]]  #add polarity

        bag = C()
        for d in dataset[i]:
            bag += C(d)

        for w in counter:
            if w in bag:
                if presfreq == 'freq':  #if we want the frequency of words, retrieve it from counter
                    rep.append(bag[w])
                elif presfreq == 'pres':  #else just write 1
                    rep.append(1)
            else:
                rep.append(0)

        bow.append(rep)  #append polarity + text as 1 row

    return bow
Beispiel #24
0
def test_call():
    programs = [
        Program(
            name="algo1",
            labels=[
                Label("if", [S(1, 1), S(1, 1), S(2, 5)]),
                Label("if_else", [S(1, 1), S(2, 5)]),
                Label("comparison_operator:Lt", [S(1, 1), S(3, 3), S(2, 2)]),
            ],
            taxons=[],
            addition={},
            deletion={},
        ),
        Program(
            name="algo2",
            labels=[
                Label("member_call:difference_update", [S(1, 1), S(1, 1), S(2, 5)]),
                Label("literal:Set", [S(1, 1), S(2, 5)]),
            ],
            taxons=[],
            addition={},
            deletion={},
        ),
    ]
    result = {program.name: t.to_taxons(program.labels) for program in programs}
    print(result)
    assert result == {
        "algo1": [
            ("flow/conditional", C({S(1, 1): 1})),
            ("flow/conditional/else", C({S(1, 1): 1, S(2, 5): 1})),
            ("test/inequality", C({S(2, 2): 1, S(3, 3): 1, S(1, 1): 1})),
        ],
        "algo2": [
            ("call/function/builtin/casting/set", C({S(1, 1): 1, S(2, 5): 1})),
            ("type/non_sequence/set", C({S(1, 1): 3, S(2, 5): 2})),
        ],
    }
Beispiel #25
0
    def firstUniqChar(self, s):
        """
        :type s: str
        :rtype: int
        """

        from collections import Counter as C

        c = C(s)

        for i, v in enumerate(s):
            if c[v] == 1:
                return i

        return -1
Beispiel #26
0
 def update(self, spp):
     # get a copy of the previous counts
     copy = np.copy(self.counts)
     # get the species, then get a counter of number of individuals
     # within each cell
     counter = C([(int(x), int(y))
                  for x, y in zip(spp._get_x(), spp._get_y())])
     # convert that counter into an array of cell counts
     for i in range(self.dim[0]):
         for j in range(self.dim[1]):
             self.update_counts_cell(i, j, counter.get((j, i), 0))
     # update diff
     self.diff = self.counts - copy
     # update stats
     for fn, v in self.stats.items():
         self.stats[fn].append(fn(self.diff))
def shadow():
    for gi, name in enumerate(glob.glob('out/*')):
        day = re.search('out/(.*?_.*?_.*?)_(.*?$)', name).group(1)
        who = re.search(':\d\d_(.*?$)', name).group(1)
        f = open(name, 'r')
        c = json.loads(f.read())
        try:
            favs = c['favs']
            fr = c['fr']
            txt = c['txt']
        except:
            continue
        oneshot = {'___META_FR___': fr, "___META_ID___%s" % who: 1}
        oneshot.update(dict(C(m.parse(txt).strip().split())))
        db.put(bytes(name.split('/')[-1], 'utf-8'), pickle.dumps(oneshot))
        if gi % 1000 == 0:
            print(gi, day, name)
            print(favs, oneshot)
Beispiel #28
0
def test_deduplicated_taxons():
    assert t.deduplicated_taxons([]) == []
    taxons = [
        ("flow/conditional", C({S(1, 1): 2, S(2, 5): 1})),
        ("flow/conditional/else", C({S(1, 1): 1, S(2, 5): 1})),
        ("test/inequality", C({S(2, 2): 1, S(3, 3): 1, S(1, 1): 1})),
    ]
    result = t.deduplicated_taxons(taxons)
    print(result)
    assert result == [
        ("flow/conditional", C({S(1, 1): 1})),
        ("flow/conditional/else", C({S(1, 1): 1, S(2, 5): 1})),
        ("test/inequality", C({S(2, 2): 1, S(3, 3): 1, S(1, 1): 1})),
    ]
Beispiel #29
0
def calc_pi(mod, nonneut_loc, window_width=6):
    print("\nCalculating nucleotide diversity (WILL TAKE A WHILE)...\n")
    # make speciome
    speciome = mod.comm[0]._get_genotypes()
    # create data structure to store pi value for each genomic window
    pi_vals = []
    # for each locus
    for loc in range(speciome.shape[1]):
        # get 5-locus window around the locus
        start = max(loc-window_width, 0)
        # add 6 so that the resulting interval includes 5 loci on either
        # side of focal locus
        stop = min(loc+window_width+1, speciome.shape[1])
        print('\tcalculating for interval [%i, %i)' % (start, stop))
        # get all individs' genotypes for that interval of loci
        interval_idxs = list(range(start, stop))
        # NOTE: get rid of locus 50 itself, if in there, because it is the only
        # non-segregating locus, so it creates an artefactual depression in
        # nucleotide diversity in the area surrounding it
        #if nonneut_loc in interval_idxs:
        #    interval_idxs = [n for n in interval_idxs if n != nonneut_loc]
        intervalome = speciome[:, interval_idxs, :]
        # break into a list by seq (i.e. melt down to 2 seqs per individ)
        intervalome = np.vstack([intervalome[:, :, i] for i in (0, 1)])
        # get freq of each unique sequence
        seq_counts = C([''.join([str(n) for n in seq]) for seq in intervalome])
        seq_freqs = {seq: ct / intervalome.shape[0] for seq,
                     ct in seq_counts.items()}
        # get all pairwise combos of seqs
        combos = [*combinations([*seq_freqs], 2)]
        # get number of nucleotide differences per nucleotide site for each
        # pairwise combo of seqs
        pi_ij = [np.mean([i != j for i, j in zip(c[0], c[1])]) for c in combos]
        # get sum across all pairwise seq combos of mean number of nucleotide
        # differences multiplied by frequencies of each of the two seqs
        pi = 2 * sum([seq_freqs[c[0]] * seq_freqs[c[1]] * pi_ij[n] for n, c in
                     enumerate(combos)])
        pi_vals.append(pi)
    return pi_vals
Beispiel #30
0
def _make_von_mises_multimodal_sampler(neigh,
                                       dirs,
                                       vm_distr_kappa=12,
                                       approx_len=5000):
    # Returns a lambda function that is a quick and reliable way to simulate
    # draws from a Von Mises mixture distribution:
    # 1.) Chooses a direction by neighborhood-weighted probability
    # 2.) Makes a random draw from a Von Mises dist centered on the direction,
    # with a vm_distr_kappa value set such that the net effect,
    # when doing this a ton of times for a given neighborhood and then
    # plotting the resulting histogram, gives the visually/heuristically
    # satisfying approximation of a Von Mises mixture distribution

    # NOTE: Just visually, heuristically, vm_distr_kappa = 10 seemed
    # like a perfect middle value (vm_distr_kappa ~3 gives too
    # wide of a Von Mises variance and just chooses values around
    # the entire circle regardless of neighborhood
    # probability, whereas vm_distr_kappa ~20 produces noticeable reductions
    # in probability of moving to directions between the 8 queen's
    # neighborhood directions (i.e. doesn't simulate the mixing well enough)
    # and would generate artefactual movement behavior); 12 also
    # seemed to really well in generating probability valleys
    # when tested on neighborhoods that should generate bimodal distributions
    d = list(dirs.ravel())
    n = list(neigh.ravel())
    del d[4]
    del n[4]
    sum_n = float(sum(n))
    if sum_n > 0:
        n_probs = [i / sum_n for i in n]
    else:
        n_probs = [.125] * 8
    loc_choices = r.choice(d, approx_len, replace=True, p=n_probs)
    loc_choices = list(C(loc_choices).items())
    approx = np.hstack([
        s_vonmises.rvs(vm_distr_kappa, loc=loc, scale=1, size=size)
        for loc, size in loc_choices
    ])
    return approx