Advertisement
Hasli4

String.Tasks

Jun 15th, 2025
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.22 KB | None | 0 0
  1. # 1. Гласные и согласные
  2. def count_vowels_consonants(s):
  3.     s = s.lower()
  4.     vowels = set("аеёиоуыэюя")
  5.     consonants = set("бвгджзйклмнпрстфхцчшщ")
  6.     v_count = sum(1 for ch in s if ch in vowels)
  7.     c_count = sum(1 for ch in s if ch in consonants)
  8.     print("Гласных:", v_count, "Согласных:", c_count)
  9.  
  10. # 2. Имя из e‑mail
  11. def extract_username(email):
  12.     if "@" not in email:
  13.         print("Ошибка: нет символа @")
  14.     else:
  15.         print(email.split("@", 1)[0])
  16.  
  17. # 3. Нормализация пробелов
  18. def normalize_spaces(s):
  19.     parts = s.split()
  20.     result = " ".join(parts)
  21.     print(result)
  22.  
  23. # 4. Палиндром
  24. import re
  25. def is_palindrome(phrase):
  26.     cleaned = re.sub(r'[^A-Za-zА-Яа-я0-9]', '', phrase).lower()
  27.     print(cleaned == cleaned[::-1])
  28.  
  29. # 5. Шифр Цезаря (русский алфавит)
  30. def caesar_rus(text, k):
  31.     alpha = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя"
  32.     def shift(ch):
  33.         lower = ch.lower()
  34.         if lower in alpha:
  35.             i = alpha.index(lower)
  36.             new = alpha[(i + k) % len(alpha)]
  37.             return new.upper() if ch.isupper() else new
  38.         return ch
  39.     print("".join(shift(ch) for ch in text))
  40.  
  41. # 6. Проверка пароля
  42. def check_password(pwd):
  43.     ok = True
  44.     if len(pwd) < 8:
  45.         print("Пароль слишком короткий")
  46.         ok = False
  47.     if not any(ch.isdigit() for ch in pwd):
  48.         print("Нет цифр")
  49.         ok = False
  50.     if not any(ch.islower() for ch in pwd):
  51.         print("Нет строчных букв")
  52.         ok = False
  53.     if not any(ch.isupper() for ch in pwd):
  54.         print("Нет заглавных букв")
  55.         ok = False
  56.     if ok:
  57.         print("Пароль надёжен")
  58.  
  59. # 7. Формат даты
  60. def reformat_date(dt):
  61.     parts = dt.split(".")
  62.     if len(parts) != 3 or not all(p.isdigit() for p in parts):
  63.         print("Некорректный ввод")
  64.     else:
  65.         d, m, y = parts
  66.         if len(d)==2 and len(m)==2 and len(y)==4:
  67.             print(f"{y}-{m}-{d}")
  68.         else:
  69.             print("Некорректный ввод")
  70.  
  71. # 8. Аббревиатура ФИО
  72. def abbrev_fio(fio):
  73.     parts = fio.split()
  74.     if len(parts)==3:
  75.         last, first, patron = parts
  76.         print(f"{last} {first[0]}.{patron[0]}.")
  77.     else:
  78.         print("Неверный формат ФИО")
  79.  
  80. # 9. Частотный анализ
  81. import string
  82. def top3_words(text):
  83.     text = text.lower()
  84.     for p in string.punctuation:
  85.         text = text.replace(p, " ")
  86.     words = [w for w in text.split() if w]
  87.     from collections import Counter
  88.     cnt = Counter(words)
  89.     for word, _ in cnt.most_common(3):
  90.         print(word)
  91.  
  92. # 10. Парсер параметров URL
  93. def parse_url_params(url):
  94.     if "?" not in url:
  95.         print("Нет параметров")
  96.         return
  97.     qs = url.split("?",1)[1]
  98.     pairs = qs.split("&")
  99.     for pair in pairs:
  100.         if "=" in pair:
  101.             k, v = pair.split("=",1)
  102.             print(f"{k}:{v}")
  103.         else:
  104.             print(f"{pair}:")
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement