From 6687050cdcb04cf24c4bc627004e8072bb059c86 Mon Sep 17 00:00:00 2001 From: magnes Date: Sun, 4 May 2014 13:06:17 +0400 Subject: [PATCH] 1 --- magnes/combinations/allvectors.py | 14 +++++++++ magnes/combinations/choose.py | 39 +++++++++++++++++++++++++ magnes/combinations/nextchoose.py | 33 ++++++++++++++++++++++ magnes/combinations/nextperm.py | 42 +++++++++++++++++++++++++++ magnes/combinations/nextvector.py | 44 +++++++++++++++++++++++++++++ magnes/combinations/permutations.py | 15 ++++++++++ magnes/combinations/subsets.py | 28 ++++++++++++++++++ magnes/combinations/vectors.py | 26 +++++++++++++++++ magnes/final homework/airplane.py | 12 ++++++++ magnes/final homework/harddrive.py | 17 +++++++++++ magnes/final homework/olympic.py | 36 +++++++++++++++++++++++ magnes/final homework/tower.py | 30 ++++++++++++++++++++ magnes/graphs/components.py | 38 +++++++++++++++++++++++++ magnes/graphs/pathbge1.py | 34 ++++++++++++++++++++++ 14 files changed, 408 insertions(+) create mode 100644 magnes/combinations/allvectors.py create mode 100644 magnes/combinations/choose.py create mode 100644 magnes/combinations/nextchoose.py create mode 100644 magnes/combinations/nextperm.py create mode 100644 magnes/combinations/nextvector.py create mode 100644 magnes/combinations/permutations.py create mode 100644 magnes/combinations/subsets.py create mode 100644 magnes/combinations/vectors.py create mode 100644 magnes/final homework/airplane.py create mode 100644 magnes/final homework/harddrive.py create mode 100644 magnes/final homework/olympic.py create mode 100644 magnes/final homework/tower.py create mode 100644 magnes/graphs/components.py create mode 100644 magnes/graphs/pathbge1.py diff --git a/magnes/combinations/allvectors.py b/magnes/combinations/allvectors.py new file mode 100644 index 0000000..e5faec2 --- /dev/null +++ b/magnes/combinations/allvectors.py @@ -0,0 +1,14 @@ +def gen_perm(a, p): + if p < k: + a[p] = 0 + gen_perm(a, p + 1) + a[p] = 1 + gen_perm(a, p + 1) + else: + outfile.write(str(''.join([str(i) for i in a])) + '\n') + +with open('allvectors.in', 'r') as infile: + k = int(infile.readline().strip()) + +with open('allvectors.out', 'w') as outfile: + gen_perm([2 for i in range(k)], 0) diff --git a/magnes/combinations/choose.py b/magnes/combinations/choose.py new file mode 100644 index 0000000..1431207 --- /dev/null +++ b/magnes/combinations/choose.py @@ -0,0 +1,39 @@ + +def right(x): + if x[-1] == 0: + x = x[::-1] + for i in range(len(x)): + if x[i] == 1: + x[i], x[i - 1] = x[i - 1], x[i] + return x[::-1] + else: + x = x[::-1] + for i in range(len(x) - 1): + if x[i] == 0 and x[i + 1] == 1: + x[i], x[i + 1] = x[i + 1], x[i] + x = x[:i][::-1] + x[i:] + return x[::-1] + + +def gen_all(array): + while array != end: + array = list(right(array)) + yield ' '.join([str(i + 1) for i in range(len(array)) if array[i] == 1]) + + +with open('choose.in', 'r') as infile: + + n, k = [int(i) for i in infile.readline().strip().split()] + array = [0 for x in range(n)] + for x in range(k): + array[x] = 1 + end = [0 for x in range(n)] + for x in range(k): + end[-x - 1] = 1 + + +with open('choose.out', 'w') as outfile: + outfile.write( + ' '.join([str(i + 1) for i in range(len(array)) if array[i] == 1]) + '\n') + for x in gen_all(array): + outfile.write(x + '\n') diff --git a/magnes/combinations/nextchoose.py b/magnes/combinations/nextchoose.py new file mode 100644 index 0000000..38b9c28 --- /dev/null +++ b/magnes/combinations/nextchoose.py @@ -0,0 +1,33 @@ + +def right(x): + if x[-1] == 0: + x = x[::-1] + for i in range(len(x)): + if x[i] == 1: + x[i], x[i - 1] = x[i - 1], x[i] + return x[::-1] + else: + x = x[::-1] + for i in range(len(x) - 1): + if x[i] == 0 and x[i + 1] == 1: + x[i], x[i + 1] = x[i + 1], x[i] + x = x[:i][::-1] + x[i:] + return x[::-1] + + +with open('nextchoose.in', 'r') as infile: + + n, k = [int(i) for i in infile.readline().strip().split()] + data = [int(i) for i in infile.readline().strip().split()] + array = [0 for i in range(n)] + for x in data: + array[x - 1] = 1 + + +with open('nextchoose.out', 'w') as outfile: + try: + array = right(array) + outfile.write( + ' '.join([str(i + 1) for i in range(n) if array[i] == 1])) + except TypeError: + outfile.write('-1') diff --git a/magnes/combinations/nextperm.py b/magnes/combinations/nextperm.py new file mode 100644 index 0000000..ebdaf69 --- /dev/null +++ b/magnes/combinations/nextperm.py @@ -0,0 +1,42 @@ + +def right(x): + p = [int(i) for i in x[::-1]] + + i = 1 + while i < n and p[i - 1] < p[i]: + i += 1 + + if n == i: + return ' '.join(['0' for x in range(n)]) + + j = p.index(min([x for x in p[:i] if x > p[i]])) + p[i], p[j] = p[j], p[i] + p = p[:i][::-1] + p[i:] + + return ' '.join(list([str(i) for i in p[::-1]])) + + +def left(x): + p = [int(i) for i in x[::-1]] + + i = 1 + while i < n and p[i - 1] > p[i]: + i += 1 + + if n == i: + return ' '.join(['0' for x in range(n)]) + + j = p.index(max([x for x in p[:i] if x < p[i]])) + p[i], p[j] = p[j], p[i] + p = p[:i][::-1] + p[i:] + + return ' '.join(list([str(i) for i in p[::-1]])) + + +with open('nextperm.in', 'r') as infile: + + n = int(infile.readline().strip()) + p = infile.readline().strip().split(' ') + +with open('nextperm.out', 'w') as outfile: + outfile.write(left(p) + '\n' + right(p)) diff --git a/magnes/combinations/nextvector.py b/magnes/combinations/nextvector.py new file mode 100644 index 0000000..e3b35fe --- /dev/null +++ b/magnes/combinations/nextvector.py @@ -0,0 +1,44 @@ + +def left(x): + x = list([int(i) for i in x])[::-1] + if x[0] == 1: + x[0] = 0 + else: + for i in range(len(x)): + if x[i] == 1: + x[i] = 0 + for z in range(len(x[:i])): + x[z] = 1 + break + return ''.join([str(i) for i in x[::-1]]) + + +def right(x): + x = list([int(i) for i in x])[::-1] + if x[0] == 0: + x[0] = 1 + else: + for i in range(len(x)): + if x[i] == 0: + x[i] = 1 + for z in range(len(x[:i])): + x[z] = 0 + break + return ''.join([str(i) for i in x[::-1]]) + + +with open('nextvector.in', 'r') as infile: + + x = infile.readline().strip() + + a = '-' + b = '-' + + if set(list(x)) != set(['0']): + a = left(x) + + if set(list(x)) != set(['1']): + b = right(x) + +with open('nextvector.out', 'w') as outfile: + outfile.write(a + '\n' + b) diff --git a/magnes/combinations/permutations.py b/magnes/combinations/permutations.py new file mode 100644 index 0000000..977754f --- /dev/null +++ b/magnes/combinations/permutations.py @@ -0,0 +1,15 @@ +def gen_perm(a, p): + if p < k: + for i in range(1, k + 1): + if i not in a: + a[p] = i + gen_perm(a, p + 1) + a[p] = 0 + else: + outfile.write(str(' '.join([str(i) for i in a])) + '\n') + +with open('permutations.in', 'r') as infile: + k = int(infile.readline().strip()) + +with open('permutations.out', 'w') as outfile: + gen_perm([0 for i in range(k)], 0) diff --git a/magnes/combinations/subsets.py b/magnes/combinations/subsets.py new file mode 100644 index 0000000..f7c2294 --- /dev/null +++ b/magnes/combinations/subsets.py @@ -0,0 +1,28 @@ +from operator import itemgetter + + +def bar(a, p, i): + if i < n: + a[i] = '' + bar(a, p[1:], i + 1) + + for x in p: + if x not in a: + a[i] = x + bar(a, p[1:], i + 1) + + else: + answer.append([x for x in a]) + +with open('subsets.in', 'r') as infile: + n = int(infile.readline().strip()) + +with open('subsets.out', 'w') as outfile: + a = [0 for x in range(n)] + p = [x for x in range(1, n + 1)] + + answer = [] + bar(a, p, 0) + answer = [filter(None, x) for x in answer] + for x in sorted(answer): + outfile.write(' '.join([str(z) for z in x]) + '\n') diff --git a/magnes/combinations/vectors.py b/magnes/combinations/vectors.py new file mode 100644 index 0000000..f81a0d8 --- /dev/null +++ b/magnes/combinations/vectors.py @@ -0,0 +1,26 @@ +def gen_perm(a, p): + if k == 0: + return 0 + if p < k: + a[p] = 0 + gen_perm(a, p + 1) + + if a[p - 1] != 1: + a[p] = 1 + gen_perm(a, p + 1) + + else: + output.append(str(''.join([str(i) for i in a])) + '\n') + +with open('vectors.in', 'r') as infile: + k = int(infile.readline().strip()) + +with open('vectors.out', 'w') as outfile: + output = [] + gen_perm([0 for i in range(k)], 0) + gen_perm([1 for i in range(k)], 1) + + output = sorted(list(set(output))) + outfile.write(str(len(output)) + '\n') + for x in output: + outfile.write(x) diff --git a/magnes/final homework/airplane.py b/magnes/final homework/airplane.py new file mode 100644 index 0000000..7a9d2fd --- /dev/null +++ b/magnes/final homework/airplane.py @@ -0,0 +1,12 @@ +with open('airplane.in', 'r') as infile: + s, n, a = [int(x) for x in infile.readline().strip().split()] + p = sum([int(x) for x in infile.readline().strip().split()]) + + if a >= 1000: + x = 'Impossible' + else: + x = (s + p) * a / (1000 - a) + + +with open('airplane.out', 'w') as outfile: + outfile.write(str(x)) diff --git a/magnes/final homework/harddrive.py b/magnes/final homework/harddrive.py new file mode 100644 index 0000000..550999a --- /dev/null +++ b/magnes/final homework/harddrive.py @@ -0,0 +1,17 @@ +from collections import defaultdict + +with open('harddrive.in', 'r') as infile: + n = int(infile.readline().strip()) + data = [int(x) for x in infile.readline().strip().split()] + + starts = defaultdict(int) + + for x in data: + if x - 1 not in starts: + starts[x] = 1 + if x - 1 in starts: + starts[x - 1] = 0 + starts[x] = 1 + +with open('harddrive.out', 'w') as outfile: + outfile.write(str(len([x for x in starts if starts[x]]) - 1)) diff --git a/magnes/final homework/olympic.py b/magnes/final homework/olympic.py new file mode 100644 index 0000000..ff61cc6 --- /dev/null +++ b/magnes/final homework/olympic.py @@ -0,0 +1,36 @@ +from collections import defaultdict + +with open('olympic.in', 'r') as infile: + + n = int(infile.readline().strip()) + + data = [] + for x in infile: + data.append(x.strip().split()) + + prizes = defaultdict(lambda: defaultdict(lambda: 0)) + + for a, b, c in data: + prizes[a][0] += 1 + prizes[b][1] += 1 + prizes[c][2] += 1 + + data = defaultdict(list) + for x in prizes.items(): + for z in range(3): + data[x[0]].append((x[1][z])) + + for i in range(3): + out = [] + best = max([x[1][i] for x in data.items()]) + for x in data.items(): + if x[1][i] < best: + out.append(x[0]) + for x in out: + del data[x] + + data = [x for x in data] + + +with open('olympic.out', 'w') as outfile: + outfile.write((sorted(data)[0])) diff --git a/magnes/final homework/tower.py b/magnes/final homework/tower.py new file mode 100644 index 0000000..fd2038c --- /dev/null +++ b/magnes/final homework/tower.py @@ -0,0 +1,30 @@ +from collections import defaultdict + +with open('tower.in', 'r') as infile: + + n = int(infile.readline().strip()) + data = [] + for x in infile: + data.append(set([int(z) for z in x.strip().split()[1:]])) + + g = defaultdict(set) + for i in range(len(data)): + for j in range(len(data)): + if data[i] & data[j] and i != j: + g[i].add(j) + g[j].add(i) + + seen = set() + que = [0] + + while que: + for x in que: + seen.add(x) + for y in g[que[0]]: + seen.add(y) + que.extend([z for z in g[y] if z not in que and z not in seen]) + del que[0] + + +with open('tower.out', 'w') as outfile: + outfile.write(str(len(seen))) diff --git a/magnes/graphs/components.py b/magnes/graphs/components.py new file mode 100644 index 0000000..61335ce --- /dev/null +++ b/magnes/graphs/components.py @@ -0,0 +1,38 @@ +from collections import defaultdict + + +def bfs(): + num_ks = 0 + ks = {} + for x in range(1, n + 1): + if x not in ks: + num_ks += 1 + ks[x] = num_ks + que = [x] + while len(que) != 0: + for x in g[que[0]]: + if x not in ks: + ks[x] = num_ks + que.append(x) + del que[0] + return str(num_ks) + '\n' + ' '.join([str(x) for x in ks.values()]) + +with open('components.in', 'r') as infile: + + g = defaultdict(list) + edges = [] + + n, m = [int(x) for x in infile.readline().strip().split()] + + for x in range(m): + e = infile.readline().strip().split() + if e[0] != e[1]: + edges.append(e) + + for x, y in edges: + g[int(x)].append(int(y)) + g[int(y)].append(int(x)) + + +with open('components.out', 'w') as outfile: + outfile.write(dfs()) diff --git a/magnes/graphs/pathbge1.py b/magnes/graphs/pathbge1.py new file mode 100644 index 0000000..1eb55c7 --- /dev/null +++ b/magnes/graphs/pathbge1.py @@ -0,0 +1,34 @@ +from collections import defaultdict + + +def bfs(): + ks = defaultdict(int) + ks[1] = 0 + que = [1] + while len(que) != 0: + for x in g[que[0]]: + if x not in ks: + ks[x] = ks[que[0]] + 1 + que.append(x) + del que[0] + return(' '.join([str(x) for x in ks.values()])) + +with open('pathbge1.in', 'r') as infile: + + g = defaultdict(list) + edges = [] + + n, m = [int(x) for x in infile.readline().strip().split()] + + for x in range(m): + e = infile.readline().strip().split() + if e[0] != e[1]: + edges.append(e) + + for x, y in edges: + g[int(x)].append(int(y)) + g[int(y)].append(int(x)) + + +with open('pathbge1.out', 'w') as outfile: + outfile.write(str(bfs()))