GeezerVison is showing github.com — Fixed syntax errors · code3002/coding-problems@a348932
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: Backtracking/river_sizes.py +8-8Lines changed: 8 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,10 +39,10 @@ def river_sizes(matrix): | 39 | 39 | for i in range(n): | 40 | 40 | for j in range(m): | 41 | 41 | if matrix[i][j] != 0: | 42 | | - # find the river size | | 42 | + # find the river size | 43 | 43 | size = dfs((i, j), matrix) | 44 | 44 |
| 45 | | - # save the river size | | 45 | + # save the river size | 46 | 46 | results.append(size) | 47 | 47 |
| 48 | 48 | return results | @@ -51,29 +51,29 @@ def dfs(coord, matrix): | 51 | 51 | (i, j) = coord | 52 | 52 |
| 53 | 53 | if i < 0 or j < 0: | 54 | | - # invalid position | | 54 | + # invalid position | 55 | 55 | return 0 | 56 | 56 |
| 57 | 57 | n = len(matrix) | 58 | 58 | m = len(matrix[0]) | 59 | 59 |
| 60 | 60 | if i == n or j == m: | 61 | | - # invalid position | | 61 | + # invalid position | 62 | 62 | return 0 | 63 | 63 |
| 64 | 64 | if matrix[i][j] == 0: | 65 | | - # not a river | | 65 | + # not a river | 66 | 66 | return 0 | 67 | 67 |
| 68 | 68 | # update the matrix, the matrix is passed by reference | 69 | 69 | matrix[i][j] = 0 | 70 | | - # this position is part of river | | 70 | + # this position is part of river | 71 | 71 | size = 1 | 72 | 72 |
| 73 | | - # directions: down, left, up, right | | 73 | + # directions: down, left, up, right | 74 | 74 | dirs = [(-1, 0), (0, -1), (1, 0), (0, 1)] | 75 | 75 |
| 76 | | - # check all 4 directions | | 76 | + # check all 4 directions | 77 | 77 | for d in dirs: | 78 | 78 | size += dfs((i + d[0], j + d[1]), matrix) | 79 | 79 |
| |
Collapse file Copy file name to clipboardExpand all lines: Dynamic Programming/climbing_staircase.py +2-5Lines changed: 2 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 | 2, 2 | 16 | 16 |
| 17 | 17 | ========================================= | 18 | | -Dynamic Programing | | 18 | +Dynamic Programing solution. | 19 | 19 | Time Complexity: O(N*S) | 20 | 20 | Space Complexity: O(N) | 21 | | -If steps are only [1, 2], this problem can be solved using Fibonacci algorithm, because ways(n) = ways(n-1) + ways(n-2). | 22 | | - Time Complexity: O(Fib(N)) | 23 | | - Space Complexity: O(1) | 24 | 21 | ''' | 25 | 22 |
| 26 | 23 |
| @@ -36,7 +33,7 @@ def climbing_staircase(steps, height): | 36 | 33 | if s <= height: | 37 | 34 | dp[s - 1] = 1 | 38 | 35 |
| 39 | | - # for each current position look how you can arrive there | | 36 | + # for each position look how you can arrive there | 40 | 37 | for i in range(height): | 41 | 38 | for s in steps: | 42 | 39 | if i - s >= 0: | |
Collapse file Copy file name to clipboardExpand all lines: Dynamic Programming/coin_change.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -56,7 +56,7 @@ def coin_change_2(coins, amount): | 56 | 56 | if len(coins) == 0: | 57 | 57 | return -1 | 58 | 58 |
| 59 | | - max_value = amount + 1 # use this instead of math.inf | | 59 | + max_value = amount + 1 | 60 | 60 | max_coin = min(max_value, max(coins) + 1) | 61 | 61 | dp = [max_value for i in range(max_coin)] | 62 | 62 | dp[0] = 0 | |
Collapse file Copy file name to clipboardExpand all lines: Linked Lists/intersecting_ll.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,7 +23,7 @@ def __init__(self, x, n = None): | 23 | 23 | self.val = x | 24 | 24 | self.next = n | 25 | 25 |
| 26 | | -def findIntersectingNode(ll1, ll2): | | 26 | +def find_intersecting_node(ll1, ll2): | 27 | 27 | # count how many nodes contains the first ll | 28 | 28 | count1 = 0 | 29 | 29 | temp1 = ll1 | @@ -73,4 +73,4 @@ def findIntersectingNode(ll1, ll2): | 73 | 73 | # Correct result => 8 | 74 | 74 | in1 = ListNode(3, ListNode(7, ListNode(8, ListNode(10)))) | 75 | 75 | in2 = ListNode(1, ListNode(8, ListNode(10))) | 76 | | -print(findIntersectingNode(in1, in2).val) | | 76 | +print(find_intersecting_node(in1, in2).val) |
Collapse file Copy file name to clipboardExpand all lines: Lists/find_first_missing_positive.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -61,12 +61,12 @@ def find_first_missing(a): | 61 | 61 |
| 62 | 62 | # Test 2 | 63 | 63 | # Correct result => 2 | 64 | | -test = [3, 4, -1, 1] # 2 | | 64 | +test = [3, 4, -1, 1] | 65 | 65 | print(find_first_missing(test)) | 66 | 66 |
| 67 | 67 | # Test 3 | 68 | 68 | # Correct result => 3 | 69 | | -test = [1, 2, 0] # 3 | | 69 | +test = [1, 2, 0] | 70 | 70 | print(find_first_missing(test)) | 71 | 71 |
| 72 | 72 | # Test 4 | |
Collapse file Copy file name to clipboard +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +''' | | 2 | +Reverse array | | 3 | +
| | 4 | +Reverse an array, in constant space and linear time complexity. | | 5 | +
| | 6 | +Input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | | 7 | +Output: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] | | 8 | +
| | 9 | +========================================= | | 10 | +Reverse the whole array by swapping pair letters in-place (first with last, second with second from the end, etc). | | 11 | + Time Complexity: O(N) | | 12 | + Space Complexity: O(1) | | 13 | +''' | | 14 | + | | 15 | + | | 16 | +############ | | 17 | +# Solution # | | 18 | +############ | | 19 | + | | 20 | +def reverse_arr(arr): | | 21 | + start = 0 | | 22 | + end = len(arr) - 1 | | 23 | + | | 24 | + while start < end: | | 25 | + # reverse the array from the start index to the end index by | | 26 | + # swaping each element with the pair from the other part of the array | | 27 | + swap(arr, start, end) | | 28 | + start += 1 | | 29 | + end -= 1 | | 30 | + | | 31 | + return arr | | 32 | + | | 33 | +def swap(arr, i, j): | | 34 | + # swapping two elements from a same array | | 35 | + temp = arr[i] | | 36 | + arr[i] = arr[j] | | 37 | + arr[j] = temp | | 38 | + | | 39 | + | | 40 | +########### | | 41 | +# Testing # | | 42 | +########### | | 43 | + | | 44 | +# Test 1 | | 45 | +# Correct result => [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] | | 46 | +print(reverse_arr([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])) | | 47 | + | | 48 | +# Test 2 | | 49 | +# Correct result => [5, 4, 3, 2, 1] | | 50 | +print(reverse_arr([1, 2, 3, 4, 5])) |
Collapse file Copy file name to clipboardExpand all lines: Lists/sliding_window_maximum.py +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 11 | 11 | 8 = max(7, 8, 7) | 12 | 12 |
| 13 | 13 | ========================================= | 14 | | -Sliding window solution using deque or linked lists (only need to be able to remove from both sides and to add on both side in constant time) | 15 | | -https://www.geeksforgeeks.org/sliding-window-maximum-maximum-of-all-subarrays-of-size-k/ | | 14 | +Sliding window solution using deque or linked lists | | 15 | +(only needs to be able to remove from both sides and to add on both sides in constant time). | 16 | 16 | Time Complexity: O(N) | 17 | 17 | Space Complexity: O(K) | 18 | 18 | ''' | | 24 | 24 |
| 25 | 25 | from collections import deque | 26 | 26 |
| 27 | | -# using sliding window | 28 | 27 | def max_el_subarrays(arr, k): | 29 | 28 | n = len(arr) | 30 | 29 | if n == 0: | |
Collapse file Copy file name to clipboardExpand all lines: Other/power.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | ========================================= | 7 | 7 | Using divide and conquer approach. | 8 | 8 | Time Complexity: O(LogB) | 9 | | - Space Complexity: O(LogB) , because of recursion call stack | | 9 | + Space Complexity: O(LogB) , because of recursion call stack | 10 | 10 | ''' | 11 | 11 |
| 12 | 12 | ############ | |
Collapse file Copy file name to clipboardExpand all lines: Strings/anagram_indices.py +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | ''' | 2 | | -Anagram indicies | | 2 | +Anagram indices | 3 | 3 |
| 4 | 4 | Given a word and a string S, find all starting indices in S which are anagrams of word. | 5 | 5 | (An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once) | @@ -54,7 +54,7 @@ def is_empty(self): | 54 | 54 | return len(self.__letters) == 0 | 55 | 55 |
| 56 | 56 |
| 57 | | -def anagram_indicies(s, word): | | 57 | +def anagram_indices(s, word): | 58 | 58 | n = len(s) | 59 | 59 | w = len(word) | 60 | 60 | res = [] | @@ -92,4 +92,4 @@ def anagram_indicies(s, word): | 92 | 92 | ########### | 93 | 93 |
| 94 | 94 | # Correct result => [0, 3, 4] | 95 | | -print(anagram_indicies('abxaba', 'ab')) | | 95 | +print(anagram_indices('abxaba', 'ab')) |
Collapse file Copy file name to clipboardExpand all lines: Strings/create_palindrome.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,7 +28,7 @@ def create_palindrome(string): | 28 | 28 |
| 29 | 29 | # find the biggest left palindrome, which starts from the first letter | 30 | 30 | max_left_palindrome = 1 | 31 | | - for i in reversed(range(1, n)): | | 31 | + for i in range(n - 1, 0, -1): # same as reversed(range(1, n)) | 32 | 32 | front = 0 | 33 | 33 | back = i | 34 | 34 | found = True | |
|