/
Nik_voz
/
rgpu_python_analysis
Обзор
Документация
Войти
/
Nik_voz
/
rgpu_python_analysis
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
python_analysis/conditional_operator.py
86 строк
2 KB
«Nick-voz»
feat: Add conditional operator exercises
16 фев 2025, 23:15
16 фев 2025, 23:15
d61801c
Код
Авторство
О чём код?
import calendar from math import cos from math import log def conditional_operator_1(a: float) -> float: if a <= 0: return 0 return a if a <= 1 else a**4 def conditional_operator_2(x: float) -> float: if x < 2: return cos(x) ** 2 return x**2 if x <= 4 else log(x**3) def conditional_operator_3(x: float) -> float: if x <= 1: return abs(x) return 1 if x <= 2 else (-1) * x + 5 def conditional_operator_4(a: str) -> str | None: try: _a = int(a) except ValueError: # there should be not integer value processing return None # I really don't want to mindlessly write a million if/else lines. figures = { 1: "(1/2)ah", # triangle 2: "ah", # parallelogram 3: "ab", # rectangle 4: "(1/2)*d_1*d_2", # rhombus 5: "a^2", # square 6: "PI*r^2", # circle } if _a not in figures: # 1. There should be out of range value processing # 2. There may be just "figures.get(_x, default=None)" - # - this has same effect as entire condition return None return figures[_a] def conditional_operator_5(a: str) -> str | None: try: _a = int(a) - 1 # to avoid collisions ( 1 is 0 in lists) except ValueError: # there should be not integer value processing return None # I will continue to avoid writing millions of dumb if/else lines # by all possible means. punctuation_symbols = [ "dot", # . "question mark", # ? "exclamation mark", # ! "colon", # : "semicolon", # ; "dash", # - ] if _a > len(punctuation_symbols) - 1 or _a < 0: # There should be out of range value processing return None return punctuation_symbols[_a] def conditional_operator_6(a: str) -> str | None: try: _a = int(a) except ValueError: # there should be not integer value processing return None if not (1 <= _a <= 12): # There should be out of range value processing return None return calendar.month_name[_a]