GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fixed syntax errors · onowdev/coding-problems@a348932

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Backtracking
    • river_sizes.py
  • Dynamic Programming
    • climbing_staircase.py
    • coin_change.py
  • Linked Lists
    • intersecting_ll.py
  • Lists
    • find_first_missing_positive.py
    • reverse_array.py
    • sliding_window_maximum.py
  • Other
    • power.py
  • Strings
    • anagram_indices.py
    • create_palindrome.py
    • longest_substring_with_k_distinct_characters.py
    • reverse_string.py
    • reverse_words_in_sentence.py
    • swap_first_and_last_word.py
  • Trees
    • valid_bst.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Backtracking/river_sizes.py‎

Copy file name to clipboardExpand all lines: Backtracking/river_sizes.py
+8-8Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ def river_sizes(matrix):
3939
for i in range(n):
4040
for j in range(m):
4141
if matrix[i][j] != 0:
42-
# find the river size
42+
# find the river size
4343
size = dfs((i, j), matrix)
4444

45-
# save the river size
45+
# save the river size
4646
results.append(size)
4747

4848
return results
@@ -51,29 +51,29 @@ def dfs(coord, matrix):
5151
(i, j) = coord
5252

5353
if i < 0 or j < 0:
54-
# invalid position
54+
# invalid position
5555
return 0
5656

5757
n = len(matrix)
5858
m = len(matrix[0])
5959

6060
if i == n or j == m:
61-
# invalid position
61+
# invalid position
6262
return 0
6363

6464
if matrix[i][j] == 0:
65-
# not a river
65+
# not a river
6666
return 0
6767

6868
# update the matrix, the matrix is passed by reference
6969
matrix[i][j] = 0
70-
# this position is part of river
70+
# this position is part of river
7171
size = 1
7272

73-
# directions: down, left, up, right
73+
# directions: down, left, up, right
7474
dirs = [(-1, 0), (0, -1), (1, 0), (0, 1)]
7575

76-
# check all 4 directions
76+
# check all 4 directions
7777
for d in dirs:
7878
size += dfs((i + d[0], j + d[1]), matrix)
7979

Collapse file

‎Dynamic Programming/climbing_staircase.py‎

Copy file name to clipboardExpand all lines: Dynamic Programming/climbing_staircase.py
+2-5Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@
1515
2, 2
1616
1717
=========================================
18-
Dynamic Programing
18+
Dynamic Programing solution.
1919
Time Complexity: O(N*S)
2020
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)
2421
'''
2522

2623

@@ -36,7 +33,7 @@ def climbing_staircase(steps, height):
3633
if s <= height:
3734
dp[s - 1] = 1
3835

39-
# for each current position look how you can arrive there
36+
# for each position look how you can arrive there
4037
for i in range(height):
4138
for s in steps:
4239
if i - s >= 0:
Collapse file

‎Dynamic Programming/coin_change.py‎

Copy file name to clipboardExpand all lines: Dynamic Programming/coin_change.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def coin_change_2(coins, amount):
5656
if len(coins) == 0:
5757
return -1
5858

59-
max_value = amount + 1 # use this instead of math.inf
59+
max_value = amount + 1
6060
max_coin = min(max_value, max(coins) + 1)
6161
dp = [max_value for i in range(max_coin)]
6262
dp[0] = 0
Collapse file

‎Linked Lists/intersecting_ll.py‎

Copy file name to clipboardExpand all lines: Linked Lists/intersecting_ll.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, x, n = None):
2323
self.val = x
2424
self.next = n
2525

26-
def findIntersectingNode(ll1, ll2):
26+
def find_intersecting_node(ll1, ll2):
2727
# count how many nodes contains the first ll
2828
count1 = 0
2929
temp1 = ll1
@@ -73,4 +73,4 @@ def findIntersectingNode(ll1, ll2):
7373
# Correct result => 8
7474
in1 = ListNode(3, ListNode(7, ListNode(8, ListNode(10))))
7575
in2 = ListNode(1, ListNode(8, ListNode(10)))
76-
print(findIntersectingNode(in1, in2).val)
76+
print(find_intersecting_node(in1, in2).val)
Collapse file

‎Lists/find_first_missing_positive.py‎

Copy file name to clipboardExpand all lines: Lists/find_first_missing_positive.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ def find_first_missing(a):
6161

6262
# Test 2
6363
# Correct result => 2
64-
test = [3, 4, -1, 1] # 2
64+
test = [3, 4, -1, 1]
6565
print(find_first_missing(test))
6666

6767
# Test 3
6868
# Correct result => 3
69-
test = [1, 2, 0] # 3
69+
test = [1, 2, 0]
7070
print(find_first_missing(test))
7171

7272
# Test 4
Collapse file

‎Lists/reverse_array.py‎

Copy file name to clipboard
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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

‎Lists/sliding_window_maximum.py‎

Copy file name to clipboardExpand all lines: Lists/sliding_window_maximum.py
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
8 = max(7, 8, 7)
1212
1313
=========================================
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).
1616
Time Complexity: O(N)
1717
Space Complexity: O(K)
1818
'''
@@ -24,7 +24,6 @@
2424

2525
from collections import deque
2626

27-
# using sliding window
2827
def max_el_subarrays(arr, k):
2928
n = len(arr)
3029
if n == 0:
Collapse file

‎Other/power.py‎

Copy file name to clipboardExpand all lines: Other/power.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
=========================================
77
Using divide and conquer approach.
88
Time Complexity: O(LogB)
9-
Space Complexity: O(LogB) , because of recursion call stack
9+
Space Complexity: O(LogB) , because of recursion call stack
1010
'''
1111

1212
############
Collapse file

‎Strings/anagram_indicies.py‎ ‎Strings/anagram_indices.py‎Strings/anagram_indicies.py renamed to Strings/anagram_indices.py

Copy file name to clipboardExpand all lines: Strings/anagram_indices.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'''
2-
Anagram indicies
2+
Anagram indices
33
44
Given a word and a string S, find all starting indices in S which are anagrams of word.
55
(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):
5454
return len(self.__letters) == 0
5555

5656

57-
def anagram_indicies(s, word):
57+
def anagram_indices(s, word):
5858
n = len(s)
5959
w = len(word)
6060
res = []
@@ -92,4 +92,4 @@ def anagram_indicies(s, word):
9292
###########
9393

9494
# Correct result => [0, 3, 4]
95-
print(anagram_indicies('abxaba', 'ab'))
95+
print(anagram_indices('abxaba', 'ab'))
Collapse file

‎Strings/create_palindrome.py‎

Copy file name to clipboardExpand all lines: Strings/create_palindrome.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def create_palindrome(string):
2828

2929
# find the biggest left palindrome, which starts from the first letter
3030
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))
3232
front = 0
3333
back = i
3434
found = True

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.