Skip to content

Commit 099688b

Browse files
committed
learn lists..
1 parent 40634cf commit 099688b

File tree

6 files changed

+34
-41
lines changed

6 files changed

+34
-41
lines changed

Algorythms/CoutnBetween.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Find shortest distance what contains sum of elems = target.
2+
target = 7
3+
nums = [2, 3, 1, 2, 4, 3]
4+
if len(nums) == 0:
5+
resultant = 0
6+
start, end = 0, 0
7+
dist, summa = float("inf"), nums[0]
8+
while end <= len(nums) - 1:
9+
if summa < target:
10+
end += 1
11+
if end <= len(nums) - 1:
12+
summa += nums[end]
13+
elif summa >= target:
14+
dist = min(end - start + 1, dist)
15+
summa -= nums[start]
16+
start += 1
17+
resultant = dist if dist != float("inf") else 0

Sort/SortBuble.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
#n^2 Сравнивает пары и менят местами и так по кругу
2-
oldlist = [10, 75, 43, 15, 25, -4, 27]
3-
42
def bubble_sort(mylist):
53
last_item = len(mylist) - 1
64
for z in range(0, last_item):
75
for x in range(0, last_item-z):
86
print(mylist)
97
if mylist[x] > mylist[x+1]:
108
mylist[x], mylist[x+1] = mylist[x+1], mylist[x]
11-
129
return mylist
1310

11+
12+
oldlist = [10, 75, 43, 15, 25, -4, 27]
1413
print("Old List: ", oldlist)
1514
newlist = bubble_sort(oldlist).copy()
1615
print("New List: ", newlist)

Sort/SortMerge.py

+1-17
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,19 @@
11
# Делит на кучу массивов затем собирая назад передвигает наименьшее в начало
22
# принты для наглядности процесса
33
def merge_sort(lst):
4-
print(1)
5-
print(lst)
64
if len(lst) <= 1:
75
return lst
86
middle = len(lst) // 2
97
left_list = lst[:middle]
10-
print(2)
11-
print(left_list)
128
right_list = lst[middle:]
13-
print(3)
14-
print(right_list)
15-
9+
1610
left_list = merge_sort(left_list)
1711
right_list = merge_sort(right_list)
18-
print(4)
19-
print(left_list)
20-
print(5)
21-
print(right_list)
2212

2313
return list(merge(left_list, right_list))
2414

2515

2616
def merge(left_half, right_half):
27-
print(6)
28-
print(left_half)
29-
print(7)
30-
print(right_half)
3117
res = []
3218
while len(left_half) != 0 and len(right_half) != 0:
3319
if left_half[0] < right_half[0]:
@@ -40,8 +26,6 @@ def merge(left_half, right_half):
4026
res += right_half
4127
else:
4228
res += left_half
43-
print(8)
44-
print(res)
4529
return res
4630

4731

Sort/SortQuick.py

-19
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,7 @@ def quick_sort(lst):
88

99
def quick_sort_helper(lst, low, high):
1010
if low < high:
11-
print("#####")
12-
print(lst)
13-
print(low)
14-
print(high)
15-
print("#####")
1611
split_point = partition(lst, low, high)
17-
print("*****")
18-
print(lst)
19-
print(low)
20-
print(high)
21-
print("*****")
2212
quick_sort_helper(lst, low, split_point - 1)
2313
quick_sort_helper(lst, split_point + 1, high)
2414

@@ -28,25 +18,16 @@ def partition(lst, low, high):
2818
left_mark = low + 1
2919
right_mark = high
3020
done = False
31-
print("begin")
32-
print(lst)
33-
print(right_mark)
3421
while not done:
3522
while left_mark <= right_mark and lst[left_mark] <= pivot_value:
3623
left_mark += 1
3724
while right_mark >= left_mark and lst[right_mark] >= pivot_value:
3825
right_mark -= 1
39-
4026
if right_mark < left_mark:
4127
done = True
4228
else:
4329
lst[left_mark], lst[right_mark] = lst[right_mark], lst[left_mark]
44-
print("inside")
45-
print(lst)
4630
lst[low], lst[right_mark] = lst[right_mark], lst[low]
47-
print("end")
48-
print(lst)
49-
print(right_mark)
5031
return right_mark
5132

5233

arrays.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
NewCities = []
66

77
mynumber_list = list(range(0, 10)) # Creates array 0-10
8-
visited = [ [] for _ in range(10)] # Empty array create
8+
visited = [[] for _ in range(10)] # Empty array create
99
graph = {key: [] for key in range(10)} # empty dickt create
10+
new_list = [[None for _ in range(x+1)] for x in range(numRows)] # Creade empty 2d matrix
11+
l[x] = [0] * (i + 1) # Create llist inside list
1012

1113

1214
sys.argv # array of args provided
@@ -18,6 +20,7 @@
1820
cities.remove('Tula') # remove by name
1921
cities.reverse() # Reverse
2022
cities[::-1] # also reverse and can be done with STR!!
23+
cities[:] = cities[::-1] # for cases when need to change inside function and also have change outside
2124
print(cities[-4:-1]) # From -4 includet to -1 not includet
2225
cities.pop(1) # Delete and show deleted(can be saved in var)
2326
NewCities.append(cities.copy()) # Create a COPY of array

strings.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,13 @@
4141
textlookfor = r"@\w+\.\w+"
4242
allresults = re.findall(textlookfor, mytext)
4343

44-
print(allresults)
44+
print(allresults)
45+
46+
# Get similar start
47+
strs = ["flower", "flow", "flight"]
48+
shortest = min(strs, key=len)
49+
for i, ch in enumerate(shortest):
50+
for other in strs:
51+
if other[i] != ch:
52+
print(shortest[:i])
53+
print(shortest)

0 commit comments

Comments
 (0)