Skip to content

Commit af62767

Browse files
author
Мето Трајковски
committed
Added 2 nondeterministic algorithms and changed swap methods
1 parent 1157279 commit af62767

16 files changed

+137
-62
lines changed

Arrays/find_first_missing_positive.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ def find_first_missing_1(a):
3838
break
3939

4040
# swap elements
41-
temp = a[i]
42-
a[i] = a[swap]
43-
a[swap] = temp
41+
a[i], a[swap] = a[swap], a[i]
4442

4543
for i in range(n):
4644
if a[i] - 1 != i:
@@ -107,19 +105,19 @@ def find_first_missing_2(a):
107105
print(find_first_missing_2(list(test)))
108106

109107
# Test 5
110-
# Correct result => 5
108+
# Correct result => 1
111109
test = [-4, -1, -3, -1]
112110
print(find_first_missing_1(list(test)))
113111
print(find_first_missing_2(list(test)))
114112

115113
# Test 6
116-
# Correct result => 6
114+
# Correct result => 3
117115
test = [2, 1, 2, -1, 0, 20]
118116
print(find_first_missing_1(list(test)))
119117
print(find_first_missing_2(list(test)))
120118

121119
# Test 7
122-
# Correct result => 7
120+
# Correct result => 3
123121
test = [1, 2, 5, 5, 1, 2]
124122
print(find_first_missing_1(list(test)))
125123
print(find_first_missing_2(list(test)))

Arrays/k_closest_points.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ def pivoting(arr, left, right, pt):
6363

6464
def swap(arr, i, j):
6565
# swaps two elements in an array
66-
temp = arr[i]
67-
arr[i] = arr[j]
68-
arr[j] = temp
66+
arr[i], arr[j] = arr[j], arr[i]
6967

7068
def sqr_dist(a, b):
7169
# no need from the square root

Arrays/kth_smallest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ def pivoting(arr, left, right):
6767

6868
def swap(arr, i, j):
6969
# swaps two elements in an array
70-
temp = arr[i]
71-
arr[i] = arr[j]
72-
arr[j] = temp
70+
arr[i], arr[j] = arr[j], arr[i]
7371

7472

7573
##############

Arrays/min_swaps.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ def min_swaps(a):
3535
while a[i] - 1 != i:
3636
swap = a[i] - 1
3737
# swap the elements
38-
tmp = a[swap]
39-
a[swap] = a[i]
40-
a[i] = tmp
38+
a[swap], a[i] = a[i], a[swap]
4139

4240
swaps += 1
4341

Arrays/reverse_array.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ def reverse_arr(arr):
3636

3737
def swap(arr, i, j):
3838
# swapping two elements from a same array
39+
arr[i], arr[j] = arr[j], arr[i]
40+
'''same as
3941
temp = arr[i]
4042
arr[i] = arr[j]
4143
arr[j] = temp
44+
'''
4245

4346

4447
###########

Arrays/reverse_ascending_sublists.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,12 @@ def reverse_arr(arr, start, end):
4141
while start < end:
4242
# reverse the array from the start index to the end index by
4343
# swaping each element with the pair from the other part of the array
44-
swap(arr, start, end)
44+
arr[start], arr[end] = arr[end], arr[start]
4545
start += 1
4646
end -= 1
4747

4848
return arr
4949

50-
def swap(arr, i, j):
51-
# swapping two elements from a same array
52-
temp = arr[i]
53-
arr[i] = arr[j]
54-
arr[j] = temp
55-
5650

5751
###########
5852
# Testing #

Arrays/secret_santa.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'''
2+
Secret Santa
3+
4+
Secret Santa is a game in which a group of friends or colleagues exchange Christmas presents anonymously,
5+
each member of the group being assigned another member for whom to provide a small gift.
6+
You're given a list of names, make a random pairs (each participant should have another name as pair).
7+
Return an array with pairs represented as tuples.
8+
9+
Input: ['a', 'b', 'c']
10+
Output: This is a nondeterministic algorithm, more solutions exists, here are 2 possible solutions:
11+
[('a', 'b'), ('b', 'c'), ('c', 'a')], [('a', 'c'), ('c', 'b'), ('b', 'a')]
12+
13+
=========================================
14+
Shuffle the array (this algorithm is explained in shuffle_array.py) and pair the current element
15+
with the next element (neighbouring).
16+
Time Complexity: O(N)
17+
Space Complexity: O(N)
18+
'''
19+
20+
21+
############
22+
# Solution #
23+
############
24+
25+
from random import randint
26+
27+
def secret_santa(names):
28+
# or use shuffle method from random module (from random import shuffle)
29+
shuffle_array(names)
30+
pairs = []
31+
32+
n = len(names)
33+
prev = names[-1] # or names[n - 1]
34+
35+
for curr in names:
36+
pairs.append((prev, curr))
37+
prev = curr
38+
39+
return pairs
40+
41+
def shuffle_array(arr):
42+
n = len(arr)
43+
44+
for i in range(n):
45+
rand = randint(i, n - 1) # or randint(0, i) it's same
46+
arr[i], arr[rand] = arr[rand], arr[i] # swap elements
47+
48+
# the original arr is already changed
49+
return arr
50+
51+
52+
###########
53+
# Testing #
54+
###########
55+
56+
# Test 1
57+
# Correct result => nondeterministic algorithm, many solutions exist
58+
print(secret_santa(['a', 'b', 'c']))

