- Notifications
You must be signed in to change notification settings - Fork1.6k
My code#86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
RohanSaxena2020 wants to merge5 commits intocodebasics:masterChoose a base branch fromRohanSaxena2020:My_Code
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
My code#86
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions2_BubbleSort/2.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
25 changes: 25 additions & 0 deletions2_BubbleSort/bubble_sort.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# 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 | ||
if __name__ == '__main__': | ||
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) |
40 changes: 40 additions & 0 deletions2_BubbleSort/bubble_sort_exercise.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
### 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'}, | ||
] | ||
``` | ||
[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/algorithms/2_BubbleSort/bubble_sort_exercise_solution.py) | ||
105 changes: 105 additions & 0 deletionsAlgorithms/1_BinarySearch/1.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<len(numbers_list): | ||
if numbers_list[i] == numbers_list[index]: | ||
result_indices.append(i) | ||
else: | ||
break | ||
i = i+1 | ||
return sorted(result_indices) | ||
numbers_list = [12, 15, 17, 19, 21, 21, 21, 21, 24, 45, 67] | ||
number_to_find = 21 | ||
index = binary_search_multiple(numbers_list, number_to_find) | ||
print(f"Number found at index {index} using binary search") | ||
numbers = [1,4,6,9,11,15,15,15,15,17,21,34,34,56] | ||
number_to_find = 15 | ||
index = binary_search_multiple(numbers, number_to_find) | ||
print(f"Number found at index {index} using binary search") | ||
#Lesson: I was approaching it wrong. If something isn't working, scratch the approach. | ||
#Lesson #2: Try the simplest solution first. Although in this case it's a bit ugly since you're just doing a linear search after your binary search |
84 changes: 84 additions & 0 deletionsAlgorithms/1_BinarySearch/2_BubbleSort/2.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
25 changes: 25 additions & 0 deletionsAlgorithms/1_BinarySearch/2_BubbleSort/bubble_sort.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# 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 | ||
if __name__ == '__main__': | ||
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) |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.