/
biggor
/
Python_for_Data_Science
Обзор
Документация
Войти
/
biggor
/
Python_for_Data_Science
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
13.11.py
59 строк
2 KB
biggor
upload files
19 ноя 2025, 23:34
19 ноя 2025, 23:34
74ee117
Код
Авторство
О чём код?
import random def generate_random_string(length): states = ['U', 'D', 'L', 'R'] return ''.join(random.choice(states) for _ in range(length)) # Выбираем случайную длину от, например, 10 до 30 random_length = random.randint(10, 30) input_string = generate_random_string(random_length) def compress_string(s): if not s: return "", 0, 0, 0 compressed = [] max_d_length = 0 d_incidents = 0 prev_char = s[0] count = 1 for i in range(1, len(s)): if s[i] == prev_char: count += 1 else: if prev_char == 'D': max_d_length = max(max_d_length, count) d_incidents += 1 if count == 1 or count == 2: compressed.append(prev_char * count) else: compressed.append(f'{prev_char}{count}') prev_char = s[i] count = 1 if prev_char == 'D': max_d_length = max(max_d_length, count) d_incidents += 1 if count == 1 or count == 2: compressed.append(prev_char * count) else: compressed.append(f'{prev_char}{count}') compressed_str = ''.join(compressed) compression_ratio = len(s) / len(compressed_str) if len(compressed_str) > 0 else 0 return compressed_str, compression_ratio, max_d_length, d_incidents compressed_string, compression_ratio, max_d_length, d_incidents = compress_string(input_string) print(f"Входная строка: {input_string}") print(f"Длина входной строки: {random_length}") print(f"Сжатая строка: {compressed_string}") print(f"Сжатие в разах: {compression_ratio:.2f}") print(f"Максимальная длина простоя D: {max_d_length}") print(f"Количество инцидентов простоя D: {d_incidents}")