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

Dmitriy_Malahov #23

Open
wants to merge 10 commits into
base: Dmitrij_Malahov
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
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
"python.linting.pylintEnabled": false,
"python.linting.mypyEnabled": true,
"python.linting.enabled": true,
"python.pythonPath": "C:\\Users\\dmitr\\AppData\\Local\\Programs\\Python\\Python39\\python.exe",
}
3 changes: 2 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
print("Hello world")
a,b=(int(s) for s in input().split())
print(a+b)
19 changes: 19 additions & 0 deletions src/module2_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
n = int(input())
arr = input()
lst = arr.split(' ')
res = []
s = True
for item in lst:
res.append(int(item))
n = len(res)
for i in range(0,n-1):
for j in range(0,n-1-i):
if res[j] > res[j+1]:
s = False
temp = res[j+1]
res[j+1] = res[j]
res[j] = temp
print(" ".join(map(str, res)))

if s == True:
print(0)
18 changes: 18 additions & 0 deletions src/module2_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def sort_pair(A, n):
for i in range(1, n):
for j in range(0, n-i):
if A[j][1] < A[j+1][1]:
A[j],A[j+1] = A[j+1],A[j]
elif A[j][1] == A[j+1][1] and A[j][0] > A[j+1][0]:
A[j],A[j+1] = A[j+1],A[j]
for i in range(0, n):
print(" ".join(map(str,A[i])))

D = int(input())
A = [0]*D
for i in range(0, D):
A[i] = input()
A[i] = A[i].split(" ")
A[i] = list(map(int,A[i]))

sort_pair(A, D)
37 changes: 37 additions & 0 deletions src/module2_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
def merge(Arr, List):
new_list = []
d, m = 0, 0
while d < len(Arr) and m < len(List):
if Arr[d] < List[m]:
new_list.append(Arr[d])
d += 1
else:
new_list.append(List[m])
m += 1
while d < len(Arr):
new_list.append(Arr[d])
d += 1
while m < len(List):
new_list.append(List[m])
m += 1

return new_list

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

sort = merge(left, right)
print(index[0]+1, index[1], sort[0], sort[-1])
return sort


Quantity = int(input())
Arr = list(map(int, input().split()))

Arr = mergeSort(Arr,[0, Quantity])
print(' '.join(map(str, Arr)))
45 changes: 45 additions & 0 deletions src/module2_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
def Sort(Arr, Num):
List = [0]*Num
return merge_sort(Arr, List, 0, Num-1)

def merge_sort(Arr, List, left, right):
Meter = 0
if left < right:
mid = (left + right)//2
Meter += merge_sort(Arr, List, left, mid)
Meter += merge_sort(Arr, List, mid + 1, right)
Meter += Merge(Arr, List, left, mid, right)
return Meter

def Merge(Arr, List, left, mid, right):
i = left
j = mid + 1
u = left
Meter = 0
while i <= mid and j <= right:
if Arr[i] <= Arr[j]:
List[u] = Arr[i]
u += 1
i += 1
else:
List[u] = Arr[j]
Meter += (mid-i + 1)
u += 1
j += 1
while i <= mid:
List[u] = Arr[i]
u += 1
i += 1
while j <= right:
List[u] = Arr[j]
u += 1
j += 1
for now in range(left, right + 1):
Arr[now] = List[now]
return Meter

Quantity = int(input())
Arr = list(map(int, input().split()))
Num = len(Arr)
Arr = Sort(Arr, Num)
print(Arr)
8 changes: 8 additions & 0 deletions src/module2_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
d = 1
m = int(input())
numbers = list(map(int, input().split(maxsplit = m)))
numbers.sort()
for c in range(1, m):
if numbers[c-1] != numbers[c]:
d= d+1
print(d)
15 changes: 15 additions & 0 deletions src/module2_6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
quantity_product = int(input())
product = list(map(int, input().split()))

quantily_purpose = int(input())
purpose = list(map(int,input().split()))

calculate = [0] * (quantity_product+1)
for now in purpose:
calculate[now] += 1

for i in range(quantity_product):
if product[i] < calculate[i+1]:
print("yes")
else:
print("no")
26 changes: 26 additions & 0 deletions src/module2_7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def store(D, k):
digit = len(D[0])
for c in range(digit):
mass = [[] for f in range(10)]
for item in D:
index = int(item)//10**c % 10
mass[index].append(item)
print("Phase", c+1)
D = []
for c in range(10):
print("Bucket ", c ,":", sep = "", end=" ")
print(", ".join(mass[c]) if len(mass[c])>0 else "empty")
D += mass[c]
print("**********")
return D

