✅ Решения

⚡ Кратко

Решения используют yield, random, itertools.cycle и обработку исключений.

Решение 1. custom_range

def custom_range(start, stop, step=1):
    while start < stop:
        yield start
        start += step

for num in custom_range(2, 10, 2):
    print(num)

Решение 2. random_dates

import random


def is_leap(year: int) -> bool:
    return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)


def random_dates(year: int):
    days_in_month = {
        1: 31, 2: 29 if is_leap(year) else 28, 3: 31,
        4: 30, 5: 31, 6: 30, 7: 31, 8: 31,
        9: 30, 10: 31, 11: 30, 12: 31
    }
    while True:
        month = random.randint(1, 12)
        day = random.randint(1, days_in_month[month])
        yield f"{year}-{month:02d}-{day:02d}"


date_gen = random_dates(2025)
for _ in range(5):
    print(next(date_gen))

Решение 3. random_names

from random import choice


def random_names():
    first_names = ["Alice", "Bob", "Charlie", "David", "Emma"]
    last_names = ["Smith", "Johnson", "Brown", "Williams", "Jones"]
    while True:
        yield f"{choice(first_names)} {choice(last_names)}"


name_gen = random_names()
for _ in range(5):
    print(next(name_gen))

Решение 4. combine_generators

def combine_generators(*generators):
    while True:
        yield tuple(next(gen) for gen in generators)


name_gen = random_names()
date_gen = random_dates(2025)
data_gen = combine_generators(name_gen, date_gen)
for _ in range(5):
    print(next(data_gen))

Решение 5. Система распределения задач

# add_tasks.py
def add_tasks():
    while True:
        task = input("Введите задачу (или 'exit' для выхода): ").strip()
        if task.lower() == "exit":
            break
        with open("tasks.txt", "a", encoding="utf-8") as file:
            file.write(task + "\n")
        print("Задача добавлена!")


# assign_tasks.py
import itertools
import time


def task_assigner(employees, filename="tasks.txt"):
    employee_cycle = itertools.cycle(employees)
    attempts = 0
    retries = 5

    while attempts < retries:
        try:
            with open(filename, "r", encoding="utf-8") as file:
                while True:
                    task = file.readline().strip()
                    if task:
                        yield next(employee_cycle), task
                    else:
                        time.sleep(3)
        except FileNotFoundError:
            attempts += 1
            print(f"Файл не найден, попытка {attempts}/{retries}...")
            time.sleep(3)

    print("Файл так и не найден. Завершаем работу.")


employees = ["Alice", "Bob", "Charlie"]
for employee, task in task_assigner(employees):
    print(f"{employee} выполняет: {task}")