Arrays/shuffle_array.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'''
2+
Shuffle an Array
3+
4+
Given an array (array elements could be of any type/kind),
5+
write a program to generate in-place a random permutation of array elements.
6+
This question is also asked as “shuffle a deck of cards” or “randomize a given array”.
7+
Here shuffle means that every permutation of array element should equally likely.
8+
9+
Input: [1, 2, 3]
10+
Output: This is a nondeterministic algorithm, N! solutions/permutations exists.
11+
In this case 3! = 6. All permutations are a valid solution.
12+
[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]
13+
14+
=========================================
15+
Easy in-place solution, choose an random index/position from I (I is the current position) to N-1
16+
(or choose from 0 to I, it's same), swap the element at random position with the element at I position.
17+
Increase I and continue with the same algorithm.
18+
Note: In Python there is already implemented shuffle (in random module "from random import shuffle") method
19+
which works in similar way. But it's easy to be implemented and I wanted to show how to implement it.
20+
Time Complexity: O(N)
21+
Space Complexity: O(1)
22+
'''
23+
24+
25+
############
26+
# Solution #
27+
############
28+
29+
from random import randint
30+
31+
def shuffle_array(arr):
32+
n = len(arr)
33+
34+
for i in range(n):
35+
rand = randint(i, n - 1) # or randint(0, i) it's same
36+
arr[i], arr[rand] = arr[rand], arr[i] # swap elements
37+
38+
# the original arr is already changed
39+
return arr
40+
41+
###########
42+
# Testing #
43+
###########
44+
45+
# Test 1
46+
# Correct result => One of these: [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]
47+
print(shuffle_array([1, 2, 3]))

Arrays/sort_rgb_array.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ def sort_rgb_array(arr):
3333
for i in range(n):
3434
# swap the element and move the pointer
3535
if arr[i] == 'R':
36-
temp = arr[i]
37-
arr[i] = arr[r]
38-
arr[r] = temp
36+
swap(arr, i, r)
3937
r += 1
4038

4139
# move pointer
@@ -44,9 +42,7 @@ def sort_rgb_array(arr):
4442

4543
# swap the element and move the pointer
4644
if arr[i] == 'G':
47-
temp = arr[i]
48-
arr[i] = arr[g]
49-
arr[g] = temp
45+
swap(arr, i, g)
5046
g += 1
5147

5248
# move pointer
@@ -55,13 +51,15 @@ def sort_rgb_array(arr):
5551

5652
# swap the element and move the pointer
5753
if arr[i] == 'B':
58-
temp = arr[i]
59-
arr[i] = arr[b]
60-
arr[b] = temp
54+
swap(arr, i, b)
6155
b += 1
6256

6357
return arr
6458

59+
def swap(arr, i, j):
60+
# swaps two elements in an array
61+
arr[i], arr[j] = arr[j], arr[i]
62+
6563

6664
##############
6765
# Solution 2 #

Arrays/top_k_frequent_elements.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
Output: [1]
1212
1313
=========================================
14-
Using Min Priority Queue, in each step add an element with its frequency and remove the element with the smallest frequency
15-
if there are more than K elements inside the Priority Queue. This solution isn't much faster than sorting the frequencies.
14+
Using Min Priority Queue, in each step add an element with its frequency and remove the element with the smallest frequency
15+
if there are more than K elements inside the Priority Queue. This solution isn't much faster than sorting the frequencies.
1616
Time Complexity: O(U LogK) , U in this case is the number of unique elements (but all elements from the array could be unique, so because of that U can be equal to N)
1717
Space Complexity: O(N)
18-
Using pivoting, this solution is based on the quick sort algorithm (divide and conquer).
18+
Using pivoting, this solution is based on the quick sort algorithm (divide and conquer).
1919
Same pivoting solution as the nth_smallest.py.
2020
Time Complexity: O(U)
2121
Space Complexity: O(N)
@@ -30,7 +30,7 @@
3030

3131
# priority queue comparator class
3232
# acctualy in this case you don't need a comparator class, because the elements are tuples
33-
# and comparison operator can work with tuples
33+
# and comparison operator can work with tuples
3434
# (The comparison starts with a first element of each tuple. If they do not compare to =,< or > then it proceed to the second element and so on.)
3535
class PQElement:
3636
def __init__(self, el):
@@ -146,9 +146,7 @@ def pivoting(arr, left, right):
146146

147147
def swap(arr, i, j):
148148
# swaps two elements in an array
149-
temp = arr[i]
150-
arr[i] = arr[j]
151-
arr[j] = temp
149+
arr[i], arr[j] = arr[j], arr[i]
152150

153151

154152
###########

0 commit comments

Comments
 (0)