Skip to content

Interesting Python Cases #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 177 additions & 0 deletions Интересные_кейсы/Excercises.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
def exercise1():
count = 0
s = ''
x = 0
xs = 0
for a in range(1, 35):
for b in range(1, 35):
for c in range(1, 35):
for d in range(1, 35):
if (a != b) and (a != c) and (a != d) and (b != c) and (b != d) and (c != d):
if ((a ** 3) + (b ** 3)) == ((c ** 3) + (d ** 3)):
count += 1
x = (a ** 3) + (b ** 3)
s += str(x) + ' '
if str(x) in s:
xs += 1
if xs == 1:
print('x =', x)
xs = 0
else:
xs = 0
if count == 5:
break
if count == 5:
break
if count == 5:
break
return 'Конец программы'

#exercise1()

def exercise2():
age = 27
txt = 'My name is Timur, I am {}'.format(age)
print(txt)
return 'Конец программы'

#exercise2()

def exercise3():
from time import time
start = time()
a, flag = [17, 24, 91, 96, 67, -27, 79, -71, -71, 58, 48, 88, 88, -16, -78, 96, -76, 56, 92, 1, 32, -17, 36, 88,
-61, -97, -37, -84, 50, 47, 94, -6, 52, -76, 93, 14, -32, 98, -65, -16, -9, -68, -20, -40, -71, 93, -91,
44, 25, 79, 97, 0, -94, 7, -47, -96, -55, -58, -78, -78, -79, 75, 44, -56, -41, 38, 16, 70, 17, -17, -24,
-83, -74, -73, 11, -26, 63, -75, -19, -13, -51, -74, 21, -8, 21, -68, -66, -84, -95, 78, 69, -29, 39, 38,
-55, 7, -11, -26, -62, -84], 0

n = len(a)
for i in range(n - 1):
for j in range(n - i - 1):
if a[j] > a[j + 1]:
a[j], a[j + 1] = a[j + 1], a[j]
flag += 1
if flag == 0:
break
print(a)
print(time() - start)

#exercise3()

def exercise4():
a, a1 = [78, -32, 5, 39, 58, -5, -63, 57, 72, 9, 53, -1, 63, -97, -21, -94, -47, 57, -8, 60, -23, -72, -22, -79, 90,
96, -41, -71, -48, 84, 89, -96, 41, -16, 94, -60, -64, -39, 60, -14, -62, -19, -3, 32, 98, 14, 43, 3, -56,
71, -71, -67, 80, 27, 92, 92, -64, 0, -77, 2, -26, 41, 3, -31, 48, 39, 20, -30, 35, 32, -58, 2, 63, 64, 66,
62, 82, -62, 9, -52, 35, -61, 87, 78, 93, -42, 87, -72, -10, -36, 61, -16, 59, 59, 22, -24, -67, 76, -94,
59], []
cax = a[0]
n = len(a)
# реализация алгоритма сортировки выбором
for i in range(n):
cax = max(a)
sd = a.index(cax)
del a[sd]
a1.append(cax)
a1.reverse()
print(a1)

#exercise4()

def find_all(target, symbol):
return [i for i in range(len(target)) if target[i] == symbol]

def exercise5():
s = 'abcdabcaaa'
char = 'a'
print(find_all(s, char))

#exercise5()

def row_sum_odd_numbers(n):
sx = 0
if n == 1:
print(1)
else:
ind2 = (n - 1) + (n * n)
ind1 = ind2 - ((n - 1) * 2)
for i in range(ind1, (ind2 + 1), 2):
sx += i
print(sx)

def exercise6():
row_sum_odd_numbers(2)

#exercise6()

def convert_to_python_case(text):
s = ''
for i in range(len(text)):
if i == 0:
s += text[i].lower()
continue
if text[i].isupper():
s += '_' + text[i].lower()
else:
s += text[i]
return s

def exercise7():
txt = 'ThisIsCamelCased'
print(convert_to_python_case(txt))

#exercise7()

def exercise8():
x, s, y = 15 // 2, '*', ''
for i in range(8):
y = ' ' * (x) + s
y.strip()
print(y)
s += '**'
x -= 1

#exercise8()

def exercise9(num):
v1 = ['один', 'два', 'три', 'четыре', 'пять', 'шесть', 'семь', 'восемь', 'девять']
v2 = ['одиннадцать', 'двенадцать', 'тринадцать', 'четырнадцать', 'пятнадцать', 'шестнадцать', 'семнадцать',
'восемнадцать', 'девятнадцать']
v3 = ['десять', 'двадцать', 'тридцать', 'сорок', 'пятьдесят', 'шестьдесят', 'семьдесят', 'восемьдесят',
'девяносто']

