def maximumProduct(self, nums: list) -> int: # 392ms 33.44% # 找到最大的3个和最小的四个数后暴力枚举 from itertools import permutations as pr max_three, min_four = [], [] target = [] if len(nums) > 7: for num in nums: max_three.append(num) min_four.append(num) max_three.sort(reverse=True) min_four.sort() if len(max_three) > 3: max_three.pop() if len(min_four) > 4: min_four.pop() target.extend(max_three) target.extend(min_four) else: target = nums print(target) ans_max = target[0] * target[1] * target[2] for couple in pr(target, 3): # print(couple) temp = couple[0]*couple[1]*couple[2] ans_max = max(temp, ans_max) return ans_max
def length(digits): outputs = set() ops = [o.add, o.sub, o.mul, o.truediv] for a, b, c, d in p(digits): for op1, op2, op3 in pr(ops, repeat=3): outputs.update( [op1(op2(op3(a, b), c), d), op1(op2(a, b), op3(c, d))]) return next(i for i in co(1) if i not in outputs) - 1
def largestTimeFromDigits(self, A: List[int]) -> str: x = ':'.join( reduce( lambda a, b: max([a, b]), filter( lambda t: t[0] <= '23' and t[1] <= '59', map( lambda x: (str(x[0]) + str(x[1]), str(x[2]) + str(x[3])), list(pr(A)))), ('', ''))) return x if x != ':' else ''
def __init__(self, symbolic_system): self.A = {} self.B_u = {} self.B_lambda = {} self.c = {} self.Eta = [] self.E = {} self.e = {} self.symbolic_system = symbolic_system self.list_of_contact_points = symbolic_system.list_of_contact_points self.n, self.m_u, self.m_lambda = symbolic_system.n, symbolic_system.m_u, symbolic_system.m_lambda self.Sigma = list(pr(*[i.Sigma for i in self.list_of_contact_points]))
def process(): try: l = list(st.get()) n = nu.get() from itertools import permutations as pr tot = pr(l, n) with open('outw.txt', 'w+') as f, open("dict-a_to_z.txt", 'r') as dic: f.truncate() dici = dic.read().strip().split() for i in tot: s = "" for j in (i): s = s + j if str(s) in dici: f.write(str(s) + "\n") f.seek(0) te = f.read() f.close() E3.insert(tkinter.END, te) except: messagebox.showwarning("Type correct details!!!")
from itertools import combinations_with_replacement as H from itertools import product as pr BB = ['Ab', 'Bb', 'Cb', 'Db'] WW = ['Aw', 'Bw', 'Cw', 'Dw'] BBbc = list(H(BB, 6)) WWwc = list(H(WW, 6)) total = list(pr(BBbc, WWwc)) WB = [] for i in total: WB.append(i[0] + i[1]) WBA = [] # WBA=[x for x in WB if len(set([y[0] for y in x]))==4] for x in WB: if len(set([ y[0] for y in x ])) == 4 and x.count('Ab') >= 4 and x.count('Ab') > x.count('Aw'): WBA.append(x) WBAB = [] for i in WBA: if ((i.count('Db') > i.count('Dw')) + (i.count('Cb') > i.count('Cw')) + (i.count('Bb') > i.count('Bw')) == 1): WBAB.append(i) print(len(WBAB))
apply_const = lambda consts, const: [c for c in consts if c.isdisjoint(const)] find_const = lambda key, consts: [c for c in consts if key in c] def solve(sol, cs): if len(cs) == 0: return sol if len(sol)==81 else None cnt = Counter() for c in cs: # Find best split for k in c: cnt[k] += 1 for c in find_const(cnt.most_common()[-1][0], cs): s = solve(sol+[c], apply_const(cs, c)) if s: return s with open('p96.txt') as fin: sudoku = [line.strip() for line in fin] consts = [make_const(i,j,n) for i,j,n in pr(range(9), range(9), range(1,10))] total = 0 for start in range(1, len(sudoku), 10): initial = [] for j,i in pr(range(9), range(9)): n = int(sudoku[start+j][i]) if n: initial.append(make_const(i,j,n)) s = {} for c in solve(initial, reduce(apply_const, initial, consts.copy())): parts = sorted(c) s[(parts[0][1], parts[0][2])] = parts[1][2] total += s[(0,0)]*100 + s[(1,0)]*10 + s[(2,0)] print(total)
def findSolution(self, customfunction, z): return [ [i, j] for i, j in pr(range(1, z + 1), repeat=2) if customfunction.f(i, j) == z ]
''' 흰 공 개와 검은 공 개를 세 상자 A , B, C에 남김없이 나누어 넣을 때, 각 상자에 공이 개 이상씩 들어가도록 나누어 넣는 경우의 수를 구하시오. (단, 같은 색 공끼리는 서로 구별하지 않는다.) 개수가 적은 흰색부터 배열 4, 0, 0: 3 * (2군데에 2개 검은공)3H2 = 18 2, 2, 0: 3 * 3H4 = 45 1, 3, 0: 3! * 3H3 = 60 2, 1, 1: 3 * 3H4 = 45 => 168가지 ''' from itertools import combinations_with_replacement as H from itertools import product as pr W=['Aw','Bw','Cw'] B=['Ab','Bb','Cb'] WW=list(H(W,4)) BB=list(H(B,6)) WB=list(pr(WW,BB)) totalWB=[i[0]+i[1] for i in WB] WB2=[] for i in totalWB: a=[x[0] for x in i] if a.count('A')>=2 and a.count('B')>=2 and a.count('C')>=2: WB2.append(i) print(len(WB2))
ciph = [int(a) for a in open("p059_cipher.txt").read().split(',')] shortciph = ciph[:20] # print(ciph[:20]) from itertools import product as pr from string import ascii_lowercase keys = [a for a in pr(ascii_lowercase,repeat=3)] words = ['the', 'of', 'to', 'and', 'in'] # for a in keys: # longkey = a*7 # plain = ''.join([chr(ord(k)^c) for k,c in zip(longkey, shortciph)]) # if any([w in plain for w in words]): # print(a, plain) longkey = "god"*(1+len(ciph)/3) plain = ''.join([chr(ord(k)^c) for k,c in zip(longkey, ciph)]) print(plain) print(sum([ord(c) for c in plain]))
data2 = [] # . 개수 data3 = [] # 가능한 R,C,S for _ in 'a' * p: s = input() s2 = s.lstrip('.') dot = len(s) - len(s2) for i in s: if i == '(': a += 1 if i == ')': a -= 1 if i == '{': b += 1 if i == '}': b -= 1 if i == '[': c += 1 if i == ']': c -= 1 data1.append([a, b, c]) data2.append(dot) for j in pr(range(1, 21), repeat=3): x, y, z = j # 가능한 R,C,S for i in range(1, p): if x * data1[i][0] + y * data1[i][1] + z * data1[i][2] != data2[i]: break else: data3.append([x, y, z]) R = set() C = set() S = set() for i in data3: R.add(i[0]) C.add(i[1]) S.add(i[2]) if len(R) == 1: R = R.pop()
import itertools from itertools import product as pr pete_poss = pr('1234', repeat=9) colin_poss = pr('123456', repeat=6) pete_sums = [0] * 28 colin_sums = [0] * 31 for i in pete_poss: pete_sums[sum([int(j) for j in list(i)]) - 9] += 1 for i in colin_poss: colin_sums[sum([int(j) for j in list(i)]) - 6] += 1 P_pete = 0 runsum = sum(colin_sums[:3]) for n in range(9, 37): P_pete += (pete_sums[n - 9] * runsum) runsum += colin_sums[n - 6] ''' P_tie = 0 for n in range(9,37): P_tie += (pete_sums[n - 9] * colin_sums[n - 6]) P_colin = 0 runsum = colin_sums[30] for n in range(35, 8, -1): P_colin += (pete_sums[n - 9] * runsum) runsum += colin_sums[n - 6] ''' denom = 262144 * 46656
from itertools import permutations as pr perm = pr([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) arr = list(perm) length = len(arr) print arr[999999]
def permutations(s): return [''.join(i) for i in sorted(set(pr(s)))]
#!/usr/bin/env python # coding: utf-8 # In[28]: a,b=input().split(' ') i=int(input()) from itertools import product as pr comb=pr(list(range(int(a),int(b)+1)),repeat=i) count=0 for j in comb: if sum(j)%2==0: count=count+1 print(count%1000000007)
from itertools import combinations_with_replacement as H #중복조합 from itertools import product as pr BB = ['Ab', 'Bb', 'Cb', 'Db'] #'Ab'는 A가 검은모자 갖기 WW = ['Aw', 'Bw', 'Cw', 'Dw'] totalB = list(H(BB, 6)) #검은색모자나눠주기 totalW = list(H(WW, 6)) #흰색모자나눠주기 totalBW = list(pr(totalB, totalW)) BW = [i[0] + i[1] for i in totalBW] #튜플을 합쳐준다 조건제시법 BWGa = [] for i in BW: if set([x[0] for x in i ]) == {'A', 'B', 'C', 'D'} and i.count('Ab') >= 4 and i.count( 'Ab') > i.count('Aw'): # len(set([x[0] for x in i]))==4 BWGa.append(i) BWGaB = [] for i in BWGa: if i.count('Bb') > i.count('Bw') and i.count('Cb') <= i.count( 'Cw') and i.count('Db') <= i.count('Dw'): BWGaB.append(i) BWGaC = [] for i in BWGa: if i.count('Cb') > i.count('Cw') and i.count('Bb') <= i.count( 'Bw') and i.count('Db') <= i.count('Dw'): BWGaC.append(i) BWGaD = [] for i in BWGa: if i.count('Db') > i.count('Dw') and i.count('Cb') <= i.count( 'Cw') and i.count('Bb') <= i.count('Bw'): BWGaD.append(i) len(BWGaB + BWGaC + BWGaD)
s = list(input()) from itertools import product as pr p = list(pr(['+', '-'], repeat=3)) for ope in p: f = s[0] + ope[0] + s[1] + ope[1] + s[2] + ope[2] + s[3] if eval(f) == 7: print(f + '=7') exit(0)