Skip to content
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

Danila bludov #25

Open
wants to merge 10 commits into
base: Danila_Bludov
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
23 changes: 23 additions & 0 deletions src/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def summ(a, b):
return a + b


n = int(input())

arr = input()
str_lst = arr.split(" ")
res = []
for item in str_lst:
res.append (int(item))
#print(res)
n = len(res)
for i in range(0, n-1):
#print(f"i={i}")
for j in range(0, n-1-i):
#print (f"j={i}")
if res[j] > res[j+1]:
res[j] , res[j+1] = res[j+1], res[j]
print(" ".join(map(str,res)))
print (res)


2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# print("Danila_BLudov")
print ("Hello, " + input () + "!")
7 changes: 7 additions & 0 deletions src/main1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
inp = "10 3"
splt = inp.split(" ")
print(splt)
res = []
for item in splt:
res.append(int(item))
print(res[0]+res[1])
2 changes: 2 additions & 0 deletions src/main2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a,b = [int(item) for item in input().split(" ")]
print(a + b)
16 changes: 16 additions & 0 deletions src/main3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def sort (a , n):
for i in range(0 , n):
for j in range(0 , n-i-1):
if a[j] > a[j + 1]:
a[j + 1] , a[j] = a[j] , a[j + 1]2



n = "4" #input()
inp = "4 3 2 1"
lst = inp.split(" ")
res = []
for i in range (0 , n):
res.append(int(lst[i]))

print(res)
Empty file added src/module3/2_1.py
Empty file.
Empty file added src/module3/2_2.py
Empty file.
Empty file added src/module3/2_3.py
Empty file.
Empty file added src/module3/2_4.py
Empty file.
Empty file added src/module3/2_5.py
Empty file.
Empty file added src/module3/2_6.py
Empty file.
Empty file added src/module3/2_7.py
Empty file.
28 changes: 28 additions & 0 deletions src/module3/3_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def get_hash(s, k, p, d):
res = 0
for j in range(0, k):
res=(res*d+ord(s[j]))%p
return res


def rabin_karp(s, q, p, d):
hq=get_hash(q, len(q), p, d)
hs=get_hash(s, len(q), p, d)
dq=1
l=[]
for j in range (len(q)):
dq=(dq*d)%p
for j in range(len(s)-len(q)+1):
if hq==hs:
l.append(j)
if j+len(q)<len(s):
hs=(hs*d-ord(s[j])*dq+ord(s[j+len(q)]))%p

print(" ".join(map(str, l)))
def main():
s=input()
q=input()
p=1e9+7
d=26
rabin_karp(s,q,p,d)
main()
31 changes: 31 additions & 0 deletions src/module3/3_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def hash(A, n):
res = 0
for i in range(n):
res = (res * p + ord(A[i])) % d
return res

def rabin_karp(s, q, n):
hs_hash = hash(s, n)
hq_hash = hash(q, n)

dq = 1
for i in range(n-1):
dq = (dq * p) % d
if hs_hash == hq_hash:
return 0
for i in range(n-1):
hq_hash = (hq_hash - ord(q[i]) * dq) * p
hq_hash += ord(q[i])
hq_hash %= d
if hs_hash== hq_hash:
return i+1
return -1

s = input()
q = input()

p = 31
d = 2 ** 31 - 1

A = rabin_karp(s, q, len(s))
print(A)
22 changes: 22 additions & 0 deletions src/module3/3_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def pref_funct(s,n):
k = [0] * n
k[0] = 0
for i in range (n - 1):
j = k[i]
while (j > 0) and (s[i + 1] != s[j]):
j = k[j - 1]
if (s[i + 1] == s[j]):
k[i+1] = j + 1
else:
k[i + 1] = 0
return k
#префикс функция