if num % 10 == 0:
x = num // 10
return v3[x - 1]
elif num < 10:
return v1[num - 1]
elif 11 <= num <= 19:
x = num % 10
return v2[x - 1]
elif 20 < num < 100:
x1 = num // 10
x2 = num % 10
xx = v3[x1 - 1] + ' ' + v1[x2 - 1]
return xx

#for i in range(1, 100):
#print(exercise9(i))

def is_pangram(text):
text1 = text.lower()
alph = [chr(i) for i in range(97, 123)]
for i in range(len(text)):
if text1[i] == ' ':
continue
elif (text1[i] in alph):
x = alph.index(text1[i])
del alph[x]
if len(alph) == 0:
return True
else:
return False

def exercise10(text):
print(is_pangram(text))

#exercise10('Jackdaws love my big sphinx of quartz')
29 changes: 29 additions & 0 deletions Интересные_кейсы/Search_Internet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import time
import pyautogui
import webbrowser

def write_comment():
pyautogui.moveTo(1209, 1010, duration=0.5)
pyautogui.click()
time.sleep(0.25)
pyautogui.typewrite('good!')
pyautogui.press('enter')


def maint():
webbrowser.open('https://www.google.com/')

time.sleep(1)
pyautogui.moveTo(611, 339, duration=0.25)
pyautogui.click()

pyautogui.typewrite('python hello world', 0.05)
pyautogui.press('enter')

time.sleep(1)
pyautogui.moveTo(330, 209, duration=0.25)
pyautogui.click()

write_comment()

maint()
16 changes: 16 additions & 0 deletions Интересные_кейсы/Table_X.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
a, b, c, d = int(input()), int(input()), int(input()), int(input())
k = c
print(" ", end="\t")

while c != (d + 1): # печатаем первую строку с изначальным табом)
print(c, end="\t")
c += 1
if c == (d + 1): # переходим на следующую строку
print()

while a != (b + 1): # заполняем оставшиеся столбцы таблицы умножения
print(a, end="\t")
for w in range(k, c):
print(w * a, end="\t")
print()
a += 1
22 changes: 22 additions & 0 deletions Интересные_кейсы/club_proga.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import itertools

def union(A, B):
return {a + b for a in A for b in B}
teams = union('w', '12') | union('l', '0') | union('N', '3456789')

import itertools
from fractions import Fraction

def Omega(p, k):
return {' '.join(comb) for comb in itertools.combinations(p, k)}

omega = Omega(teams, 5)
print(len(omega))
A = {a for a in omega if a.count('w') == 1}
print(len(A))
# A - события, а Р - это вероятность событий

def P(event, space):
return Fraction(len(event & space), len(space))

print('P(A)=', P(A, omega))
27 changes: 27 additions & 0 deletions Интересные_кейсы/Генераторы.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
print('----------9. Генераторы----------')
print()
print((i for i in range(100)), ' -> ')
print('->', 'это объект-генератор, в котором просто есть range(100)')

print('Тернарный оператор с условиями :')
c = 1 if type('privet') == str else 500
print('Итоговый ответ тут равен', c)

print('Выражение посложнее :')
b = (i if i > 30 else 1 / i for i in range(50) if i % 5 in [1, 4])

for i in b:
print(i)

print('Генератор словарей :')
a = {i: i * 20 for i in range(10)}
print(a)

gen = [ъ for ъ in range(11)]
print(gen)
gen1 = [i for i in range(11) if i%3 == 0]
print('Элементы, кратные 3:', gen1)
gen1 = [1 if i%3 == 0 else 0 for i in range(11)] # 1 - если кратен трем, 0 - если не кратен
print(gen1)
gen2 = {i:i*'Сынба' for i in range(1,11)} # генератор словаря
print(gen2)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
def is_correct_bracket_seq(seq):
r_brackets = 0
s_brackets = 0
braces = 0
for i in seq:
if i == '{':
braces += 1
if i == '}':
if braces == 0:
braces += 99
braces -= 1
if i == '[':
s_brackets += 1
if i == ']':
if s_brackets == 0:
s_brackets += 99
s_brackets -= 1
if i == '(':
r_brackets += 1
if i == ')':
if r_brackets == 0:
r_brackets += 99
r_brackets -= 1
if r_brackets + s_brackets + braces == 0:
print('True')
else:
print('False')


def main():
sequence = input()
is_correct_bracket_seq(sequence)


main()