Skip to content

Latest commit

 

History

History
83 lines (79 loc) · 2.05 KB

python_basics_4.1.md

File metadata and controls

83 lines (79 loc) · 2.05 KB

Yandex handbook "Python Basics" answers

4.1. Функции. Области видимости. Передача параметров в функции

A. Функциональное приветствие

def print_hello(name):
    print(f'Hello, {name}!')   

B. Функциональный НОД

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

C. Длина числа

def number_length(number):
    return len(str(abs(number)))

D. Имя of the month

def month(number, language):
    MONTH = {
        'en': [
            'January', 'February', 'March',
            'April', 'May', 'June',
            'July', 'August', 'September',
            'October', 'November', 'December'
        ],
        'ru': [
            'Январь', 'Февраль', 'Март',
            'Апрель', 'Май', 'Июнь',
            'Июль', 'Август', 'Сентябрь',
            'Октябрь', 'Ноябрь', 'Декабрь'
        ]
    }
    return MONTH[language][number - 1]

E. Числовая строка

def split_numbers(line):
    return tuple(map(int, line.split()))

F. Модернизация системы вывода

lst = []


def modern_print(string):
    print(string) if string not in lst else None
    lst.append(string)

G. Шахматный «обед»

def can_eat(horse, shape):
    return abs(horse[0] - shape[0]) + abs(horse[1] - shape[1]) == 3

H. А роза упала на лапу Азора 7.0

def is_palindrome(n):
    return str(n) == str(n)[::-1] if isinstance(n, int) else n == n[::-1]

I. Простая задача 5.0

def is_prime(n):
    for i in range(2, int(n**0.5) + 1):
        if not n % i:
            return False
    return n != 1

J. Слияние

def merge(a, b):
    c = list(a) + list(b)
    n = len(c)
    for i in range(n):
        for j in range(0, n - i - 1):
            if c[j] > c[j + 1]:
                c[j], c[j + 1] = c[j + 1], c[j]
    return tuple(c)