k = int(input())
D = []
for i in range(k):
D.append(input())
print("Initial array:")
print(", ".join(D))
print("**********")
D = store(D, k)
print("Sorted array:")
print(", ".join(D))
30 changes: 30 additions & 0 deletions src/module3_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def hash(A, x):
hash_str = 0
for i in range(x):
hash_str = (hash_str * p + ord(A[i])) % q
return hash_str

def rabin_karp(str, sub_str, x, m):
lst = []
str_hash = hash(str, m)
sub_str_hash = hash(sub_str, m)
pt = 1
for i in range(m-1):
pt = (pt * p) % q
for i in range(1, x-m+2):
if str_hash == sub_str_hash:
lst.append(i-1)
if i < x-m+1:
str_hash = (str_hash - ord(str[i-1]) * pt) * p
str_hash += ord(str[i + m - 1])
str_hash %= q
return lst

string = input()
sub_string = input()
global p
p = 31
global q
q = 2 ** 31 - 1
A = rabin_karp(string, sub_string, len(string), len(sub_string))
print(' '.join(map(str, A)))
40 changes: 40 additions & 0 deletions src/module3_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
def shift(string_two, string_one, q):

if string_one == string_two:
return 0

string_one += string_one

d = 11
m = 1
s1hash = 0
for i in string_two[::-1]:
s1hash += ord(i) * m
m *= d
s1hash %= q
m %= q

m = 1
hash = 0
for i in string_one[:len(string_two)][::-1]:
hash += m * ord(i)
m *= d
hash %= q
m %= q

hashtag = 1
for i in range(len(string_two) - 1):
hashtag *= d
hashtag %= q
for i in range(1, len(string_one) - len(string_two) + 1):
hashnew = ((hash % q - ord(string_one[i - 1]) * hashtag % q) * d % q + ord(string_one[i + len(string_two) - 1])) % q
if hashnew == s1hash:
return i
hash = hashnew

return -1

string_one = input()
string_two = input()
q = 2 ** 31 - 1
print(shift(string_one, string_two, q))
27 changes: 27 additions & 0 deletions src/module3_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def pref_func(s):
length = len(s)
Meter = [0] * length
Meter[0] = 0
for i in range (length - 1):
j = Meter[i]
while (j > 0) and (s[i + 1] != s[j]):
j = Meter[j - 1]
if (s[i + 1] == s[j]):
Meter[i+1] = j + 1
else:
Meter[i + 1] = 0
return Meter

text = str(input())
s = list(text)

length = len(s)
pref = pref_func(s)

for i in range(length - 1, length):
result = length - pref[i]

if (length % result == 0):
print(length // result)
else:
print('1')
19 changes: 19 additions & 0 deletions src/module3_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def pref_func(s):
length = len(s)
Meter = [0] * length
Meter[0] = 0
for i in range (length - 1):
j = Meter[i]
while (j > 0) and (s[i + 1] != s[j]):
j = Meter[j - 1]
if (s[i + 1] == s[j]):
Meter[i+1] = j + 1
else:
Meter[i + 1] = 0
return Meter
text = str(input())
s = list(text)

length = len(s)
pref = pref_func(s)
print(length - pref[-1])
14 changes: 14 additions & 0 deletions src/module4_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def PSP(s):
Meter = 0
new_list = []
for i in s:
if (i == '('):
new_list.append(i)
elif (new_list != []) and (new_list[-1] == '('):
new_list.pop()
else:
Meter += 1
return Meter+len(new_list)

s = str(input())
print(PSP(s))
24 changes: 24 additions & 0 deletions src/module4_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
length = int(input())
traject_a = list(map(int,input().split()))
traject_b = []
stack = []
test = sorted(traject_a)
for i in range(length):
if (stack == []) or (stack[-1] > traject_a[0]):
stack.append(traject_a[0])
traject_a.pop(0)
else:
while (stack != [] and stack[-1] < traject_a[0]):
traject_b.append(stack[-1])
stack.pop(-1)
if (stack == []) or (stack[-1] > traject_a[0]):
stack.append(traject_a[0])
traject_a.pop(0)
if stack != []:
for i in range(len(stack)):
traject_b.append(stack[-1])
stack.pop(-1)
if traject_b == test:
print("YES")
else:
print("NO")
13 changes: 13 additions & 0 deletions src/module4_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
length = int(input())
x = list(zip(list(map(int, input().split())), range(length)))
stack = []
result = [0]*length
for i in reversed(x):
while (stack != []) and (stack[-1][0] >= i[0]):
stack.pop()
if stack == []:
result[i[1]] = -1
else:
result[i[1]] = stack[-1][1]
stack.append(i)
print(' '.join(map(str, result)))
Loading