This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
СПИСКИ И РЕКУРСИЯ В ФУНКЦИИ | |
""" | |
# Примем на вход число и при помощи рекурсии найдем сумму всех цифр, из которых это число состоит | |
l = [] | |
def sum_digits(b): | |
if (b == 0): | |
return l |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
DICTIONARIES - основные манипуляции | |
""" | |
# Как смержить два словаря в один? | |
x = {'a': 1, 'b': 2} | |
y = {'b': 3, 'c': 4} | |
z = {**x, **y} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Классические алгоритмы из книги | |
Problem Solving with Algorithms and Data Structures Release 3 (by B.Miller, D.Ranum) | |
""" | |
# Bubble softing algorithm (сортировка пузырьком) | |
def bubble_sort(a_list): | |
for pass_num in range(len(a_list) - 1, 0, -1): | |
for i in range(pass_num): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Удаление элементов списка по условию | |
==================================== | |
""" | |
# Задача: из списка чисел удалить элементы, значения которых больше 35 и меньше 65. При этом удаляемые числа сохранить в другом списке. | |
# В Python с помощью инструкции del можно удалить элемент списка, указав сам список и индекс удаляемого элемента. | |
import random | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
СПИСОК + СЛОВАРЬ: подсчет количества одинаковых элементов в списке | |
================================================================== | |
""" | |
# Дан список целых чисел. Посчитать, сколько раз в нем встречается каждое число. | |
# Например, если дан список [1, 1, 3, 2, 1, 3, 4], то в нем число 1 встречается три раза, число 3 - два раза, числа 2 и 4 - по одному разу. | |
# Для хранения количества каждого встречающегося в списке значения создадим _словарь_. | |
# В нем ключами будут числа, которое встречается в списке, а значениями - количества этих чисел в списке. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
ЧАСТЫЙ (MOST COMMON) ЭЛЕМЕНТ ИЗ МАССИВА | |
""" | |
# Задание: Дан массив a из n целых чисел. Напишите код, который найдет число, что чаще других встречается в массиве. | |
# Формат вывода: единственное число x, наибольшее из чисел, которое чаще других встречается в массиве a. | |
# Решение: задачу можно решить как с использованием обычного словаря, так и с использованием структуры Counter из модуля collections: | |
from collections import Counter | |
from operator import itemgetter |
Строки
Какие виды строк бывают в питоне?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Implementation of Bubble Sorting (from geeks-to-geeks): | |
def bubble_sort(arr): | |
n = len(arr) | |
for i in range(n-1): | |
for j in range(0, n-i-1): | |
# Swap if the element found is greater, than the next element | |
if arr[j] > arr[j+1]: | |
arr[j], arr[j+1] = arr[j+1], arr[j] |