Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions magnes/combinations/allvectors.py
Original file line number Diff line number Diff line change
@@ -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)
39 changes: 39 additions & 0 deletions magnes/combinations/choose.py
Original file line number Diff line number Diff line change
@@ -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')
33 changes: 33 additions & 0 deletions magnes/combinations/nextchoose.py
Original file line number Diff line number Diff line change
@@ -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')
42 changes: 42 additions & 0 deletions magnes/combinations/nextperm.py
Original file line number Diff line number Diff line change
@@ -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))
44 changes: 44 additions & 0 deletions magnes/combinations/nextvector.py
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 15 additions & 0 deletions magnes/combinations/permutations.py
Original file line number Diff line number Diff line change
@@ -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)
28 changes: 28 additions & 0 deletions magnes/combinations/subsets.py
Original file line number Diff line number Diff line change
@@ -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')
26 changes: 26 additions & 0 deletions magnes/combinations/vectors.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions magnes/final homework/airplane.py
Original file line number Diff line number Diff line change
@@ -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))
17 changes: 17 additions & 0 deletions magnes/final homework/harddrive.py
Original file line number Diff line number Diff line change
@@ -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))
36 changes: 36 additions & 0 deletions magnes/final homework/olympic.py
Original file line number Diff line number Diff line change
@@ -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]))
30 changes: 30 additions & 0 deletions magnes/final homework/tower.py
Original file line number Diff line number Diff line change
@@ -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)))
38 changes: 38 additions & 0 deletions magnes/graphs/components.py
Original file line number Diff line number Diff line change
@@ -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())
Loading