From fb7ae957fde79758193d58798b7c59a0b60ab5f8 Mon Sep 17 00:00:00 2001 From: Nadezda Date: Sat, 15 Mar 2014 11:00:54 +0400 Subject: [PATCH 1/4] Find minimum --- Pilshchikova/minimum.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Pilshchikova/minimum.py diff --git a/Pilshchikova/minimum.py b/Pilshchikova/minimum.py new file mode 100644 index 0000000..bf94c15 --- /dev/null +++ b/Pilshchikova/minimum.py @@ -0,0 +1,9 @@ +a = [3, 2, 7, 5] +if len(a) > 0: + m = a[0] + for i in a: + if i < m: + m = i + print m +else: + print 'No' \ No newline at end of file From f415fdb13e54a8d84954d4b5e470579ce8834ae5 Mon Sep 17 00:00:00 2001 From: Nadezda Date: Sat, 22 Mar 2014 11:11:33 +0400 Subject: [PATCH 2/4] Generate binary sequences --- yalie/gen_bin.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 yalie/gen_bin.py diff --git a/yalie/gen_bin.py b/yalie/gen_bin.py new file mode 100644 index 0000000..3ba39e3 --- /dev/null +++ b/yalie/gen_bin.py @@ -0,0 +1,12 @@ +k = 3 + +def gen_bin(a, p): + if p < k: + a[p] = 0 + gen_bin(a, p+1) + a[p] = 1 + gen_bin(a, p+1) + else: + print(a) + +gen_bin([0 for i in range(k)], 0) \ No newline at end of file From 4ee9c0f1ad135a6d80bcbd96b0469530886a0f95 Mon Sep 17 00:00:00 2001 From: Nadezda Date: Sat, 12 Apr 2014 11:48:09 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=A0=D0=B5=D1=88=D1=91=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D0=B5=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B8.=20=D0=97=D0=B0?= =?UTF-8?q?=D0=B4=D0=B0=D1=87=D0=B0=205=20=D0=B2=20=D0=B4=D0=B2=D1=83?= =?UTF-8?q?=D1=85=20=D0=B2=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82=D0=B0=D1=85?= =?UTF-8?q?:=20=D0=BF=D0=B5=D1=80=D0=B2=D1=8B=D0=B9=20=D1=82=D0=BE=D0=B6?= =?UTF-8?q?=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82,=20?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=BD=D0=B5=20=D0=BF=D1=80=D0=BE=D1=85=D0=BE?= =?UTF-8?q?=D0=B4=D0=B8=D1=82=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D1=83,=20=D1=82.=D0=BA.=20=D1=82=D0=B0=D0=BC=20=D0=B8=D0=B4?= =?UTF-8?q?=D1=91=D1=82=20=D1=81=D0=BE=D1=80=D1=82=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BF=D0=BE=D0=B4=D0=BC=D0=BD=D0=BE=D0=B6=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=B2=20=D0=B2=20=D0=BB=D0=B5=D0=BA=D1=81=D0=B8?= =?UTF-8?q?=D0=BA=D0=BE=D0=B3=D1=80=D0=B0=D1=84=D0=B8=D1=87=D0=B5=D1=81?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC=20=D0=BF=D0=BE=D1=80=D1=8F=D0=B4=D0=BA=D0=B5?= =?UTF-8?q?=20(10=20=D0=B8=D0=B4=D1=91=D1=82=20=D1=80=D0=B0=D0=BD=D1=8C?= =?UTF-8?q?=D1=88=D0=B5=202).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yalie/generators/generator1.py | 16 ++++++++++++++++ yalie/generators/generator2.py | 28 ++++++++++++++++++++++++++++ yalie/generators/generator3.py | 17 +++++++++++++++++ yalie/generators/generator4.py | 17 +++++++++++++++++ yalie/generators/generator5.py | 24 ++++++++++++++++++++++++ yalie/generators/generator5a.py | 16 ++++++++++++++++ 6 files changed, 118 insertions(+) create mode 100644 yalie/generators/generator1.py create mode 100644 yalie/generators/generator2.py create mode 100644 yalie/generators/generator3.py create mode 100644 yalie/generators/generator4.py create mode 100644 yalie/generators/generator5.py create mode 100644 yalie/generators/generator5a.py diff --git a/yalie/generators/generator1.py b/yalie/generators/generator1.py new file mode 100644 index 0000000..7476f5d --- /dev/null +++ b/yalie/generators/generator1.py @@ -0,0 +1,16 @@ +def gen_bin(p, a): + if p < n: + a[p] = 0 + gen_bin(p+1, a) + a[p] = 1 + gen_bin(p+1, a) + else: + out.write(str(''.join((str(i) for i in a)))+'\n') + +inf = open('allvectors.in') +n = int(inf.readline().strip()) +inf.close() + +out = open('allvectors.out', 'w') +gen_bin(0, [0 for i in range(n)]) +out.close() \ No newline at end of file diff --git a/yalie/generators/generator2.py b/yalie/generators/generator2.py new file mode 100644 index 0000000..82929dc --- /dev/null +++ b/yalie/generators/generator2.py @@ -0,0 +1,28 @@ +def gen_bin(p, a): + if p < n: + a[p] = 0 + gen_bin(p+1, a) + a[p] = 1 + gen_bin(p+1, a) + else: + v = ''.join(str(i) for i in a) + if '11' not in v: + global not11 + not11 += 1 + tmp[not11-1] = str(v) + return not11 + +inf = open('vectors.in') +n = int(inf.readline().strip()) +inf.close() + +not11 = 0 +tmp = [0 for i in range(2**n)] +ansver = gen_bin(0, [0 for i in range(n)]) + +out = open('vectors.out', 'w') +out.write(str(ansver) + '\n') +for i in tmp: + if i != 0: + out.write(str(i) + '\n') +out.close() \ No newline at end of file diff --git a/yalie/generators/generator3.py b/yalie/generators/generator3.py new file mode 100644 index 0000000..e31592f --- /dev/null +++ b/yalie/generators/generator3.py @@ -0,0 +1,17 @@ +def gen_perm(a, p): + if p < n: + for i in range(1, n+1): + if i not in a: + a[p] = i + gen_perm(a, p+1) + a[p] = 0 + else: + out.write(str(' '.join((str(i) for i in a)))+'\n') + +infile = open('permutations.in', 'r') +n = int(infile.readline().strip()) +infile.close() + +out = open('permutations.out', 'w') +gen_perm([0 for i in range(n)], 0) +out.close() \ No newline at end of file diff --git a/yalie/generators/generator4.py b/yalie/generators/generator4.py new file mode 100644 index 0000000..8d22c5a --- /dev/null +++ b/yalie/generators/generator4.py @@ -0,0 +1,17 @@ +def gen_comb(a, p): + if p < k: + for i in range(1, n+1): + if i not in a and i > a[p-1]: + a[p] = i + gen_comb(a, p+1) + a[p] = 0 + else: + out.write(str(' '.join((str(i) for i in a)))+'\n') + +infile = open('choose.in', 'r') +n, k = [int(i) for i in infile.readline().strip().split()] +infile.close() + +out = open('choose.out', 'w') +gen_comb([0 for i in range(k)], 0) +out.close() \ No newline at end of file diff --git a/yalie/generators/generator5.py b/yalie/generators/generator5.py new file mode 100644 index 0000000..977cb09 --- /dev/null +++ b/yalie/generators/generator5.py @@ -0,0 +1,24 @@ +#WA in test 10, but this code is working! + +def gen_subs(a): + ans = [] + for i in range(2**n): + tmp = [] + for j in range(n): + if i & (1 << j): + tmp.append(mn[j]) + ans.append(' '.join(str(i) for i in tmp)) + return ans + + +with open('subsets.in', 'r') as infile: + n = int(infile.readline().strip()) + +mn = range(1, n+1) +subs = gen_subs(mn) +subs.sort() + +with open('subsets.out', 'w') as out: + out.write('\n'.join(subs)) + +print '\n'.join(subs) \ No newline at end of file diff --git a/yalie/generators/generator5a.py b/yalie/generators/generator5a.py new file mode 100644 index 0000000..1b4d800 --- /dev/null +++ b/yalie/generators/generator5a.py @@ -0,0 +1,16 @@ +def gen_subs(line, l): + ans.append(' '.join(str(i) for i in line)) + for i in mn: + if i > l: + gen_subs(line + [i], i) + return ans + +with open('subsets.in', 'r') as infile: + n = int(infile.readline().strip()) + +mn = range(1, n+1) +ans = [] +gen_subs([], 0) + +with open('subsets.out', 'w') as out: + out.write('\n'.join(ans)) \ No newline at end of file From 76d05a540378f3b2bf15be68c829195c8770fdf6 Mon Sep 17 00:00:00 2001 From: Nadezda Date: Sat, 12 Apr 2014 11:54:48 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=A0=D0=B5=D1=88=D1=91=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D0=B5=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B8=20=D0=BF=D0=BE=20?= =?UTF-8?q?=D0=B3=D1=80=D0=B0=D1=84=D0=B0=D0=BC.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yalie/graphs/graph1a.py | 50 +++++++++++++++++++++++++++++++++++++++++ yalie/graphs/graph2.py | 44 ++++++++++++++++++++++++++++++++++++ yalie/graphs/graph3.py | 43 +++++++++++++++++++++++++++++++++++ yalie/graphs/graph5.py | 37 ++++++++++++++++++++++++++++++ yalie/graphs/graph6a.py | 50 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 224 insertions(+) create mode 100644 yalie/graphs/graph1a.py create mode 100644 yalie/graphs/graph2.py create mode 100644 yalie/graphs/graph3.py create mode 100644 yalie/graphs/graph5.py create mode 100644 yalie/graphs/graph6a.py diff --git a/yalie/graphs/graph1a.py b/yalie/graphs/graph1a.py new file mode 100644 index 0000000..7c2ee6e --- /dev/null +++ b/yalie/graphs/graph1a.py @@ -0,0 +1,50 @@ +from collections import deque + +#file reading +ingraph = open('components.in', 'r') +s = [int(i) for i in ingraph.readline().split()] +n = s[0] +m = s[1] +smezhn = {} #dict like adjacency list +for j in range(m): + x = [int(i) for i in ingraph.readline().split()] + if x[0] in smezhn: + smezhn[x[0]].append(x[1]) + else: + smezhn[x[0]] = [x[1]] + if x[1] in smezhn: #because here is not-directed graph + smezhn[x[1]].append(x[0]) + else: + smezhn[x[1]] = [x[0]] +for i in range(1, n+1): + if i not in smezhn: + smezhn[i] = [] +ingraph.close() + +#depth-first search +comp = {} #dict of components +def dfs(): + cc = 0 #number of components + for i in range(1, n+1): + if i not in comp: + cc += 1 + comp[i] = cc + queue = deque([i]) + while len(queue) > 0: + for j in smezhn[queue[0]]: + if j not in comp: + comp[j] = cc + queue.append(j) + queue.popleft() + out = open('components.out', 'w') + out.write(str(cc)+'\n') + out.close() + +dfs() + +#saving results +out = open('components.out', 'a') +a = comp.values() +for k in a: + out.write(str(k)+' ') +out.close() \ No newline at end of file diff --git a/yalie/graphs/graph2.py b/yalie/graphs/graph2.py new file mode 100644 index 0000000..42b85be --- /dev/null +++ b/yalie/graphs/graph2.py @@ -0,0 +1,44 @@ +from collections import deque + +smezhn = {} #dict like adjacency list +dist = {} #a dict of distances +dist[1] = 0 + +def read(): #file reading + ingraph = open('pathbge1.in', 'r') + s = [int(i) for i in ingraph.readline().split()] + n = s[0] + m = s[1] + for j in range(m): + x = [int(i) for i in ingraph.readline().split()] + if x[0] in smezhn: + smezhn[x[0]].append(x[1]) + else: + smezhn[x[0]] = [x[1]] + if x[1] in smezhn: #because here is not-directed graph + smezhn[x[1]].append(x[0]) + else: + smezhn[x[1]] = [x[0]] + for i in range(1, n+1): + if i not in smezhn: + smezhn[i] = [] + ingraph.close() + return smezhn + +def bfs(): + queue = deque([1]) + while len(queue) > 0: + for i in smezhn[queue[0]]: + if i not in dist: + dist[i] = dist[queue[0]] + 1 + queue.append(i) + queue.popleft() + return dist + +read() +bfs() +out = open('pathbge1.out', 'w') +a = dist.values() +for k in a: + out.write(str(k)+' ') +out.close() \ No newline at end of file diff --git a/yalie/graphs/graph3.py b/yalie/graphs/graph3.py new file mode 100644 index 0000000..cc9b188 --- /dev/null +++ b/yalie/graphs/graph3.py @@ -0,0 +1,43 @@ +#file reading +ingraph = open('pathmgep.in', 'r') +x = [int(i) for i in ingraph.readline().split()] +N = x[0] #number of nodes +S = x[1] #start +F = x[2] #finish +msm = [] #incidence matrix +for i in range(N): + y = [int(i) for i in ingraph.readline().split()] + msm.append(y) +ingraph.close() + +d = {} #distance dict +for i in range(1, N+1): + d[i] = -1 +n = [] #list of nodes + +def Dejkstra(d,S,F): # Dejkstra algorithm + d[S] = 0 + for y in range(N-1): + n.append(S) + for i in range(N): + if msm[S-1][i] != -1: + if d[i+1] != -1: + d[i+1] = min(d[i+1], (d[S]+msm[S-1][i])) + else: + d[i+1] = msm[S-1][i]+d[S] + a = {} #only help in finding a min node + for key in d: + if key not in n and d[key] != -1: + a[key] = d[key] + if len(a) == 0: + break + S = min(a, key=lambda k: a[k]) + return d + +Dejkstra(d,S,F) + +out = open('pathmgep.out', 'w') +out.write(str(d[F])) +out.close() + +print d[F] \ No newline at end of file diff --git a/yalie/graphs/graph5.py b/yalie/graphs/graph5.py new file mode 100644 index 0000000..1ebacc3 --- /dev/null +++ b/yalie/graphs/graph5.py @@ -0,0 +1,37 @@ +#file reading +ingraph = open('pathsg.in', 'r') +s = [int(i) for i in ingraph.readline().split()] +n = s[0] +m = s[1] +smezhn = {} #dict of dicts like adjacency list, with weights +for j in range(m): + x = [int(i) for i in ingraph.readline().split()] + if x[0] not in smezhn: + smezhn[x[0]] = {x[1]: x[2]} + else: + smezhn[x[0]][x[1]] = x[2] +ingraph.close() + +#distance matrix +d = [[1e100 for i in range(n)] for i in range(n)] +for i in range(n): + for j in range(n): + if i == j: + d[i][j] = 0 +for key in smezhn: + for kk in smezhn[key]: + d[key-1][kk-1] = smezhn[key][kk] + +def floyd(d): #Floyd algorithm + for k in range(n): + for i in range(n): + for j in range(n): + d[i][j] = min(d[i][j], (d[i][k]+d[k][j])) + +floyd(d) + +out = open('pathsg.out', 'w') +for l in d: + out.write(' '.join(str(i) for i in l)) + out.write('\n') +out.close() \ No newline at end of file diff --git a/yalie/graphs/graph6a.py b/yalie/graphs/graph6a.py new file mode 100644 index 0000000..5d1b8ce --- /dev/null +++ b/yalie/graphs/graph6a.py @@ -0,0 +1,50 @@ +#reading a file +ingraph = open('spantree.in','r') +n = int(ingraph.readline().strip()) +p = [] #a list of lists with all points +for i in range(n): + t = [int(i) for i in ingraph.readline().split()] + p.append(t) +ingraph.close() + +#calculation of length of the edge +def le(a,b): + import math + m = math.sqrt((p[a][0] - p[b][0])**2 + (p[a][1] - p[b][1])**2) + return m + +#saving a graph to the matrix +def sm(p): + smezhn = [['x' for i in range(n)] for j in range(n)] + for i in range(n): + for j in range(n): + smezhn[i][j] = le(i,j) + return smezhn + +#Prim algorithm +def Prim(smezhn): + used = [] + mine = [] + for i in range(n): + used.append(False) + mine.append('x') + used[0] = True + mine[0] = 0 + idx = 0 + for i in range(n-1): + tmp = {} + for j in range(n): + if not used[j]: + mine[j] = min(mine[j], smezhn[idx][j]) + tmp[j] = mine[j] + idx = min(tmp, key=tmp.get) #finding an index (node) of edge with min weight + used[idx] = True + return mine + +smezhn = sm(p) +wedge = Prim(smezhn) +ans = sum(wedge) + +out = open('spantree.out', 'w') +out.write(str(ans)) +out.close() \ No newline at end of file