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

Lapshin alexander #137

Open
wants to merge 5 commits into
base: Lapshin_Alexander
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
12 changes: 12 additions & 0 deletions Contest module 2/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
a=int(input())
s=input().split()[:a]
k=[int(x) for x in s]
m=0
for i in range(a-1):
for j in range(a-1-i):
if k[j+1]<k[j]:
m=1
k[j],k[j+1]=k[j+1],k[j]
print(*k,sep=" ")
if m==0:
print(0)
9 changes: 9 additions & 0 deletions Contest module 2/2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
a=int(input())

m=[]
for i in range(a):
k, v=map(int, input().split())
m.append([k,v])
m.sort(key=lambda x: x[0])
m.sort(key=lambda x: x[1], reverse = True)
[print(i[0], i[1]) for i in m]
38 changes: 38 additions & 0 deletions Contest module 2/3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
def merge_sort(nums, start, end):
if len(nums) > 1:
middle = len(nums) // 2
left = nums[:middle]
right = nums[middle:]
merge_sort(left, start, start + middle - 1)
merge_sort(right, start + middle, end)

i = j = k = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
nums[k] = left[i]
i+=1
else:
nums[k] = right[j]
j+=1
k+=1

while i < len(left):
nums[k] = left[i]
i+=1
k+=1

while j < len(right):
nums[k] = right[j]
j+=1
k+=1

print(start, end, nums[0], nums[-1])

N=int(input())

p = input().split()[:N]
p = [int(x) for x in p]

merge_sort(p, 1, len(p))

print(*p)
35 changes: 35 additions & 0 deletions Contest module 2/4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
def merge_sort(arr):
global count
if len(arr) > 1:
middle = len(arr) // 2
half1 = arr[:middle]
half2 = arr[middle:]
merge_sort(half1)
merge_sort(half2)
i = j = k = 0
while i < len(half1) and j < len(half2):
if half1[i] > half2[j]:
arr[k] = half2[j]
j += 1
count += len(half1) - i
else:
arr[k] = half1[i]
i += 1
k += 1

while i < len(half1):
arr[k] = half1[i]
k += 1
i += 1

while j < len(half2):
arr[k] = half2[j]
k += 1
j += 1
return arr

n = int(input())
arr = list(map(int, input().split()))[:n]
count = 0
merge_sort(arr)
print(count)
2 changes: 2 additions & 0 deletions Contest module 2/5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a=int(input())
print(len(set(input().split()[:a])))
12 changes: 12 additions & 0 deletions Contest module 2/6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
a= int(input())
c = input().split()[:a]
c = [int(x) for x in c]
b = int(input())
n= input().split()[:b]
n = [int(x) for x in n]

for i in range(0,a):
if c[i] < n.count(i + 1):
print('yes')
else:
print('no')
25 changes: 25 additions & 0 deletions Contest module 2/7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
n = int(input())
nums = []
inteng = 10
for i in range(n):
nums.append(input())
print('Initial array:')
print(', '.join(nums))
for i in range(len(str(max(nums)))):
print('**********')
print('Phase', i+1)
buck = [[] for k in range(inteng)]
for k in nums:
f = int(k) // 10**i % 10
buck[f].append(k)
nums = []
for k in range(inteng):
nums = nums+buck[k]
print('Bucket', k, end=": ")
if len(buck[k]) == 0:
print('empty')
else:
print(', '.join(map(str, buck[k])))
print('**********')
print('Sorted array:')
print(', '.join(map(str, nums)))
19 changes: 19 additions & 0 deletions Contest module 3/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def pref(s):
p=[0]*len(s)
for i in range(len(s)-1):
k=p[i]
while k>0 and s[i+1] != s[k]:
k = p[k-1]
if s[i+1] == s[k]:
p[i+1] = k+1
else:
p[i+1] = 0
return p

S=input()
T=input()
a=T+"$"+S
p=pref(a)
for i in range(len(p)):
if p[i] == len(T):
print(i-2*len(T), end=" ")
32 changes: 32 additions & 0 deletions Contest module 3/2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def hash(s, x, p):
hash_s = 0
for i in range(len(s)):
hash_s = (hash_s * x + ord(s[i])) % p
return hash_s

def rolling_hash(s, t ,x ,p):
if s != t:
m = 1
t = t[1:] + t[0]
hash_s = hash(s, x ,p)
hash_t = hash(t, x ,p)
x_t = 1
for i in range(len(s) - 1):
x_t = (x_t * x) % p
for i in range(len(t) - 1):
if hash_s == hash_t:
break
else:
hash_t = (x * (hash_t - ord(t[i]) * x_t) + ord(t[i])) % p
m += 1
[print(m) if hash_s == hash_t else print(-1)]
else:
print(0)

def main():
S = input()
T = input()
x = 26
p = 1e9 + 7
rolling_hash(S, T, x, p)
main()
28 changes: 28 additions & 0 deletions Contest module 3/3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def fun(s):
p = [0]*len(s)
for i in range(len(s)-1):
j = p[i]
while j > 0 and s[j] != s[i+1]:
j = p[j-1]
if s[j] == s[i+1]:
p[i+1] = j+1
else:
p[i+1] = 0
return p

def funk(s, t, m):
if 1 in t:
for i in range(len(s)):
if t[i] == 1:
m.append(i)
k = s[:m[-1]]
else:
k = s
[print(len(s)// len(k)) if s == k*(len(s)//len(k)) else print(1)]

S = input()
T = fun(S)

mass = []

funk(S, T, mass)
17 changes: 17 additions & 0 deletions Contest module 3/4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def min_len(t):
kek = 1
p = [0]*len(t)
for i in range(len(t)-1):
j = p[i]
while t[j] != t[i+1] and j > 0:
j = p[j-1]
if t[j] == t[i+1]:
p[i+1] = j+1
else:
p[i+1] = 0
if p[i+1] < 2:
kek = i+1
return kek

T = input()
print(min_len(T))
12 changes: 12 additions & 0 deletions Contest module 4/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
n = input() #переменная принимающая значения
stack = [] # добавляем пустой стек
k = 0 # создаем счетчик
for i in n:
if i == '(':
stack.append(i) #добавляем в стек
elif i == ')':
if stack:
stack.pop() #очищаем стек
else:
k += 1
print(k+len(stack)) #выводим
21 changes: 21 additions & 0 deletions Contest module 4/2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from collections import deque

N = int(input()) # вводим количество вагонов в поезде
A = deque(list(map(int, input().split(maxsplit=N)))) #создаем очередь с установленым размером N
B = []
Toopic = []
kolvo = list(range(1, N+1)) #кол-во вагонов


while A:
if not Toopic or Toopic[-1] > A[0]:
Toopic.append(A.popleft()) #добавляет в Toopic первый удаленный элемент из A
if A and Toopic[-1] < A[0]:
B.append(Toopic.pop()) #добавляет в B последний удаленный элемент из Toopic

while Toopic:
B.append(Toopic.pop()) #добавляет в B последний удаленный элемент из Toopic
if B == kolvo:
print("YES")
else:
print("NO")
11 changes: 11 additions & 0 deletions Contest module 4/3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
N = int(input()) # количество элементов в массиве
n = list(map(int, input().split(maxsplit = N))) #создаем очередь с установленым размером N
stack = []
index = [0]*N
for i in range(N-1, -1, -1):
while stack and n[stack[-1]] >= n[i]:
stack.pop() #удаляем последний элемент в стеке
index[i] = stack[-1] if stack else -1 # если есть ближайшее справа строго меньшего числа,то выводим,иначе -1
stack.append(i) #добавляем в стек

print(*index)
21 changes: 21 additions & 0 deletions Contest module 4/4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def min(N, K, arr): #вызываем функцию
stack = [] #создаем пустой стек
for i in range(K):
while stack and arr[i] <= arr[stack[-1]]:
stack.pop() #удаляем последний элемент
stack.append(i)
for i in range(K, N):
print(arr[stack[0]]) #выводим элемент списка arr
while stack and arr[i] <= arr[stack[-1]]:
stack.pop()
while stack and i - K >= stack[0]:
stack.pop(0)
stack.append(i) #добавляем в стек
print(arr[stack[0]])

def main():
N, K = map(int, input().split())
arr = list(map(int, input().split()))
min(N, K, arr)

main()
44 changes: 44 additions & 0 deletions Contest module 5/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None

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

def find_forcs(self):
if self.left:
self.left.find_forcs()
if (self.left and not self.right) or (not self.left and self.right):
print(self.data,)
if self.right:
self.right.find_forcs()


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Мда... как же у Вас код сработал, если у Вас ошибки в 25 и 33 строке?

return root


def main():
elements = list(map(int, input().split()))
elements =elements[:-1]
tree = build_tree(elements)
tree.find_forcs()


main()
Loading