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

task a #26

Open
wants to merge 24 commits into
base: Stepan_Bugasov
Choose a base branch
from
16 changes: 16 additions & 0 deletions src/Looped_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def looped_string(S, n):
res = [0]*n
res[0] = 0
for i in range(n-1):
x = res[i]
while (x > 0 and S[i+1] != S[x]):
x = res[x-1]
if (S[i+1] == S[x]):
res[i+1] = x + 1
else:
res[i+1] = 0
return res

T = input()
S = looped_string(T, len(T))
print(len(T) - S[len(S)-1])
14 changes: 14 additions & 0 deletions src/Psp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def Psp(s):
Counter=0
myStack = []
for c in s:
if (c == '('):
myStack.append(c)
elif (myStack != []) and (myStack[-1] == '('):
myStack.pop()
else:
Counter += 1
return Counter+len(myStack)

s = str(input())
print(Psp(s))
48 changes: 48 additions & 0 deletions src/balanced_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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.right:
self.right.add(data)
else:
self.right = Node(data)
else:
if self.left:
self.left.add(data)
else:
self.left = Node(data)

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

def binary_tree(el):
base = Node(el[0])
for i in range(1, len(el)):
base.add(el[i])
return base

def balance(tree):
if not tree or ((elevation(tree.right) == elevation(tree.left) or elevation(tree.right)+1 == elevation(tree.left) or elevation(tree.right) == elevation(tree.left)+1)
and balance(tree.right) and balance(tree.left)):
return 1
return 0

def main():
n = list(map(int,input().split()))
n.pop()
tree = binary_tree(n)

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

main()
20 changes: 20 additions & 0 deletions src/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
n= int(input())
arr = input()
str_lst = arr.split()
res = []
num = True
for item in str_lst:
res.append(int(item))

n = len(res)
for i in range (0, n-1):
#print(i)
for j in range(0, n-i-1):
#print(j)
if res[j] > res[j+1]:
num = False
res[j],res[j+1] = res[j+1],res[j]
print(" ".join(map(str, res)))

if num == True:
print(0)
50 changes: 50 additions & 0 deletions src/dynamic_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from math import gcd

def dynamic_version(val, x, r, num, p):
if r - x == 1:
num[val] = p[x]
return

middle = (x + r)//2
dynamic_version(2*val + 1, x, middle, num, p)
dynamic_version(2*val + 2, middle, r, num, p)
num[val] = gcd(num[2*val+1], num[2*val+2])

def calc(val, x, r, it, q_x, q_r):
if q_x <= x and q_r >= r:
return it[val]
if q_x >= r or q_r <= x:
return 0

middle = (x + r)//2
tx = calc(2*val + 1, x, middle, it, q_x, q_r)
tr = calc(2*val + 2, middle, r, it, q_x, q_r)
return gcd(tx, tr)

def up(val, x, r, num, idx, value):
if r - x == 1:
num[val] = value
return
middle = (r+x)//2
if idx < middle:
up(val*2+1, x, middle, num, idx, value)
else:
up(val*2+2, middle, r, num, idx, value)
num[val] = gcd(num[2*val+1], num[2*val+2])

def main():
t = int(input())
num = [0] * (4 * t)
p = list(map(int, input().split()))[:t]
dynamic_version(0, 0, t, num, p)
q = int(input())
ct = []
while q != 0:
t_q, x, r = map(str, input().split())
if t_q == 's':
ct.append(calc(0, 0, t, num, int(x)-1, int(r)))
else:
up(0, 0, t, num, int(x)-1, int(r))
q -= 1
print(*ct)
main()
8 changes: 8 additions & 0 deletions src/fifth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
n = int(input())
m = list(map(int, input().split(" ")))
sort ={}
for i in m:
if i not in sort:
sort[i] = 1

print(len(sort))
40 changes: 40 additions & 0 deletions src/greatest_common_factor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
def g_c_f(b, lft, rght, s_leaf, arr):
if rght - lft == 1:
s_leaf[b] = arr[lft]
return
m = (rght + lft) // 2
g_c_f(2 * b + 1, lft, m, s_leaf, arr)
g_c_f(2 * b + 2, m, rght, s_leaf, arr)
s_leaf[b] = GCF(s_leaf[2 * b + 1], s_leaf[2 * b + 2])

def GCF(p,b):
while b:
p, b = b, p%b
return p

def get_GCF(b, lft, rght, s_leaf, left, right):
if left <= lft and right >= rght:
return s_leaf[b]
if left >= rght or right <= lft:
return 0
m = (rght + lft) // 2
st_right = get_GCF(2 * b + 2, m, rght, s_leaf, left, right)
st_left = get_GCF(2 * b + 1, lft, m, s_leaf, left, right)
return GCF(st_left, st_right)


def main():
n = int(input())
arr = list(map(int, input().split()))[:n]
s_leaf = [0] * 4 * n
g_c_f(0, 0, n, s_leaf, arr)
q = int(input())
p = []

