-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask41.py
More file actions
46 lines (39 loc) · 1.46 KB
/
Task41.py
File metadata and controls
46 lines (39 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# A. Задана натуральная степень k.
# Сформировать случайным образом список коэффициентов (значения от 0 до 100)
# многочлена и записать в файл многочлен степени k.
# Пример:
# если k = 2, то многочлены могут быть => 2*x² + 4*x + 5 = 0
# или x² + 5 = 0 или 10*x² = 0
from random import randint
print('Введите натуральную степень k:')
k = int(input())
def write_file(st):
with open('Task04.txt', 'w') as data:
data.write(st)
def create_list(k):
list = []
for i in range(k + 1):
list.append(randint(0, 101))
return list
def create_str(sp):
list = sp[::-1]
wr = ''
if len(list) < 1:
wr = 'x = 0'
else:
for i in range(len(list)):
if i != len(list) - 1 and list[i] != 0 and i != len(list) - 2:
wr += f'{list[i]}x^{len(list) - i - 1}'
if list [i + 1] != 0:
wr += ' + '
elif i == len(list) - 2 and list[i] != 0:
wr += f'{list[i]}x'
if list[i + 1] != 0:
wr += ' + '
elif i == len(list) - 1 and list[i] != 0:
wr += f'{list[i]} = 0'
elif i == len(list) - 1 and list[i] == 0:
wr += ' = 0'
return wr
koef = create_list(k)
write_file(create_str(koef))