Example #1
0
def precompute():
    # list of all three digit nos divisible by 8 along with their permutations
    global L
    for i in xrange(1001):
        if i % 8 == 0:
            st = "{0:03}".format(i)
            for p in pe(st):
                L.add(''.join(p))
Example #2
0
def bruteForce():
    global maxm
    for j in xrange(1, 10 + 1):
        print "\n\t\t Main j: ", j
        for i in pe(range(1, j + 1)):
            makelis(i)
        print "\n\nMAXIMUM: ", maxm
        maxm = 0
def bruteForce():
    global maxm
    for j in xrange(1, 10 + 1):
        print '\n\t\t Main j: ', j
        for i in pe(range(1, j + 1)):
            makelis(i)
        print '\n\nMAXIMUM: ', maxm
        maxm = 0
Example #4
0
def twoSums(nums: list[int], target: int) -> list[int]:
    ind = []
    per_num = pe(nums, 2)
    for i in per_num:
        if sum(i) == target:
            for x, y in enumerate(nums):
                if y in i:
                    ind.append(x)
            return ind
Example #5
0
def solve(s, n):
    if n <= 3:
        for p in pe(s):
            if int(''.join(p)) % 8 == 0:
                return 'YES'
        return 'NO'
    else:
        # Look for a subsequence in s, which is of three digits and
        for candidate in L:
            if s.find(candidate) != -1:
                return 'YES'
        return 'NO'
Example #6
0
from itertools import permutations as pe
from scipy.sparse.csgraph import floyd_warshall as wf
n, m, r = map(int, input().split())
s = [[(0 if i == j else float("inf")) for j in range(n + 1)]
     for i in range(n + 1)]
g = list(pe(list(map(int, input().split()))))
for i in range(m):
    a, b, c = map(int, input().split())
    s[a][b] = c
    s[b][a] = c
s = wf(s, directed=False)
k = float("inf")
for i in range(len(g)):
    d = g[i]
    c = 0
    for j in range(1, len(d)):
        c += s[d[j - 1]][d[j]]
        if c >= k:
            break
    else:
        k = min(c, k)
print(int(k))
from itertools import permutations as pe
p = pe([1, 2, 3])
for i in list(p):
    print(i)
Example #8
0
#11
x = [3,5,2,4]
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
Example #9
0
        elif d[i] == 1008:
            if d[d[i + 1]] == d[i + 2]: d[d[i + 3]] = 1
            else: d[d[i + 3]] = 0
            i += 4
        elif d[i] == 1108:
            if d[i + 1] == d[i + 2]: d[d[i + 3]] = 1
            else: d[d[i + 3]] = 0
            i += 4

        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)]
Example #10
0
def permutations(string):

    p = pe(string)
    # print(set(p))
    return ["".join(s) for s in (set(p))]
Example #11
0
from itertools import permutations as pe

li = list(pe(list(range(1, int(input()) + 1))))
for i in li:
    print(" ".join(map(str, i)))