Skip to content

Commit f49f78f

Browse files
author
Danyelle Eleusis
committed
d2
1 parent bfd5c90 commit f49f78f

File tree

1,353 files changed

+252074
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,353 files changed

+252074
-0
lines changed

day_02/ex00/data.csv

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
head,tail
2+
0,1
3+
1,0
4+
0,1
5+
1,0
6+
0,1
7+
0,1
8+
0,1
9+
1,0
10+
1,0
11+
0,1
12+
1,0
13+

day_02/ex00/first_class.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Must_read:
2+
with open('data.csv', 'r') as f: # open(path_to_file, mode)
3+
print(f.read())
4+
5+
if __name__ == '__main__':
6+
Must_read()

day_02/ex01/first_method.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Research:
2+
def file_reader():
3+
with open("../ex00/data.csv", 'r') as f:
4+
return(f.read())
5+
6+
if __name__ == '__main__':
7+
print(Research.file_reader())

day_02/ex02/data.csv

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
head,tail
2+
0,1
3+
1,0
4+
0,1
5+
1,0
6+
0,1
7+
0,1
8+
0,1
9+
1,0
10+
1,0
11+
0,1
12+
1,0

day_02/ex02/first_constructor.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import sys
2+
3+
class Research:
4+
def __init__(self, path_):
5+
self.path_ = path_
6+
7+
def file_reader(self):
8+
with open(self.path_, 'r') as f:
9+
return(f.read())
10+
11+
def check_args(path_):
12+
with open(path_, 'r') as f:
13+
line = f.readlines() # file.readlines() cчитывает из файла все строки в список и возвращает его.
14+
# first_line = line[0].split(',')
15+
# print(first_line)
16+
# if len(first_line) != 2:
17+
# raise Exception('Error in first line')
18+
# if first_line[0].isspace or first_line[1].isspace:
19+
# raise Exception('Header error')
20+
if len(line) < 2 or (len(line[0].split(',')) != 2 or (line[0].split(',')[1] == '\n')):
21+
raise Exception('Argument error')
22+
# if line[0].split(',')[1].isspace or line[0].split(',')[0].isspace:
23+
# raise Exception('Header error')
24+
for i in range(1, len(line) -1):
25+
if line[i] != '0,1\n' and line[i]!= '1,0\n':
26+
raise Exception('Argument error')
27+
28+
29+
if __name__ == '__main__':
30+
if len(sys.argv) != 2 or check_args(sys.argv[1]):
31+
raise Exception('Argument error')
32+
print(Research(sys.argv[1]).file_reader())
33+
# import sys
34+
35+
# class Research:
36+
# def __init__(self, path):
37+
# self.path = path
38+
39+
# def file_reader(self):
40+
# with open(self.path, 'r') as input_file:
41+
# lines = input_file.readlines()
42+
43+
# # first_line = lines[0].split(',')
44+
# if len(lines) != 2 or len(lines) < 2:
45+
# raise Exception('Error in first string')
46+
# # rest_line = lines[1:]
47+
# # if len(rest_line) == 0:
48+
# # raise Exception('There is no data')
49+
# for line in lines:
50+
# if line != '0,1' and line != '1,0':
51+
# raise ValueError('Error in strings')
52+
# return lines
53+
54+
55+
# if __name__ == '__main__':
56+
# if len(sys.argv) == 2:
57+
# print(Research(sys.argv[1]).file_reader())
58+
# print(research.file_reader())

day_02/ex03/data.csv

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
head,tail
2+
0,1
3+
1,0
4+
0,1
5+
1,0
6+
0,1
7+
0,1
8+
0,1
9+
1,0
10+
1,0
11+
0,1
12+
1,0

