Content-Length: 16859 | pFad | http://github.com/codebasics/data-structures-algorithms-python/pull/90.patch
thub.com
From 001fd46c6160a13c73210d64eedf145a1b2bd047 Mon Sep 17 00:00:00 2001
From: rohansaxena2020
Date: Tue, 19 Mar 2024 23:46:16 -0400
Subject: [PATCH 1/5] Adding 1st exercise files
---
1.py | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
util.py | 9 +++++
2 files changed, 114 insertions(+)
create mode 100644 1.py
create mode 100644 util.py
diff --git a/1.py b/1.py
new file mode 100644
index 0000000..860e313
--- /dev/null
+++ b/1.py
@@ -0,0 +1,105 @@
+# ### Binary Search Exercise
+# 1. When I try to find number 5 in below list using binary search, it doesn't work and returns me -1 index. Why is that?
+
+# ```numbers = [1,4,6,9,10,5,7]```
+
+# This is because the array is not sorted in order from lowest to highest.
+# Once it splits the first time, it starts looking in the [1,4,6] range and doesn't find 5
+
+# 1. Find index of all the occurances of a number from sorted list
+
+# ```
+# numbers = [1,4,6,9,11,15,15,15,17,21,34,34,56]
+# number_to_find = 15
+# ```
+# This should return 5,6,7 as indices containing number 15 in the array
+
+from util import time_it
+
+@time_it
+def linear_search(numbers_list, number_to_find):
+ for index, element in enumerate(numbers_list):
+ if element == number_to_find:
+ return index
+ return -1
+
+@time_it
+def binary_search(numbers_list, number_to_find):
+ left_index = 0
+ right_index = len(numbers_list) - 1
+ mid_index = 0
+
+ while left_index <= right_index:
+ mid_index = (left_index + right_index) // 2
+ mid_number = numbers_list[mid_index]
+
+ if mid_number == number_to_find:
+ return mid_index
+
+ if mid_number < number_to_find:
+ left_index = mid_index + 1
+ else:
+ right_index = mid_index - 1
+
+ return -1
+
+def binary_search_recursive(numbers_list, number_to_find, left_index, right_index):
+ if right_index < left_index:
+ return -1
+
+ mid_index = (left_index + right_index) // 2
+ if mid_index >= len(numbers_list) or mid_index < 0:
+ return -1
+
+ mid_number = numbers_list[mid_index]
+
+ if mid_number == number_to_find:
+ return mid_index
+
+ if mid_number < number_to_find:
+ left_index = mid_index + 1
+ else:
+ right_index = mid_index - 1
+
+ return binary_search_recursive(numbers_list, number_to_find, left_index, right_index)
+
+#this should run the binary search, find the index, and then recursively run the search on both the right and left side
+def binary_search_multiple(numbers_list, number_to_find):
+
+ index = binary_search(numbers_list,number_to_find)
+ result_indices = [index]
+
+ # find all indices on the left
+ i = index - 1
+ while i>=0:
+ if numbers_list[i] == numbers_list[index]:
+ result_indices.append(i)
+ else:
+ break
+ i = i-1
+
+ # find all indices on the right
+ i = index + 1
+ while i
Date: Wed, 20 Mar 2024 22:53:31 -0400
Subject: [PATCH 2/5] adding 2nd exercise my solution
---
2.py | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 84 insertions(+)
create mode 100644 2.py
diff --git a/2.py b/2.py
new file mode 100644
index 0000000..a6e01ca
--- /dev/null
+++ b/2.py
@@ -0,0 +1,84 @@
+# ### Bubble Sort Exercise
+
+# Modify [bubble_sort function](https://github.com/codebasics/data-structures-algorithms-python/blob/master/algorithms/2_BubbleSort/bubble_sort.py) such that it can sort following list of transactions happening in an electronic store,
+# ```
+# elements = [
+# { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
+# { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
+# { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
+# { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
+# ]
+# ```
+# bubble_sort function should take key from a transaction record and sort the list as per that key. For example,
+# ```
+# bubble_sort(elements, key='transaction_amount')
+# ```
+# This will sort elements by transaction_amount and your sorted list will look like,
+# ```
+# elements = [
+# { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
+# { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
+# { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
+# { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
+# ]
+# ```
+# But if you call it like this,
+# ```
+# bubble_sort(elements, key='name')
+# ```
+# output will be,
+# ```
+# elements = [
+# { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
+# { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
+# { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
+# { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
+# ]
+# ```
+
+# base bubble_sort. you can use this to sort strings too
+def bubble_sort(elements):
+ size = len(elements)
+
+ for i in range(size-1):
+ swapped = False
+ for j in range(size-1-i):
+ if elements[j] > elements[j+1]:
+ tmp = elements[j]
+ elements[j] = elements[j+1]
+ elements[j+1] = tmp
+ swapped = True
+
+ if not swapped:
+ break
+
+def bubble_sort_by_key(elements, key):
+ size = len(elements)
+
+ for i in range(size-1):
+ swapped = False
+ for j in range(size-1-i):
+ if elements[j][key] > elements[j+1][key]:
+ tmp = elements[j]
+ elements[j] = elements[j+1]
+ elements[j+1] = tmp
+ swapped = True
+
+ if not swapped:
+ break
+
+
+elements = [5,9,2,1,67,34,88,34]
+elements = [1,2,3,4,2]
+elements = ["mona", "dhaval", "aamir", "tina", "chang"]
+
+bubble_sort(elements)
+print(elements)
+
+elements2 = [ { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
+ { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
+ { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
+ { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
+ ]
+bubble_sort_by_key(elements2,key='transaction_amount')
+print(elements2)
From da2f908e8a59d1ed955d4773ffe38b400e889248 Mon Sep 17 00:00:00 2001
From: rohansaxena2020
Date: Wed, 20 Mar 2024 22:57:20 -0400
Subject: [PATCH 3/5] updating 2nd exercise file
---
2.py | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/2.py b/2.py
index a6e01ca..428b676 100644
--- a/2.py
+++ b/2.py
@@ -75,10 +75,10 @@ def bubble_sort_by_key(elements, key):
bubble_sort(elements)
print(elements)
-elements2 = [ { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
+elementsv2 = [ { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
{ 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
{ 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
{ 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
]
-bubble_sort_by_key(elements2,key='transaction_amount')
-print(elements2)
+bubble_sort_by_key(elementsv2,key='transaction_amount')
+print(elementsv2)
From 4784bacc2cf39618c44aeb1152f04c7288771d02 Mon Sep 17 00:00:00 2001
From: rohansaxena2020
Date: Wed, 20 Mar 2024 23:02:10 -0400
Subject: [PATCH 4/5] adding 1st exercise solution here
---
1.py | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
util.py | 9 +++++
2 files changed, 114 insertions(+)
create mode 100644 1.py
create mode 100644 util.py
diff --git a/1.py b/1.py
new file mode 100644
index 0000000..860e313
--- /dev/null
+++ b/1.py
@@ -0,0 +1,105 @@
+# ### Binary Search Exercise
+# 1. When I try to find number 5 in below list using binary search, it doesn't work and returns me -1 index. Why is that?
+
+# ```numbers = [1,4,6,9,10,5,7]```
+
+# This is because the array is not sorted in order from lowest to highest.
+# Once it splits the first time, it starts looking in the [1,4,6] range and doesn't find 5
+
+# 1. Find index of all the occurances of a number from sorted list
+
+# ```
+# numbers = [1,4,6,9,11,15,15,15,17,21,34,34,56]
+# number_to_find = 15
+# ```
+# This should return 5,6,7 as indices containing number 15 in the array
+
+from util import time_it
+
+@time_it
+def linear_search(numbers_list, number_to_find):
+ for index, element in enumerate(numbers_list):
+ if element == number_to_find:
+ return index
+ return -1
+
+@time_it
+def binary_search(numbers_list, number_to_find):
+ left_index = 0
+ right_index = len(numbers_list) - 1
+ mid_index = 0
+
+ while left_index <= right_index:
+ mid_index = (left_index + right_index) // 2
+ mid_number = numbers_list[mid_index]
+
+ if mid_number == number_to_find:
+ return mid_index
+
+ if mid_number < number_to_find:
+ left_index = mid_index + 1
+ else:
+ right_index = mid_index - 1
+
+ return -1
+
+def binary_search_recursive(numbers_list, number_to_find, left_index, right_index):
+ if right_index < left_index:
+ return -1
+
+ mid_index = (left_index + right_index) // 2
+ if mid_index >= len(numbers_list) or mid_index < 0:
+ return -1
+
+ mid_number = numbers_list[mid_index]
+
+ if mid_number == number_to_find:
+ return mid_index
+
+ if mid_number < number_to_find:
+ left_index = mid_index + 1
+ else:
+ right_index = mid_index - 1
+
+ return binary_search_recursive(numbers_list, number_to_find, left_index, right_index)
+
+#this should run the binary search, find the index, and then recursively run the search on both the right and left side
+def binary_search_multiple(numbers_list, number_to_find):
+
+ index = binary_search(numbers_list,number_to_find)
+ result_indices = [index]
+
+ # find all indices on the left
+ i = index - 1
+ while i>=0:
+ if numbers_list[i] == numbers_list[index]:
+ result_indices.append(i)
+ else:
+ break
+ i = i-1
+
+ # find all indices on the right
+ i = index + 1
+ while i
Date: Wed, 20 Mar 2024 23:08:46 -0400
Subject: [PATCH 5/5] adding 2nd exercise solution
---
2.py | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 84 insertions(+)
create mode 100644 2.py
diff --git a/2.py b/2.py
new file mode 100644
index 0000000..a6e01ca
--- /dev/null
+++ b/2.py
@@ -0,0 +1,84 @@
+# ### Bubble Sort Exercise
+
+# Modify [bubble_sort function](https://github.com/codebasics/data-structures-algorithms-python/blob/master/algorithms/2_BubbleSort/bubble_sort.py) such that it can sort following list of transactions happening in an electronic store,
+# ```
+# elements = [
+# { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
+# { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
+# { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
+# { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
+# ]
+# ```
+# bubble_sort function should take key from a transaction record and sort the list as per that key. For example,
+# ```
+# bubble_sort(elements, key='transaction_amount')
+# ```
+# This will sort elements by transaction_amount and your sorted list will look like,
+# ```
+# elements = [
+# { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
+# { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
+# { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
+# { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
+# ]
+# ```
+# But if you call it like this,
+# ```
+# bubble_sort(elements, key='name')
+# ```
+# output will be,
+# ```
+# elements = [
+# { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
+# { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
+# { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
+# { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
+# ]
+# ```
+
+# base bubble_sort. you can use this to sort strings too
+def bubble_sort(elements):
+ size = len(elements)
+
+ for i in range(size-1):
+ swapped = False
+ for j in range(size-1-i):
+ if elements[j] > elements[j+1]:
+ tmp = elements[j]
+ elements[j] = elements[j+1]
+ elements[j+1] = tmp
+ swapped = True
+
+ if not swapped:
+ break
+
+def bubble_sort_by_key(elements, key):
+ size = len(elements)
+
+ for i in range(size-1):
+ swapped = False
+ for j in range(size-1-i):
+ if elements[j][key] > elements[j+1][key]:
+ tmp = elements[j]
+ elements[j] = elements[j+1]
+ elements[j+1] = tmp
+ swapped = True
+
+ if not swapped:
+ break
+
+
+elements = [5,9,2,1,67,34,88,34]
+elements = [1,2,3,4,2]
+elements = ["mona", "dhaval", "aamir", "tina", "chang"]
+
+bubble_sort(elements)
+print(elements)
+
+elements2 = [ { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
+ { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
+ { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
+ { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
+ ]
+bubble_sort_by_key(elements2,key='transaction_amount')
+print(elements2)
--- a PPN by Garber Painting Akron. With Image Size Reduction included!Fetched URL: http://github.com/codebasics/data-structures-algorithms-python/pull/90.patch
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy