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

Модуль 2 #127

Open
wants to merge 4 commits into
base: Kurilo_Natalia
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
10 changes: 10 additions & 0 deletions модуль 2 задание 2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
n = int(input())
Copy link
Owner

Choose a reason for hiding this comment

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

Наташа, добавьте задачи на Контест.

Copy link
Owner

Choose a reason for hiding this comment

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

И еще оставляйте свои комментарии в коде, чтобы я видела, что Вы понимаете, что там написано.

Copy link
Author

Choose a reason for hiding this comment

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

Анастасия Валерьевна, здравствуйте!
Я добавила задания из модуля 5 и 6. И отправила несколько заданий на Контесте.
Надеюсь, всё отправилось без проблем)

pairs = []
for i in range(0, n):
_str = input()
_split = _str.split(' ')
pair = (int(_split[0])), int(_split[1])
pairs.sort(key=(lambda x: x[0]))
pairs.sort(key=lambda x: x[1], reverse=True)
for i in pairs:
print(str(i[0]) + " " + str(i[1]))
34 changes: 34 additions & 0 deletions модуль 2 задание 3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def main():
n = int(input())
a = list(map(int,input().split()))[:n]
merge(a,0,len(a))
print(*a,sep=" ")
def merge(a,start,end):
if end - start > 1:
middle = (start + end) // 2
merge(a,start,middle)
merge(a,middle,end)
left = a[start:middle]
right = a[middle:end]
internal_sort(a,left,right,start)
print(start + 1, end, a[start], a[end-1])
def internal_sort(arr,left,right,start):
j = i = 0
k = start
while i < len(right) and j < len(left):
if left[j] > right[i]:
arr[k] = right[i]
i+=1
else:
arr[k] = left[j]
j+=1
k+=1
while j < len(left):
arr[k] = left[j]
j+=1
k+=1
while i < len(right):
arr[k] = right[i]
i+=1
k+=1
main()
35 changes: 35 additions & 0 deletions модуль 2 задание 4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
def merge(left, right):
merge_list = []
i = j = 0
k = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
merge_list.append(left[i])
i += 1
else:
merge_list.append(right[j])
j += 1
k += len(left) - i
while i < len(left):
merge_list.append(left[i])
i += 1
while j < len(right):
merge_list.append(right[j])
j += 1
return merge_list, k

def MergeSort(A):

N = len(A)
if N <= 1:
return A, 0
middle = N//2
left, left_k = MergeSort(A[0:middle])
right, right_k = MergeSort(A[middle:N])
merge_list, k = merge(left, right)
return merge_list, k + left_k + right_k

N = int(input())
A = list(map(int, input().split(" ")))
A, result_k = MergeSort(A)
print(result_k)
2 changes: 2 additions & 0 deletions модуль 2 задание 5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
n = int(input())
print(len(set(input().split())))
11 changes: 11 additions & 0 deletions модуль 2 задание 6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
n = int(input())
l = (input().split(' '))
m = int(input())
l2 = (input().split(' '))
for item in range(0, m):
l[int(l2[item]) - 1] = int(l[int(l2[item])-1]) - 1
for item in l:
if int(item) < 0:
print('yes')
else:
print('no')
34 changes: 34 additions & 0 deletions модуль 2 задание 7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
numbers = []
n = int(input())
for i in range(n):
numbers.append(input())

print("Initial array:")
print(', '.join(numbers))

count=len(numbers[0])
Phase = 0
rang = 10
for i in range(count-1,-1,-1):
Phase+=1
print('**********')
print(f'Phase {Phase}')
bucket = [[] for _ in range(rang)]
for j in range (len(numbers)):
bucket [ord(numbers[j][i]) - ord('0')].append(numbers[j])
for j in range(rang):
if len(bucket[j])==0:
print(f'Bucket {j}: empty')
else:
print("Bucket "+str(j)+": ", end='')
for now in range(len(bucket[j])-1):
print(bucket[j][now], end=', ')
print(bucket[j][len(bucket[j])-1])
p = 0
for j in range (rang):
for k in range (len(bucket[j])):
numbers[p]= bucket[j][k]
p+=1
print('**********')
print("Sorted array:")
print(', '.join(numbers))
22 changes: 22 additions & 0 deletions модуль 3 задание 1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def Preph(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

def main():
s=input()
t=input()
s_t=t+"&"+s
p=Preph(s_t)
for i in range(len(p)):
if p[i]==len(t):
print(i-2*len(t),end=" ")

main()
36 changes: 36 additions & 0 deletions модуль 3 задание 2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
def get_hash(S,x,P):
hS = 0
for i in range(len(S)):
hS = (hS*x + ord(S[i]))%P
return hS

def func(S,t,x,P):
if S != t:
k = 1
t = t[1:] + t[0]
hS = get_hash(S, x, P)
ht = get_hash(t, x, P)
xt = 1
for i in range(len(S)-1):
xt = (xt * x) % P
for i in range(len(t)-1):
if hS == ht:
break
else:
ht=(x*(ht-ord(t[i])*xt) + ord(t[i]))%P
k += 1
if hS == ht:
print(k)
else:
print(-1)
else:
print(0)

def main():
S=input()
t=input()
x = 26
P = 10 ** 9 + 7
func(S, t, x, P)

main()
21 changes: 21 additions & 0 deletions модуль 3 задание 3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def prefix(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

s = input()
s_t = s+"$"+s
p = prefix(s_t)
p0=p[-1]-p[len(s)-1]
a = len(s)//p0
if s[:p0]*a == s:
print(a)
else:
print(1)
17 changes: 17 additions & 0 deletions модуль 3 задание 4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def prefix(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

s = input()
s_t = s+"$"+s
a = prefix(s_t)
p0=a[-1]-a[len(s)-1]
print(p0)
12 changes: 12 additions & 0 deletions модуль 4 задание 1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
s = input()
stack = []
k=0
for i in s:
if i ==")":
if not stack or stack[-1]!="(":
k+=1
continue
stack.pop()
else:
stack.append(i)
print(k+len(stack))
20 changes: 20 additions & 0 deletions модуль 4 задание 2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
N=input()
Railways_A=list(map(int, input().split()))
Railways_B=[]
stack=[]
a=1
for i in Railways_A:
if i!=a:
stack.append(i)
continue
Railways_B.append(i)
a+=1
for j in range(len(stack)):
if stack[-1] == a:
Railways_B.append(a)
stack.pop()
a += 1
if Railways_B[-1] == int(N):
print("YES")
else:
print("NO")
48 changes: 48 additions & 0 deletions модуль 5 задание 1.txt
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, 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])
return root


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


main()