def combos(num):
    if len(str(num)) not in memo:
        combos = []
        for i in range(1, len(str(num))):
            for j in co([k for k in range(1, len(str(num)))], i):
                combos.append(list(j))
        memo[len(str(num))] = combos
    return memo[len(str(num))]
Esempio n. 2
0
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
Esempio n. 3
0
def comb(arr, n):
    arr = np.asarray(arr)
    t = np.dtype([('', arr.dtype)] * n)
    result = np.fromiter(itertools.co(arr, n), t)
    return result.view(arr.dtype).reshape(-1, n)
Esempio n. 4
0
#!/bin/python
#https://www.hackerrank.com/contests/w30/challenges/poles/submissions/code/1300991420
import sys

n, k = map(int, raw_input().split())
h, w = [], []
for _ in xrange(n):
    a, b = map(int, raw_input().split())
    h = [a] + h
    w = [b] + w
limit = 1
if n - k <= limit:
    from itertools import combinations as co

    m = 10**9
    for i in co(range(n - 1), k - 1):
        i += (n - 1, )
        #print i
        temp = 0
        start = 0
        for j in i:
            temp += sum(w[l] * (h[l] - h[j]) for l in xrange(start, j))
            #print j,temp
            start = j + 1
            if temp >= m:
                break
        #print i,temp
        m = min(m, temp)
    print m
else:
    m = [[0 for _ in xrange(n)] for _ in xrange(n)]
Esempio n. 5
0
from itertools import combinations as co
t = [
]  #.........................................Lines 2-6 store the input data into array t
for i in open("input2.txt").readlines():  #.......
    t.append(i.strip("\n"))  #....................
for i in range(len(t)):  #........................
    t[i] = [int(j) for j in t[i].split("\t")]  #..
r1, r2 = sum(max(i) - min(i) for i in t), 0
for k in t:
    for j1 in co(k, 2):
        i, j = sorted(j1)[0], sorted(j1)[1]
        if j % i == 0:
            r2 += j // i
            break  # not necessary, but decreases speed a little
print(str(r1) + "\n" + str(r2))
Esempio n. 6
0
print(random.choice(x))
#5
random.shuffle(x)
print(x)
#[5,2,3,4]
print(random.random())
#0.67586467987676

from itertools import product as p , permutations as pe , combinations as co
print(list(p([1,2,3],repeat=2))) #repeat is length of each tuple
#[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
print(list(p([1,2],[2,3,4])))
#[(1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4)]
print(list(pe([1,2,3],2)))
#[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
print(list(co([1,2,3],3)))
#[(1, 2, 3)]




#SETS: are mutable but frozen sets are immutable
x = set("A Python Tutorial")
# {' ', 'A', 'P', 'T', 'a', 'h', 'i', 'l', 'n', 'o', 'r', 't', 'u', 'y'}

x = set(["Perl", "Python", "Java"])             #whole list can be converted to set
#{'Java', 'Perl', 'Python'}

cities = set((["Python","Perl"], ["Paris", "Berlin", "London"])) # indivisual mutable elements cannot be converted to set
# error
Esempio n. 7
0
        elif d[i] == 99:
            break
    return "exitcode"


m = 0
for i in pe((0, 1, 2, 3, 4), 5):
    a = 0
    for j in i:
        a = ic(j, a, [dd for dd in d2])
#        print(i,j,a)
    m = max(m, a)
print(m)
####r,i
m = 0
for i in co((9, 8, 7, 6, 5), 5):
    a = 0
    ii = 0
    ri = [0, 0, 0, 0, 0]
    rout = [[k] for k in i]
    rout = rout[1:] + [rout[0]]
    rds = [[dd for dd in d2] for iii in range(5)]
    rout[4] += [0]

    while True:
        print(ii, rout)
        #        print(i[ii],rout[(ii-1)%5][0],rds[ii])
        while len(rout[(ii - 1) % 5]) == 0:
            ii += 1
            ii %= 5
            if ii == 5: break
def inex(lim, mults):
    ans = 0
    for i in range(len(mults)):
        for j in co(mults, i + 1):
            ans += (-1)**i * SoM(lim, prod(list(j)))
    return ans
Input Format:
The first line contains N.
The second line contains the capacity of these N rooms separated by a space.
The third line contains G.
Output Format:
The first line contains W.
Boundary Conditions:
1 <= N <= 50
1 <= G <= 100
Example Input/Output 1:
Input:
5
1 2 3 4 6
8
Output:
2
Explanation:
The possible ways are
(2+6)- choosing rooms with capacity 2 and 6
(1+3+4) - choosing rooms with capacity 1,3 and 4
from itertools import combinations as co, permutations as p
n=int(input())
l=list(map(int,input().split()))
x=int(input())
c=0
for i in range(1,n+1):
    for j in co(l,i):
        if sum(j)==x:
            c+=1 
print(c)
Esempio n. 10
0
 def powerset(items):  # power set without empty set
     from itertools import chain as ch, combinations as co
     return ch.from_iterable(
         co(items, r) for r in range(1, len(items) + 1))