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
from fake_useragent import UserAgent | |
def get_html(url): | |
response = requests.get(url, headers={'User-Agent': UserAgent().chrome}) | |
return response.text | |
# Результат UserAgent().chrome: | |
# 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.15 (KHTML, like Gecko) Chrome/24.0.1295.0 Safari/537.15' |
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
from time import time | |
# Time before running program | |
start = time() | |
# for i ...: ... | |
# Time after program done | |
print('Time', time() - start) |
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
# Greatest common divisor | |
# Наибольший общий делитель | |
a, b = 16, 17 | |
while b != 0: | |
# temp = a | |
# a = b | |
# b = temp % b | |
a, b = b, a%b | |
print(a) |
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
# Selection sort alghorirm | |
# Алгоритм сортировки выбором | |
a = [3, 5, 1, 0, 2, 8] | |
for j in range(1,len(a)): | |
i = j-1 | |
while i >= 0 and a[i] > a[i+1]: | |
a[i], a[i+1] = a[i+1], a[i] | |
i -= 1 |
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
# Reverse a string/ Переворот строки | |
a = 'Hello world!' | |
# 1 way | |
a = a[::-1]# Result: !dlrow olleH | |
# 2 way | |
reversed_string = '' | |
for i in a: | |
reversed_string += i# Result: !dlrow olleH | |
# 3 way |
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
# Factorial/ Факториал | |
def factorial(num): | |
if num <= 1: return 1 | |
return num * factorial(num-1) |
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
# Binary search/ бинарная сортировка | |
def binary_search(list, item): | |
# В переменных low и high хранятся | |
# границы той части списка, в которой | |
# выполняется поиск | |
low = 0 | |
high = len(list) - 1 | |
i = 0 | |
while low <= high: |
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
# Recursion exponentiation | |
# Рекурсивное возведение в степень | |
def fast_power(a,b): | |
# Базовый случай | |
if b == 0: return 1 | |
if b % 2 == 1: | |
return a * fast_power(a, b-1) | |
else: | |
return fast_power(a*a, b//2) |
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
# Fibonacci recursion | |
# Рекурсия чисел фибоначи | |
def fib(a): | |
# Базовый случай | |
if a <= 1: return a | |
else: return fib(a - 1) + fib(a - 2) | |
print(fib(int(input()))) |
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
'''N IS REQUIRED PARAMETЕR''' | |
def recursive_sum1(arr, n): | |
n -= 1 | |
if n < 0: | |
return 0 | |
return arr[n] + recursive_sum1(arr, n) | |
def recursive_sum2(arr, n): | |
# n - value of numbers | |
if n == len(arr): | |
return 0 |
OlderNewer