Загрузка данных


Понял, нужно для каждого задания отдельный фрагмент кода. Ниже привожу решения для заданий 1–10: каждый блок можно копировать отдельно и запускать.

---

Задание 1. Сравнение двух чисел

```python
def compare(a, b):
    if a < b:
        return -1
    elif a > b:
        return 1
    else:
        return 0

# Тестирование
print(compare(5, 3))   # 1
print(compare(2, 7))   # -1
print(compare(4, 4))   # 0
print(compare(-1, -5)) # 1
print(compare(0, 0.0)) # 0
```

---

Задание 2. Факториал числа

```python
def factorial(n):
    if not isinstance(n, int) or n < 0:
        raise ValueError("Факториал определён только для неотрицательных целых чисел")
    if n == 0:
        return 1
    result = 1
    for i in range(2, n + 1):
        result *= i
    return result

# Тестирование
for val in [0, 1, 5, 7]:
    print(f"factorial({val}) = {factorial(val)}")
```

---

Задание 3. Цифры в число

```python
def digits_to_number(d1, d2, d3):
    for d in (d1, d2, d3):
        if not (0 <= d <= 9):
            raise ValueError("Каждый аргумент должен быть цифрой от 0 до 9")
    return d1 * 100 + d2 * 10 + d3

# Тестирование
print(digits_to_number(1, 4, 9))   # 149
print(digits_to_number(0, 0, 1))   # 1
print(digits_to_number(9, 9, 9))   # 999
print(digits_to_number(0, 5, 0))   # 50
```

---

Задание 4. Площадь прямоугольника и квадрата

```python
def area(length, width=None):
    if width is None:
        return length * length
    else:
        return length * width

# Тестирование
print(area(5))        # квадрат 5x5 = 25
print(area(5, 10))    # прямоугольник 5x10 = 50
print(area(2.5, 3))   # 7.5
print(area(7))        # 49
```

---

Задание 5. Совершенное число

```python
import math

def is_perfect(n):
    if n <= 1:
        return False
    sum_divisors = 1
    for i in range(2, int(math.isqrt(n)) + 1):
        if n % i == 0:
            sum_divisors += i
            other = n // i
            if other != i:
                sum_divisors += other
    return sum_divisors == n

# Тестирование
test_nums = [6, 28, 496, 8128, 1, 12]
for n in test_nums:
    print(f"{n}: {is_perfect(n)}")
```

---

Задание 6. Диапазон совершенных чисел

```python
import math

def is_perfect(n):
    if n <= 1:
        return False
    sum_divisors = 1
    for i in range(2, int(math.isqrt(n)) + 1):
        if n % i == 0:
            sum_divisors += i
            other = n // i
            if other != i:
                sum_divisors += other
    return sum_divisors == n

def find_perfect_numbers(min_val, max_val):
    perfects = []
    for num in range(min_val, max_val + 1):
        if is_perfect(num):
            perfects.append(num)
    return perfects

# Тестирование
print(find_perfect_numbers(1, 10000))  # [6, 28, 496, 8128]
```

---

Задание 7. Вывод времени в формате чч:мм:сс

```python
def print_time(hours, minutes=0, seconds=0):
    total_seconds = hours * 3600 + minutes * 60 + seconds
    h = (total_seconds // 3600) % 24
    m = (total_seconds % 3600) // 60
    s = total_seconds % 60
    print(f"{h:02d}:{m:02d}:{s:02d}")

# Тестирование
print_time(14, 30, 45)   # 14:30:45
print_time(7, 5)         # 07:05:00
print_time(23, 0, 1)     # 23:00:01
print_time(0, 0, 0)      # 00:00:00
print_time(25, 10)       # 01:10:00 (нормализация)
```

---

Задание 8. Перевод времени в секунды

```python
def to_seconds(hours, minutes, seconds):
    return hours * 3600 + minutes * 60 + seconds

# Тестирование
print(to_seconds(1, 0, 0))    # 3600
print(to_seconds(0, 10, 5))   # 605
print(to_seconds(0, 0, 1))    # 1
```

---

Задание 9. Перевод секунд в строку времени

```python
def from_seconds(total_seconds):
    total_seconds = abs(total_seconds)
    hours = total_seconds // 3600
    minutes = (total_seconds % 3600) // 60
    seconds = total_seconds % 60
    return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}"

# Тестирование
print(from_seconds(3661))   # 01:01:01
print(from_seconds(86399))  # 23:59:59
print(from_seconds(0))      # 00:00:00
print(from_seconds(90))     # 00:01:30
```

---

Задание 10. Разница между датами

```python
def to_seconds(hours, minutes, seconds):
    return hours * 3600 + minutes * 60 + seconds

def from_seconds(total_seconds):
    total_seconds = abs(total_seconds)
    hours = total_seconds // 3600
    minutes = (total_seconds % 3600) // 60
    seconds = total_seconds % 60
    return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}"

def time_difference(h1, m1, s1, h2, m2, s2):
    sec1 = to_seconds(h1, m1, s1)
    sec2 = to_seconds(h2, m2, s2)
    diff = abs(sec2 - sec1)
    return from_seconds(diff)

# Тестирование
print(time_difference(10, 30, 0, 12, 15, 0))   # 01:45:00
print(time_difference(23, 59, 0, 0, 1, 30))    # 23:57:30
print(time_difference(0, 0, 0, 0, 0, 1))       # 00:00:01
print(time_difference(8, 0, 0, 8, 0, 0))       # 00:00:00
```

---

Каждый фрагмент можно скопировать в отдельный файл или в один файл, но запускать по очереди. Во всех заданиях предусмотрены тестовые вызовы.