day_02/ex03/first_nest.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import sys
2+
3+
class Research:
4+
def __init__(self, file_name): # Конструктор, который вызывается при инициализации
5+
self.file_name = file_name
6+
7+
def file_reader(self, has_header = True):
8+
with open(self.file_name, 'r') as file:
9+
line = file.readlines()
10+
if line[0] == '0,1\n' or line[0] == '1,0\n':
11+
self.has_header = False
12+
# Если заголовок есть, то начнем записывать со второй строки (start = 1), иначе - с первой
13+
start = 0
14+
if has_header == True:
15+
start = 1
16+
list_lists = []
17+
for i in range(start, len(line)):
18+
list_i = [int(line[i][0])]
19+
list_i.append(int(line[i][2]))
20+
list_lists.append(list_i)
21+
return(list_lists)
22+
23+
class Calculations:
24+
def counts(list_lists):
25+
heads = 0
26+
tails = 0
27+
for i in range(len(list_lists)):
28+
if list_lists[i][0] == 1:
29+
heads += 1
30+
else:
31+
tails += 1
32+
return(heads, tails)
33+
34+
def fractions(list_counts):
35+
sum_counts = list_counts[0] + list_counts[1]
36+
return(list_counts[0] / sum_counts, list_counts[1] / sum_counts)
37+
38+
39+
def check_arg(file_name):
40+
with open(file_name, 'r') as file:
41+
line = file.readlines()
42+
# Если пустой файл или одна строка, но не с нужными значениями, то исключение
43+
if len(line) == 0 or (len(line) == 1 and (line[0] != '0,1\n' and line[0] != '1,0\n')):
44+
raise Exception("Error argument")
45+
# Проверяем остальные строки на нужные значения
46+
if len(line) > 1:
47+
for i in range(1, len(line) - 1):
48+
if line[i] != '0,1\n' and line[i] != '1,0\n':
49+
raise Exception("Error argument")
50+
51+
if __name__ == '__main__':
52+
if len (sys.argv) != 2 or check_arg(sys.argv[1]):
53+
raise Exception("Error argument")
54+
list_lists = Research(sys.argv[1]).file_reader()
55+
print(list_lists)
56+
list_counts = Research.Calculations.counts(list_lists)
57+
print(list_counts[0], list_counts[1])
58+
list_fractions = Research.Calculations.fractions(list_counts)
59+
print(list_fractions[0], list_fractions[1])

day_02/ex04/data.csv

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
head,tail
2+
0,1
3+
1,0
4+
0,1
5+
1,0
6+
0,1
7+
0,1
8+
0,1
9+
1,0
10+
1,0
11+
0,1
12+
1,0

day_02/ex04/first_child.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import sys
2+
from random import randint
3+
4+
class Research:
5+
def __init__(self, file_name):
6+
self.file_name = file_name
7+
8+
def file_reader(self, has_header = True):
9+
with open(self.file_name, 'r') as file:
10+
line = file.readlines()
11+
if line[0] == '0,1\n' or line[0] == '1,0\n':
12+
self.has_header = False
13+
# Если заголовок есть, то начнем записывать со второй строки (start = 1), иначе - с первой
14+
start = 0
15+
if has_header == True:
16+
start = 1
17+
list_lists = []
18+
for i in range(start, len(line)):
19+
list_i = [int(line[i][0])]
20+
list_i.append(int(line[i][2]))
21+
list_lists.append(list_i)
22+
return(list_lists)
23+
24+
class Calculations:
25+
def __init__(self, data):
26+
# Создаем объекты, чтобы их можно было вызывать снаружи
27+
self.data = data
28+
self.count = self.counts()
29+
self.fractions = self.fractions()
30+
31+
def counts(self):
32+
x = [x[0] for x in self.data]
33+
y = [y[1] for y in self.data]
34+
return [sum(x), sum(y)]
35+
36+
def fractions(self):
37+
return [(self.count[0] / (self.count[0] + self.count[1])) * 100,
38+
(self.count[1] / (self.count[0] + self.count[1])) * 100]
39+
40+
41+
class Analytics(Calculations):
42+
def __init__(self, n_steps):
43+
self.n_steps = n_steps
44+
self.predict = self.predict_random()
45+
self.predict_last = self.predict_last()
46+
47+
def predict_random(self):
48+
predict_dict = {0: [0, 1], 1: [1, 0]}
49+
return [predict_dict[randint(0, 1)] for x in range(self.n_steps)]
50+
51+
def predict_last(self):
52+
return self.predict[-1]
53+
54+
55+
def check_arg(file_name):
56+
with open(file_name, 'r') as file:
57+
line = file.readlines()
58+
# Если пустой файл или одна строка, но не с нужными значениями, то исключение
59+
if len(line) == 0 or (len(line) == 1 and (line[0] != '0,1\n' and line[0] != '1,0\n')):
60+
raise Exception("Error argument")
61+
# Проверяем остальные строки на нужные значения
62+
if len(line) > 1:
63+
for i in range(1, len(line) - 1):
64+
if line[i] != '0,1\n' and line[i] != '1,0\n':
65+
raise Exception("Error argument")
66+
67+
68+
if __name__ == '__main__':
69+
if len (sys.argv) != 2 or check_arg(sys.argv[1]):
70+
raise Exception("Error argument")
71+
output = Research(sys.argv[1]).file_reader()
72+
element = Research.Calculations(output)
73+
predict = Research.Analytics(3)
74+
fractions = Research.Calculations.fractions(element)
75+
print(element.data)
76+
print(element.count[0], element.count[1])
77+
print(round(fractions[0], 2), round(fractions[1], 2))
78+
print(predict.predict)
79+
print(predict.predict_last)
80+
81+
# https://younglinux.info/oopython/init
3.51 KB
Binary file not shown.

0 commit comments

Comments
 (0)