last lab
This commit is contained in:
66
labPython/lab7/BinSearch_2.py
Executable file
66
labPython/lab7/BinSearch_2.py
Executable file
@@ -0,0 +1,66 @@
|
||||
def find_min_radius(feasible, eps=1e-6, max_radius=None):
|
||||
if max_radius is None:
|
||||
high_bound = 1.0
|
||||
while not feasible(high_bound):
|
||||
high_bound *= 2
|
||||
if high_bound > 1e10:
|
||||
raise ValueError(
|
||||
"Не удалось найти верхнюю границу. Возможно, feasible всегда возвращает False."
|
||||
)
|
||||
low = 0.0
|
||||
high = high_bound
|
||||
else:
|
||||
low = 0.0
|
||||
high = max_radius
|
||||
|
||||
while high - low > eps:
|
||||
mid = (low + high) / 2.0
|
||||
|
||||
if feasible(mid):
|
||||
high = mid
|
||||
else:
|
||||
low = mid
|
||||
|
||||
return high
|
||||
|
||||
|
||||
def can_cover(points, m, r):
|
||||
if not points:
|
||||
return True
|
||||
|
||||
used_circles = 0
|
||||
i = 0
|
||||
|
||||
while i < len(points):
|
||||
circle_center = points[i] + r
|
||||
circle_right = circle_center + r
|
||||
|
||||
used_circles += 1
|
||||
if used_circles > m:
|
||||
return False
|
||||
|
||||
while i < len(points) and points[i] <= circle_right:
|
||||
i += 1
|
||||
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Введите N и M через пробел:")
|
||||
n, m = map(int, input("> ").split())
|
||||
|
||||
print(f"Введите {n} координат точек (по одной на строке):")
|
||||
points = []
|
||||
for _ in range(n):
|
||||
point = float(input().strip())
|
||||
points.append(point)
|
||||
|
||||
points.sort()
|
||||
|
||||
def feasible(r):
|
||||
return can_cover(points, m, r)
|
||||
|
||||
eps = 1e-6
|
||||
result = find_min_radius(feasible, eps=eps)
|
||||
|
||||
print(f"Минимальный радиус: {result:.6f}")
|
||||
BIN
labPython/lab7/__pycache__/sort2.cpython-313.pyc
Executable file
BIN
labPython/lab7/__pycache__/sort2.cpython-313.pyc
Executable file
Binary file not shown.
BIN
labPython/lab7/__pycache__/sort_methods.cpython-313.pyc
Executable file
BIN
labPython/lab7/__pycache__/sort_methods.cpython-313.pyc
Executable file
Binary file not shown.
BIN
labPython/lab7/__pycache__/sort_methods.cpython-314.pyc
Executable file
BIN
labPython/lab7/__pycache__/sort_methods.cpython-314.pyc
Executable file
Binary file not shown.
30
labPython/lab7/input.txt
Executable file
30
labPython/lab7/input.txt
Executable file
@@ -0,0 +1,30 @@
|
||||
37
|
||||
-44
|
||||
40
|
||||
93
|
||||
76
|
||||
-73
|
||||
-80
|
||||
-11
|
||||
48
|
||||
-67
|
||||
1
|
||||
-46
|
||||
-58
|
||||
-85
|
||||
67
|
||||
96
|
||||
8
|
||||
36
|
||||
-88
|
||||
-21
|
||||
54
|
||||
-2
|
||||
21
|
||||
-25
|
||||
-48
|
||||
71
|
||||
-3
|
||||
3
|
||||
71
|
||||
-55
|
||||
30
labPython/lab7/output.txt
Executable file
30
labPython/lab7/output.txt
Executable file
@@ -0,0 +1,30 @@
|
||||
-88
|
||||
-85
|
||||
-80
|
||||
-73
|
||||
-67
|
||||
-58
|
||||
-55
|
||||
-48
|
||||
-46
|
||||
-44
|
||||
-25
|
||||
-21
|
||||
-11
|
||||
-3
|
||||
-2
|
||||
1
|
||||
3
|
||||
8
|
||||
21
|
||||
36
|
||||
37
|
||||
40
|
||||
48
|
||||
54
|
||||
67
|
||||
71
|
||||
71
|
||||
76
|
||||
93
|
||||
96
|
||||
21
labPython/lab7/sort2.py
Executable file
21
labPython/lab7/sort2.py
Executable file
@@ -0,0 +1,21 @@
|
||||
from os import replace
|
||||
import sort_methods
|
||||
from random import randint
|
||||
|
||||
array = [randint(-100, 100) for _ in range(30)]
|
||||
with open("input.txt", "w") as inputFile:
|
||||
for element in array:
|
||||
inputFile.write(f"{element}\n")
|
||||
listOfNumber = []
|
||||
with open("input.txt", "r") as inputFile:
|
||||
for string in inputFile:
|
||||
listOfNumber.append(int(string.replace("\n", "")))
|
||||
print(listOfNumber)
|
||||
listOfNumber = sort_methods.bubble_sort(listOfNumber)
|
||||
|
||||
with open("output.txt", "w") as outputFile:
|
||||
for element in listOfNumber:
|
||||
outputFile.write(f"{element}\n")
|
||||
|
||||
print("Пузырьковая сортировка")
|
||||
print("сложность сортировки O(n^2)")
|
||||
32
labPython/lab7/sort_methods.py
Executable file
32
labPython/lab7/sort_methods.py
Executable file
@@ -0,0 +1,32 @@
|
||||
def printSortedList(list: list, sortedCount: int) -> None:
|
||||
text = ""
|
||||
for index in range(len(list)):
|
||||
if index != sortedCount:
|
||||
text += f"{list[index]}, "
|
||||
|
||||
else:
|
||||
if sortedCount != 0:
|
||||
text += f"{list[index]}|"
|
||||
else:
|
||||
text += f"отсортированый массив => {list[index]}, "
|
||||
print(text)
|
||||
|
||||
|
||||
def bubble_sort(list: list) -> list:
|
||||
count = 1
|
||||
sortedCount = len(list) - 1
|
||||
while count != 0:
|
||||
count = 0
|
||||
for index in range(len(list)):
|
||||
if index < len(list) - 1:
|
||||
if list[index] > list[index + 1]:
|
||||
list[index] += list[index + 1]
|
||||
list[index + 1] = list[index] - list[index + 1]
|
||||
list[index] = list[index] - list[index + 1]
|
||||
count += 1
|
||||
if count != 0:
|
||||
sortedCount -= 1
|
||||
else:
|
||||
sortedCount = 0
|
||||
printSortedList(list, sortedCount)
|
||||
return list
|
||||
Reference in New Issue
Block a user