22 - python & all web labs

This commit is contained in:
2026-05-02 20:27:02 +05:00
parent ef77f101fa
commit 19af922baf
5781 changed files with 770492 additions and 96 deletions

29
labPython/lab21/task_1.py Normal file
View File

@@ -0,0 +1,29 @@
import math
def simple_gen5(lst):
for x in lst:
if x >= 0:
yield math.sqrt(x)
else:
yield None
while True:
print("генерация квадратных корней")
print("1 - Ввести список")
print("0 - Выход")
choice = input("Выбор: ")
if choice == "0":
break
if choice == "1":
try:
data = list(map(int, input("Введите числа через пробел: ").split()))
gen = simple_gen5(data)
print("Результат:")
for val in gen:
print(val)
except:
print("Ошибка ввода")

25
labPython/lab21/task_2.py Normal file
View File

@@ -0,0 +1,25 @@
def filter_gen5(s):
vowels = "aeiouAEIOU"
for ch in s:
if ch in vowels:
yield ch
while True:
print("фильтрация гласных")
print("1 - Ввести строку")
print("0 - Выход")
choice = input("Выбор: ")
if choice == "0":
break
if choice == "1":
try:
s = input("Введите строку: ")
gen = filter_gen5(s)
print("Результат:")
for ch in gen:
print(ch)
except:
print("Ошибка")

26
labPython/lab21/task_3.py Normal file
View File

@@ -0,0 +1,26 @@
def calc_gen4(a, r):
current = a
while True:
yield current
current *= r
while True:
print("геометрическая прогрессия")
print("1 - Запустить")
print("0 - Выход")
choice = input("Выбор: ")
if choice == "0":
break
if choice == "1":
try:
a = float(input("Первый член: "))
r = float(input("Знаменатель: "))
n = int(input("Сколько элементов вывести: "))
gen = calc_gen4(a, r)
for _ in range(n):
print(next(gen))
except:
print("Ошибка ввода")

36
labPython/lab21/task_4.py Normal file
View File

@@ -0,0 +1,36 @@
class SimpleIter4:
def __init__(self, data):
self.data = data
self.result = []
evens = [data[i] for i in range(0, len(data), 2)][::-1]
odds = [data[i] for i in range(1, len(data), 2)]
self.result = evens + odds
self.index = 0
def __iter__(self):
return self
def __next__(self):
if self.index >= len(self.result):
raise StopIteration
val = self.result[self.index]
self.index += 1
return val
while True:
print("1 - Ввести список")
print("0 - Выход")
choice = input("Выбор: ")
if choice == "0":
break
if choice == "1":
try:
data = input("Введите элементы через пробел: ").split()
it = SimpleIter4(data)
for x in it:
print(x)
except:
print("Ошибка")

64
labPython/lab21/task_5.py Normal file
View File

@@ -0,0 +1,64 @@
class Node:
def __init__(self, value):
self.value = value
self.next = None
self.prev = None
class DoublyCircularList:
def __init__(self):
self.head = None
def append(self, value):
new_node = Node(value)
if not self.head:
self.head = new_node
new_node.next = new_node
new_node.prev = new_node
else:
tail = self.head.prev
tail.next = new_node
new_node.prev = tail
new_node.next = self.head
self.head.prev = new_node
def __iter__(self):
return ReverseIterator(self.head)
class ReverseIterator:
def __init__(self, head):
self.current = head.prev if head else None
def __iter__(self):
return self
def __next__(self):
if not self.current:
raise StopIteration
val = self.current.value
self.current = self.current.prev
return val
while True:
print("1 - Создать список и начать итерацию")
print("0 - Выход")
choice = input("Выбор: ")
if choice == "0":
break
if choice == "1":
try:
data = input("Введите элементы через пробел: ").split()
lst = DoublyCircularList()
for x in data:
lst.append(x)
it = iter(lst)
n = int(input("Сколько элементов вывести: "))
for _ in range(n):
print(next(it))
except:
print("Ошибка")