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
13 changes: 13 additions & 0 deletions kliver/B/task1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python

in_file = "allvectors.in"
out_file = "allvectors.out"

in_fd = open(in_file, "r")
length = int(in_fd.readline().strip())
in_fd.close()

out_fd = open(out_file, "w")
for i in range(0, 2**length):
out_fd.write(format(i, 'b').zfill(length) + "\n")
out_fd.close()
29 changes: 29 additions & 0 deletions kliver/B/task2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python


def fibbonachi(n):
if n < 3:
return 1
return fibbonachi(n-1) + fibbonachi(n-2)


def generate_binary_vectors_no_neighbor_ones(length, prefix=[]):
if len(prefix) == length:
yield prefix
else:
for vector in generate_binary_vectors_no_neighbor_ones(length, prefix + ["0"]):
yield vector
if not prefix or prefix[-1] != "1":
for vector in generate_binary_vectors_no_neighbor_ones(length, prefix + ["1"]):
yield vector

in_file = "vectors.in"
out_file = "vectors.out"

with open(in_file, "r") as in_fd:
length = int(in_fd.readline().strip())

with open(out_file, "w") as out_fd:
out_fd.write(str(fibbonachi(length + 2)) + "\n")
for vector in generate_binary_vectors_no_neighbor_ones(length):
out_fd.write("".join(vector) + "\n")
21 changes: 21 additions & 0 deletions kliver/B/task3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python


def permutations_generator(objects_list, prefix=[]):
if len(objects_list) == 1:
yield prefix + objects_list
else:
for i in range(0, len(objects_list)):
for permutation in permutations_generator(objects_list[:i] + objects_list[i+1:], prefix+[objects_list[i]]):
yield permutation

in_file = "permutations.in"
out_file = "permutations.out"

with open(in_file, "r") as in_fd:
length = int(in_fd.readline().strip())

out_fd = open(out_file, "w")
for permutation in permutations_generator([str(i) for i in range(1, length + 1)]):
out_fd.write(" ".join(permutation) + "\n")
out_fd.close()
21 changes: 21 additions & 0 deletions kliver/B/task4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python


def combination_generator(size, objects_list, prefix=[]):
if len(prefix) == size:
yield prefix
else:
for i in range(0, len(objects_list)):
for combination in combination_generator(size, objects_list[i+1:], prefix=prefix + [objects_list[i]]):
yield combination

in_file = "choose.in"
out_file = "choose.out"

with open(in_file, "r") as in_fd:
length, size = list(map(lambda x: int(x), in_fd.readline().strip().split()))

with open(out_file, "w") as out_fd:
for combination in combination_generator(size, [str(i) for i in range(1, length + 1)]):
#print(permutation)
out_fd.write(" ".join(combination) + "\n")
19 changes: 19 additions & 0 deletions kliver/B/task5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python


def subsets_generator(objects_list, prefix=[]):
yield prefix
for i in range(0, len(objects_list)):
for subset in subsets_generator(objects_list[i+1:], prefix=prefix + [objects_list[i]]):
yield subset

in_file = "subsets.in"
out_file = "subsets.out"

with open(in_file, "r") as in_fd:
length = int(in_fd.readline().strip())

with open(out_file, "w") as out_fd:
for subset in subsets_generator([str(i) for i in range(1, length + 1)]):
#print(permutation)
out_fd.write(" ".join(subset) + "\n")
27 changes: 27 additions & 0 deletions kliver/B/task6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python


def get_prev_and_next_vector(vector):
length = len(vector)
vector_value = int(vector, 2)
max_value = 2**length - 1
if not vector_value:
return "-", format(1, 'b').zfill(length)
prev_vector = format(vector_value - 1, 'b').zfill(length)
if vector_value == max_value:
return prev_vector, "-"
next_vector = format(vector_value + 1, 'b').zfill(length)
return prev_vector, next_vector

in_file = "nextvector.in"
out_file = "nextvector.out"

with open(in_file, "r") as in_fd:
input_vector = in_fd.readline().strip()

prev_vector, next_vector = get_prev_and_next_vector(input_vector)

out_fd = open(out_file, "w")
out_fd.write(prev_vector + "\n")
out_fd.write(next_vector + "\n")
out_fd.close()
47 changes: 47 additions & 0 deletions kliver/B/task7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python


def get_previous_permutation(current_permutation, length):
for i in range(1, length):
if current_permutation[-i] < current_permutation[-(i+1)]:
break
else:
return [0 for i in range(0, length)]
i += 1
prefix = current_permutation[:-i]
suffix = sorted(current_permutation[-i:], reverse=True)
for j in range(0, i):
if suffix[j] < current_permutation[-i]:
insert = suffix.pop(j)
break
return prefix + [insert] + suffix


def get_next_permutation(current_permutation, length):
for i in range(1, length):
if current_permutation[-i] > current_permutation[-(i+1)]:
break
else:
return [0 for i in range(0, length)]
i += 1
prefix = current_permutation[:-i]
suffix = sorted(current_permutation[-i:])
for j in range(0, i):
if suffix[j] > current_permutation[-i]:
insert = suffix.pop(j)
break
return prefix + [insert] + suffix

in_file = "nextperm.in"
out_file = "nextperm.out"

with open(in_file, "r") as in_fd:
length = int(in_fd.readline().strip())
current_permutation = list(map(lambda x: int(x), in_fd.readline().strip().split()))

previous_permutation = get_previous_permutation(current_permutation, length)
next_permutation = get_next_permutation(current_permutation, length)

with open(out_file, "w") as out_fd:
out_fd.write(" ".join(list(map(lambda x: str(x), previous_permutation))) + "\n")
out_fd.write(" ".join(list(map(lambda x: str(x), next_permutation))) + "\n")
25 changes: 25 additions & 0 deletions kliver/B/task8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python


def get_next_combination(current_combination, length, size):
for i in range(1, size + 1):
if current_combination[-i] != length - i + 1:
break
else:
return [-1]
prefix = current_combination[:-i]
for j in range(1, i + 1):
prefix.append(current_combination[-i] + j)
return prefix

in_file = "nextchoose.in"
out_file = "nextchoose.out"

with open(in_file, "r") as in_fd:
length, size = list(map(lambda x: int(x), in_fd.readline().strip().split()))
current_combination = list(map(lambda x: int(x), in_fd.readline().strip().split()))

next_combination = get_next_combination(current_combination, length, size)

with open(out_file, "w") as out_fd:
out_fd.write(" ".join(list(map(lambda x: str(x), next_combination))) + "\n")
Loading