while q != 0:
lft, rght = map(int, input().split())
p.append(get_GCF(0, 0, n, s_leaf, lft - 1, rght))
q -= 1
print(*p)

main()
29 changes: 29 additions & 0 deletions src/hash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def substring_search(S, T):
substring_str = 0
for i in range(T):
substring_str = (substring_str * p + ord(S[i])) % q
return substring_str

def Rabin_Karp_Matcher(str_substring, pattern, x, y):
lst = []
str_substring = hash(y)
pattern_substring = hash(y)
pt = 1
for i in range(x-1):
pt = (pt * p) % q
for i in range(1, x-y+2):
if str_substring == pattern_substring:
lst.append(i-1)
if i < x-y+1:
str_substring = (str_substring - ord(str_substring[i-1]) * pt) * p
str_substring += ord(str_substring[i + y - 1])
str_substring %= q
return lst
string = input()
pattern_string = input()
global p
p = 31
global q
q = 2 ** 31 - 1
A = Rabin_Karp_Matcher(string, pattern_string, len(string), len(pattern_string))
print(' '.join(map(str, A)))
36 changes: 36 additions & 0 deletions src/hash_hash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
def cyclic_shift(c,x,y):
hash = 0
for i in range(len(c)):
hash = (hash*x+ord(c[i]))%y
return hash

def function(c,t,x,y):
if c!=t:
arr = 1
t = t[1:] + t[0]
c_s = cyclic_shift(c,x,y)
t_s = cyclic_shift(t,x,y)
k = 1
for i in range(len(c)-1):
k=(k*x)%y
for i in range(len(t)-1):
if c_s == t_s:
break
else:
t_s = (x*(t_s-ord(t[i])*k) + ord(t[i]))%y
arr += 1
if c_s == t_s:
print(arr)
else:
print(-1)
else:
print(0)

def main():
c = input()
t = input()
x = 30
y = 1e9+6
a = function(c,t,x,y)

main()
16 changes: 16 additions & 0 deletions src/insertion_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

mas = [[int(i) for i in input().split(" ")]for _ in range(int(input()))]


for j in range( len(mas)):
for i in range( len(mas)-1):
if(mas[i][1] < mas[i+1][1]):
mas[i][0],mas[i+1][0] = mas[i+1][0],mas[i][0]
mas[i][1],mas[i+1][1] = mas[i+1][1],mas[i][1]
if(mas[i][1] == mas[i+1][1]):
if(mas[i][0] > mas[i+1][0]):
mas[i][0],mas[i+1][0] = mas[i+1][0],mas[i][0]
mas[i][1],mas[i+1][1] = mas[i+1][1],mas[i][1]
for i in range( len(mas)):

print(mas[i][0], mas[i][1])
35 changes: 35 additions & 0 deletions src/inversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
def invers_fun(num):
n = len(num)
if n <= 1:
return num, 0
mid = n//2
left, left_inv = invers_fun(num[:mid])
right, right_inv = invers_fun(num[mid:])
cout, s = sort(left, right)
m = s + left_inv + right_inv
return cout, m

def sort(left, right):
s = 0
cout = []
i, j = 0, 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
cout.append(left[i])
i += 1
else:
cout.append(right[j])
j += 1
s += len(left) - i
while i < len(left):
cout.append(left[i])
i += 1
while j < len(right):
cout.append(right[j])
j += 1
return cout, s

n = int(input())
num = list(map(int, input().split(" ")))
num, inversions = invers_fun(num)
print(inversions)
23 changes: 23 additions & 0 deletions src/line_period.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def line_period(s):
x = [0] * len(s)
for i in range(1, len(s)):
k = x[i-1]
while (k > 0 and s[k] != s[i]):
k = x[k - 1]
if s[i] == s[k]:
k += 1
x[i] = k
return x

def main():
s = input()
s_t = s + '&' + s
L = line_period(s_t)
lr = L[-1]-L[len(s)-1]
k = len(s)//lr
if s[:lr]*k == s:
print(k)
else:
print(1)

main()
2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
print("Hello world")
print("Hello, " + input() + "!")
35 changes: 35 additions & 0 deletions src/merger_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

def merger(mas, List):
new_list = []
i, j = 0, 0
while i < len(mas) and j < len(List):
if mas[i] < List[j]:
new_list.append(mas[i])
i += 1
else:
new_list.append(List[j])
j += 1
while i < len(mas):
new_list.append(mas[i])
i += 1
while j < len(List):
new_list.append(List[j])
j += 1

return new_list
def merger_sort(mas, index):
if len(mas) < 2:
return mas
else:
mid = len(mas)//2
left = merger_sort(mas[:mid], [index[0],index[0]+mid])
right = merger_sort(mas[mid:],[index[0]+mid, index[1]])

sort = merger(left, right)
print(index[0]+1, index[1], sort[0], sort[-1])
return sort
Size = int(input())
mas = list(map(int, input().split(" ")))

mas = merger_sort(mas,[0, Size])
print(' '.join(map(str, mas)))
Loading