GeezerVison is showing github.com — Added 2 nondeterministic algorithms and changed swap methods · Pranshu-Raj/coding-problems@af62767
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Arrays/find_first_missing_positive.py +4-6Lines changed: 4 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,9 +38,7 @@ def find_first_missing_1(a): | 38 | 38 | break | 39 | 39 |
| 40 | 40 | # swap elements | 41 | | - temp = a[i] | 42 | | - a[i] = a[swap] | 43 | | - a[swap] = temp | | 41 | + a[i], a[swap] = a[swap], a[i] | 44 | 42 |
| 45 | 43 | for i in range(n): | 46 | 44 | if a[i] - 1 != i: | @@ -107,19 +105,19 @@ def find_first_missing_2(a): | 107 | 105 | print(find_first_missing_2(list(test))) | 108 | 106 |
| 109 | 107 | # Test 5 | 110 | | -# Correct result => 5 | | 108 | +# Correct result => 1 | 111 | 109 | test = [-4, -1, -3, -1] | 112 | 110 | print(find_first_missing_1(list(test))) | 113 | 111 | print(find_first_missing_2(list(test))) | 114 | 112 |
| 115 | 113 | # Test 6 | 116 | | -# Correct result => 6 | | 114 | +# Correct result => 3 | 117 | 115 | test = [2, 1, 2, -1, 0, 20] | 118 | 116 | print(find_first_missing_1(list(test))) | 119 | 117 | print(find_first_missing_2(list(test))) | 120 | 118 |
| 121 | 119 | # Test 7 | 122 | | -# Correct result => 7 | | 120 | +# Correct result => 3 | 123 | 121 | test = [1, 2, 5, 5, 1, 2] | 124 | 122 | print(find_first_missing_1(list(test))) | 125 | 123 | print(find_first_missing_2(list(test))) | |
Collapse file Copy file name to clipboardExpand all lines: Arrays/k_closest_points.py +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -63,9 +63,7 @@ def pivoting(arr, left, right, pt): | 63 | 63 |
| 64 | 64 | def swap(arr, i, j): | 65 | 65 | # 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] | 69 | 67 |
| 70 | 68 | def sqr_dist(a, b): | 71 | 69 | # no need from the square root | |
Collapse file Copy file name to clipboardExpand all lines: Arrays/kth_smallest.py +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -67,9 +67,7 @@ def pivoting(arr, left, right): | 67 | 67 |
| 68 | 68 | def swap(arr, i, j): | 69 | 69 | # 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] | 73 | 71 |
| 74 | 72 |
| 75 | 73 | ############## | |
Collapse file Copy file name to clipboardExpand all lines: Arrays/min_swaps.py +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,9 +35,7 @@ def min_swaps(a): | 35 | 35 | while a[i] - 1 != i: | 36 | 36 | swap = a[i] - 1 | 37 | 37 | # 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] | 41 | 39 |
| 42 | 40 | swaps += 1 | 43 | 41 |
| |
Collapse file Copy file name to clipboardExpand all lines: Arrays/reverse_array.py +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,9 +36,12 @@ def reverse_arr(arr): | 36 | 36 |
| 37 | 37 | def swap(arr, i, j): | 38 | 38 | # swapping two elements from a same array | | 39 | + arr[i], arr[j] = arr[j], arr[i] | | 40 | + '''same as | 39 | 41 | temp = arr[i] | 40 | 42 | arr[i] = arr[j] | 41 | 43 | arr[j] = temp | | 44 | + ''' | 42 | 45 |
| 43 | 46 |
| 44 | 47 | ########### | |
Collapse file Copy file name to clipboardExpand all lines: Arrays/reverse_ascending_sublists.py +1-7Lines changed: 1 addition & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,18 +41,12 @@ def reverse_arr(arr, start, end): | 41 | 41 | while start < end: | 42 | 42 | # reverse the array from the start index to the end index by | 43 | 43 | # 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] | 45 | 45 | start += 1 | 46 | 46 | end -= 1 | 47 | 47 |
| 48 | 48 | return arr | 49 | 49 |
| 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 | | - | 56 | 50 |
| 57 | 51 | ########### | 58 | 52 | # Testing # | |
Collapse file Copy file name to clipboard +58Lines changed: 58 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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'])) |
Collapse file Copy file name to clipboard +47Lines changed: 47 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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])) |
Collapse file Copy file name to clipboardExpand all lines: Arrays/sort_rgb_array.py +7-9Lines changed: 7 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,9 +33,7 @@ def sort_rgb_array(arr): | 33 | 33 | for i in range(n): | 34 | 34 | # swap the element and move the pointer | 35 | 35 | if arr[i] == 'R': | 36 | | - temp = arr[i] | 37 | | - arr[i] = arr[r] | 38 | | - arr[r] = temp | | 36 | + swap(arr, i, r) | 39 | 37 | r += 1 | 40 | 38 |
| 41 | 39 | # move pointer | @@ -44,9 +42,7 @@ def sort_rgb_array(arr): | 44 | 42 |
| 45 | 43 | # swap the element and move the pointer | 46 | 44 | if arr[i] == 'G': | 47 | | - temp = arr[i] | 48 | | - arr[i] = arr[g] | 49 | | - arr[g] = temp | | 45 | + swap(arr, i, g) | 50 | 46 | g += 1 | 51 | 47 |
| 52 | 48 | # move pointer | @@ -55,13 +51,15 @@ def sort_rgb_array(arr): | 55 | 51 |
| 56 | 52 | # swap the element and move the pointer | 57 | 53 | if arr[i] == 'B': | 58 | | - temp = arr[i] | 59 | | - arr[i] = arr[b] | 60 | | - arr[b] = temp | | 54 | + swap(arr, i, b) | 61 | 55 | b += 1 | 62 | 56 |
| 63 | 57 | return arr | 64 | 58 |
| | 59 | +def swap(arr, i, j): | | 60 | + # swaps two elements in an array | | 61 | + arr[i], arr[j] = arr[j], arr[i] | | 62 | + | 65 | 63 |
| 66 | 64 | ############## | 67 | 65 | # Solution 2 # | |
Collapse file Copy file name to clipboardExpand all lines: Arrays/top_k_frequent_elements.py +5-7Lines changed: 5 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 11 | 11 | Output: [1] | 12 | 12 |
| 13 | 13 | ========================================= | 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. | 16 | 16 | 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) | 17 | 17 | 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). | 19 | 19 | Same pivoting solution as the nth_smallest.py. | 20 | 20 | Time Complexity: O(U) | 21 | 21 | Space Complexity: O(N) | | 30 | 30 |
| 31 | 31 | # priority queue comparator class | 32 | 32 | # 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 | 34 | 34 | # (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.) | 35 | 35 | class PQElement: | 36 | 36 | def __init__(self, el): | @@ -146,9 +146,7 @@ def pivoting(arr, left, right): | 146 | 146 |
| 147 | 147 | def swap(arr, i, j): | 148 | 148 | # 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] | 152 | 150 |
| 153 | 151 |
| 154 | 152 | ########### | |
|