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
55 changes: 47 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,53 @@ python main.py

***

this program shows you the results of insertion and merge sorts for each algorithm, it also shows some additional information, such as:
* time,
* swap counter,
* comparison counter.
```
Вождь Ваі-Ву з острова Пасхи вирішив поміняти традиції одруження на острові.

counters represent the numbers of corresponding operations that occurred during the sorting process.
Раніше усі одружувались всередині свого племені. Але після курсу біології на Coursera
Ваі-Ву дізнався, що змішування генів - це корисна штука. Тому він попросив усіх голів племен підготувати списки молодих хлопців/дівчат, і спробує організувати шлюби між племенами.

Вашим завдання буде проаналізувати списки, та сказати Ваі-Ву скільки можливих пар з різних племен він може скласти.

Формат списків племен:
Кожен юнак/дівчина - це ціле число
Юнаки - непарні числа, дівчата - парні
У кожному рядку - два числа, які означають що юнак чи дівчина належать до одного племені. Наприклад:

1 2
2 4
3 5

Описує два племені - в одному є хлопець 1 та двоє дівчат 2 та 4, а в іншому племені - двоє хлопців 3 та 5.

Вхідні дані:
Перший рядок містить кількість пар N
Наступні N рядків містять пари людей з різних племен

Вихідні дані:
Кількість можливих комбінацій пар таких, що хлопець і дівчина походять з різних племен.

Обмеження:
0 < N < 10000
Приклади:
In:
3
1 2
2 4
3 5
Out:
4 (Можливі пари - 2/3, 2/5, 4/3, 4/5)

In:
5
1 2
2 4
1 3
3 5
8 10
Out:
6 (Можливі пари - 1/8, 1/10, 3/8, 3/10, 5/8, 5/10)

the list of objects is taken from *basin.csv* file. Each row there represents a new object as a sequence of its properties' values separated by commas. The order is:
```
address, volume_of_water, max_number_of_visitors
```
складність алгоритму буде О(кількості людей)*(клькість племен)
(фінальний код в файлі exmpl.py)
68 changes: 68 additions & 0 deletions exmpl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
def wedding(N, people):
print(N, people)
if people:
tribes = [set()] # list set
for first, second in people:
o = 0
for tribe in tribes:
o = 0
if not tribe:
tribe.add(first)
tribe.add(second)
elif first in tribe:
tribe.add(second)
elif second in tribe:
tribe.add(first)
else:
o = 1
if not o:
break
if o:
tribes.append(set((first, second)))
result = find_pairs(tribes)
else:
result = 0
return result


def find_pairs(tribes):
boys = []
girls = []
for tribe in tribes:
boys_in_tribe = []
girls_in_tribe = []
for every_person in tribe:
if every_person % 2:
boys_in_tribe.append(every_person)
else:
girls_in_tribe.append(every_person)
boys.append(len(boys_in_tribe))
girls.append(len(girls_in_tribe))

return sum(boys) * sum(girls) - sum((b * g for b, g in new_zip(boys, girls)))


def new_zip(a, b):
my_list = []
for x in range(min(len(a), len(b))):
for z in range(min(len(a), len(b))):
w = set((a[x], b[x]))
my_list.append(w)
break
return my_list

print('Example 1:')
print(wedding(3, ((1, 2), (2, 4), (3, 5))), '\n')
print('Example 2:')
print(wedding(5, ((1, 2), (2, 4), (1, 3), (3, 5), (8, 10))), '\n')
print('My example:')
print(wedding(4, ((1, 2), (2, 4), (3, 5), (8, 10))), '\n')
print('My example 2 :')
print(wedding(0,()))

in_N = int(input())
in_people = [tuple(map(int, input().split(' '))) for u in range(in_N)]
print(wedding(in_N, in_people))

if __name__ == '__main__':
print(wedding())
20 changes: 20 additions & 0 deletions in_out.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import sys


def read_pairs(filename, couple_list):
try:
with open(filename, "r") as file:
for line in file:
first_person, second_person = line.split(" ")
first_person = int(first_person)
second_person = int(second_person)
couple_list.append((first_person, second_person))
return couple_list

except (FileNotFoundError):
sys.exit(f'file "{filename}" not found')


def write_result(filename, result):
with open(filename, "w") as file:
file.write(str(result))
15 changes: 15 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from in_out import write_result, read_pairs
from utils import wedding_func

if __name__ == '__main__':
# graph = [(1, 2), (2, 4), (3, 5)]
graph = []
in_file = "wedd.in"
out_file = "wedding_result.out"
graph = read_pairs(in_file, graph)
graph_length = len(graph)

print("Pairs: " + str(len(graph)) + "Result:")
how_many_pairs = wedding_func(graph, graph_length)
write_result(out_file, how_many_pairs)
print(how_many_pairs)
70 changes: 70 additions & 0 deletions unit_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import unittest
from utils import wedding_func


class TestGraph(unittest.TestCase):

def setUp(self) -> None:
"""

graphs initialising

"""
self.first_list = [(1, 2), (2, 4), (3, 5)]
self.second_list = [(1, 2), (2, 4), (1, 3), (7, 5), (8, 10)]
self.third_list = [(1, 2)]
self.forth_list = []
self.fifth_list = [(1, 2), (2, 4), (4, 6), (6, 7)]
self.sixth_list = [(1, 2), (3, 4), (5, 6)]
self.seventh_list = [[(1, 2), (2, 4), (1, 3), (3, 5), (8, 10)]]

self.seventh_list_lenght = 1200

"""

Initialising results

"""
self.first_result = 4
self.second_result = 12
self.third_result = 0
self.forth_result = 0
self.fifth_result = 0
self.sixth_result = 6
self.seventh_result = 0
"""

Directly, Tests

"""
def test_first_list_from_example(self):
self.assertEqual(wedding_func(self.first_list, len(self.first_list)),
self.first_result)

def test_second_list_from_example(self):
self.assertEqual(wedding_func(self.second_list, len(self.second_list)),
self.second_result)

def test_third_list_from_example(self):
self.assertEqual(wedding_func(self.third_list, len(self.third_list)),
self.third_result)

def test_forth_list_from_example(self):
self.assertEqual(wedding_func(self.forth_list, len(self.forth_list)),
self.forth_result)

def test_fifth_list_from_example(self):
self.assertEqual(wedding_func(self.fifth_list, len(self.fifth_list)),
self.fifth_result)

def test_sixth_list_from_example(self):
self.assertEqual(wedding_func(self.sixth_list, len(self.sixth_list)),
self.sixth_result)

def test_seventh_list_from_example(self):
self.assertEqual(wedding_func(self.seventh_list, self.seventh_list_lenght),
self.seventh_result)


if __name__ == '__main__':
unittest.main()
55 changes: 55 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
def wedding_func(people_list, len_of_graph):

tribes = reboot(people_list, len_of_graph)

boy_in_each_tribe = [boy_amount_in_tribe(tribe) for tribe in tribes]

girl_in_each_tribe = [girl_amount_in_tribe(tribe) for tribe in tribes]

all_possible_pairs = sum(boy_in_each_tribe) * sum(girl_in_each_tribe)

pairs_in_one_tribe = sum(copy_couple(boy_in_each_tribe, girl_in_each_tribe))

result = all_possible_pairs - pairs_in_one_tribe

return result


def reboot(people_list, len_of_graph):
tribes = []
sorted_couple_list = sorted(people_list)
for people in sorted_couple_list:
if len_of_graph > 1000:
print("Error! To much couples!")
break
for tribe in tribes:

if people[0] in tribe:
tribe.add(people[1])

break

elif people[1] in tribe:
tribe.add(people[0])

break

else:

tribes.append(set((people[0], people[1])))

return tribes


def boy_amount_in_tribe(tribe):
return len({boy_count for boy_count in tribe if boy_count % 2})


def girl_amount_in_tribe(tribes):
return len({girl_count for girl_count in tribes if not girl_count % 2})


def copy_couple(boy_each_tribe, girl_in_each_tribe):
return (boy * girl for boy, girl in zip(boy_each_tribe, girl_in_each_tribe))


3 changes: 3 additions & 0 deletions wedd.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
3 2
8 6
1 4
5 changes: 5 additions & 0 deletions wedd2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1 2
2 8
3 5
5 6
2 4
5 changes: 5 additions & 0 deletions wedd3.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
5 6
1 2
2 4
3 5
3 4
Loading