A = input()
n=len(A)
res = pref_funct(A,n)
k = n - res[n-1]
if (n % k == 0):
print(n//k)
else:
print(1)
18 changes: 18 additions & 0 deletions src/module3/3_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def pref_funct(s,n):
k = [0] * n
k[0] = 0
for i in range (n - 1):
j = k[i]
while (j > 0) and (s[i + 1] != s[j]):
j = k[j - 1]
if (s[i + 1] == s[j]):
k[i+1] = j + 1
else:
K[i + 1] = 0
return k
#префикс функция

A = input()
n = len(A)
pref = pref_funct(A,n)
print(n - pref[-1])
20 changes: 20 additions & 0 deletions src/module3/4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def pref_funct(s):
n = len(s)
k = [0] * n
k[0] = 0
for i in range (n - 1):
j = k[i]
while (j > 0) and (s[i + 1] != s[j]):
j = k[j - 1]
if (s[i + 1] == s[j]):
k[i+1] = j + 1
else:
k[i + 1] = 0
return k
#префикс функция

text = str(input())
s = list(text)
n = len(s)
pref = pref_funct(s)
print(n - pref[-1])
14 changes: 14 additions & 0 deletions src/module3/4_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def funct(s):
k = 0
l = []
for i in s:
if (i == '('):
l.append(i)
elif (l != []) and (l[-1] == '('):
l.pop()
else:
k += 1
return k+len(l)

s = input()
print(funct(s))
23 changes: 23 additions & 0 deletions src/module3/4_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def poezd_sort(A, n):
stack = []
B = []
poezd_sorted = sorted(A)
while(len(B) != n):
if (stack == []):
stack.append(A[0])
A.pop(0)
continue
if (len(A) > 0) and (A[0] <= stack[-1]):
stack.append(A[0])
A.pop(0)
else:
B.append(stack[-1])
stack.pop()
return B == poezd_sorted

N = int(input())
poezd = list(map(int, input().split()))
if poezd_sort(poezd, N):
print("YES")
else:
print("NO")
23 changes: 23 additions & 0 deletions src/module3/4_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def poisk(A, n):
stack = [n-1]
a = [0]*n #a-индексы
a[n-1] = -1
for i in range(n-2, -1, -1):
if A[stack[-1]] >= A[i]:
while (stack != []) and (A[stack[-1]] >= A[i]):
stack.pop()
if (stack == []):
a[i] = -1
else:
a[i] = stack[-1]
stack.append(i)
else:
a[i] = stack[-1]
stack.append(i)
return a


N = int(input())
A = list(map(int, input().split()))
A = poisk(A, N)
print(' '.join(map(str, A)))
18 changes: 18 additions & 0 deletions src/module3/4_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def min_otrezok(A, n, k):
stack = []
for i in range(k):
while (stack != []) and (A[i] <= A[stack[-1]]):
stack.pop()
stack.append(i)
for i in range(k, n):
print(A[stack[0]])
while (stack != []) and (i-k >= stack[0]):
stack.pop(0)
while (stack != []) and (A[i] <= A[stack[-1]]):
stack.pop()
stack.append(i)
print(A[stack[0]])

N, K = map(int, input().split())
A = list(map(int, input().split()))
min_otrezok(A, N, K)
55 changes: 55 additions & 0 deletions src/module3/5_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Node:
__size = 0
__height = 0

def __init__(self, data):
self.data = data
self.left = None
self.right = None
Node.__size += 1

@property
def size(self):
return self.__size

@property
def height(self):
return self.__height

def add(self, value, depth):
if self.data == value:
return
if value < self.data:
if self.left:
self.left.add(value, depth+1)
else:
if Node.__height < depth:
Node.__height = depth
self.left = Node(value)
else:
if self.right:
self.right.add(value, depth+1)
else:
if Node.__height < depth:
Node.__height = depth
self.right = Node(value)

def find_forks(self):
if self.left:
self.left.find_forks()
if self.left and not self.right:
print(self.data)
if self.right and not self.left:
print(self.data)
if self.right:
self.right.find_forks()

def build_tree(elmnt):
tree = Node(elmnt[0])
for i in range(1, len(elmnt)-1):
tree.add(elmnt[i], 1)
return tree

lisst = list(map(int, input().split()))
tree = build_tree(lisst)
tree.find_forks()
47 changes: 47 additions & 0 deletions src/module3/5_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None

def add(self, data):
if data == self.data:
return
if data < self.data:
if self.left:
self.left.add(data)
else:
self.left = Node(data)
else:
if self.right:
self.right.add(data)
else:
self.right = Node(data)

def height(tree):
if tree is None:
return 0
return max(height(tree.left), height(tree.right))+1

def build_tree(elements):
root = Node(elements[0])
for i in range(1, len(elements)):
root.add(elements[i])
return root

def balance(tree):
if not tree or ((height(tree.left) == height(tree.right) or height(tree.left)+1 == height(tree.right) or height(tree.left) == height(tree.right)+1) and balance(tree.right) and balance(tree.left)):
return True
return False

def main():
n = [int(i) for i in input().split()]
n.pop()
tree = build_tree(n)

if balance(tree):
print("YES")
else:
print("NO")

main()
Loading