From 4784bacc2cf39618c44aeb1152f04c7288771d02 Mon Sep 17 00:00:00 2001 From: rohansaxena2020 Date: Wed, 20 Mar 2024 23:02:10 -0400 Subject: [PATCH 1/9] 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 2/9] 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) From 2d38feeebe3d3ee50b3659e5dcbd4e741273df84 Mon Sep 17 00:00:00 2001 From: rohansaxena2020 Date: Sat, 23 Mar 2024 23:06:09 -0400 Subject: [PATCH 3/9] adding quicksort exercise solution --- 3.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 3.py diff --git a/3.py b/3.py new file mode 100644 index 0000000..dfc4f52 --- /dev/null +++ b/3.py @@ -0,0 +1,66 @@ +def swap(a, b, arr): + if a!=b: + tmp = arr[a] + arr[a] = arr[b] + arr[b] = tmp + +# Sorts a (portion of an) array, divides it into partitions, then sorts those +def quicksort(A, lo, hi): + if lo >= 0 and lo < hi: + lt, gt = partition(A, lo, hi) # Multiple return values + quicksort(A, lo, lt - 1) + quicksort(A, gt + 1, hi) + +# Divides array into three partitions +def partition(A, lo, hi): + # Pivot value + pivot = A[(lo + hi) // 2] # Choose the middle element as the pivot (integer division) + + # Lesser, equal and greater index + lt = lo + eq = lo + gt = hi + + # Iterate and compare all elements with the pivot + + while eq <= gt: + if A[eq] < pivot: + # Swap the elements at the equal and lesser indices + swap(eq, lt, A) + # Increase lesser index + lt += 1 + # Increase equal index + eq += 1 + elif A[eq] > pivot: + # Swap the elements at the equal and greater indices + swap(eq, gt, A) + # Decrease greater index + gt -= 1 + else: # A[eq] == pivot + # Increase equal index + eq += 1 + + # Return lesser and greater indices + return lt, gt + +elements = [11,9,29,7,2,15,28] +# elements = ["mona", "dhaval", "aamir", "tina", "chang"] +quicksort(elements, 0, len(elements)-1) +print(elements) + +tests = [ + [11,9,29,7,2,15,28], + [3, 7, 9, 11], + [25, 22, 21, 10], + [29, 15, 28], + [], + [6] +] + +try: + # Your script's entry point, e.g., function calls + for elements in tests: + quicksort(elements, 0, len(elements)-1) + print(f'sorted array: {elements}') +except Exception as e: + print(f"Error occurred: {e}") \ No newline at end of file From a0ce5d486eac65dd8fd05f32e228cc1fd4a9d3d6 Mon Sep 17 00:00:00 2001 From: rohansaxena2020 Date: Sun, 24 Mar 2024 21:23:33 -0400 Subject: [PATCH 4/9] added inserion sort code exercise solution --- 4.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 4.py diff --git a/4.py b/4.py new file mode 100644 index 0000000..e901170 --- /dev/null +++ b/4.py @@ -0,0 +1,44 @@ +# ### Exercise: Insertion Sort + +# Compute the running median of a sequence of numbers. That is, given a stream of numbers, print out the median of the list so far on each new element. + +# Recall that the median of an even-numbered list is the average of the two middle numbers in a *sorted list*. + +# For example, given the sequence `[2, 1, 5, 7, 2, 0, 5]`, your algorithm should print out: + +# ``` +# 2 +# 1.5 +# 2 +# 3.5 +# 2 +# 2 +# 2 +# ``` + +def find_median_value(elements): + if len(elements) == 1: #if the array has 1 element + return elements[0] + if len(elements) % 2 != 0: #if the array has an odd number of elements + return elements[(len(elements)//2)] + else: #if the array has an even number of elements + return ((elements[int(len(elements)/2)]+elements[int(len(elements)/2-1)])/2) + +def insertion_sort(elements): + for i in range(1, len(elements)): + print(find_median_value(elements[0:i])) + anchor = elements[i] + j = i - 1 + while j>=0 and anchor < elements[j]: + elements[j+1] = elements[j] + j = j - 1 + elements[j+1] = anchor + print(find_median_value(elements)) + +# print (find_median_value([1,2,3,4,5,7,20,33,34])) +# print (find_median_value([1,2,3,4,8,7,20,33])) + +elements = [2, 1, 5, 7, 2, 0, 5] +# print(elements[0:1]) +insertion_sort(elements) +print(elements) \ No newline at end of file From 0e337cd47c2a428fdf3fb07208418cd3be05eb10 Mon Sep 17 00:00:00 2001 From: rohansaxena2020 Date: Tue, 26 Mar 2024 22:57:31 -0400 Subject: [PATCH 5/9] Added Merge Sort Exercise Code --- 5.py | 158 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 5.py diff --git a/5.py b/5.py new file mode 100644 index 0000000..ddadcb7 --- /dev/null +++ b/5.py @@ -0,0 +1,158 @@ +# ### Merge Sort Exercise + +# Modify [merge_sort function](https://github.com/codebasics/data-structures-algorithms-python/blob/master/algorithms/5_MergeSort/merge_sort_final.py) such that it can sort following list of athletes as per the time taken by them in the marathon, +# ``` +# elements = [ +# { 'name': 'vedanth', 'age': 17, 'time_hours': 1}, +# { 'name': 'rajab', 'age': 12, 'time_hours': 3}, +# { 'name': 'vignesh', 'age': 21, 'time_hours': 2.5}, +# { 'name': 'chinmay', 'age': 24, 'time_hours': 1.5}, +# ] +# ``` +# merge_sort function should take key from an athlete's marathon log and sort the list as per that key. For example, +# ``` +# merge_sort(elements, key='time_hours', descending=True) +# ``` +# This will sort elements by time_hours and your sorted list will look like, +# ``` +# elements = [ +# {'name': 'rajab', 'age': 12, 'time_hours': 3}, +# {'name': 'vignesh', 'age': 21, 'time_hours': 2.5}, +# {'name': 'chinmay', 'age': 24, 'time_hours': 1.5}, +# {'name': 'vedanth', 'age': 17, 'time_hours': 1}, +# ] +# ``` +# But if you call it like this, +# ``` +# merge_sort(elements, key='name') +# ``` +# output will be, +# ``` +# elements = [ +# { 'name': 'chinmay', 'age': 24, 'time_hours': 1.5}, +# { 'name': 'rajab', 'age': 12, 'time_hours': 3}, +# { 'name': 'vedanth', 'age': 17, 'time_hours': 1}, +# { 'name': 'vignesh', 'age': 21, 'time_hours': 2.5}, +# ] +# ``` + +# [Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/algorithms/5_MergeSort/merge_sort_exercise_solution.py) + +# def merge_sort(arr): +# if len(arr) <= 1: +# return + +# mid = len(arr)//2 + +# left = arr[:mid] +# right = arr[mid:] + +# merge_sort(left) +# merge_sort(right) + +# merge_two_sorted_lists(left, right, arr) + +# def merge_two_sorted_lists(a,b,arr): +# len_a = len(a) +# len_b = len(b) + +# i = j = k = 0 + +# while i < len_a and j < len_b: +# if a[i] <= b[j]: +# arr[k] = a[i] +# i+=1 +# else: +# arr[k] = b[j] +# j+=1 +# k+=1 + +# while i < len_a: +# arr[k] = a[i] +# i+=1 +# k+=1 + +# while j < len_b: +# arr[k] = b[j] +# j+=1 +# k+=1 + +# def merge_two_sorted_by_key(a,b,arr,key): +# len_a = len(a) +# len_b = len(b) + +# i = j = k = 0 + +# while i < len_a and j < len_b: +# if a[i][key] <= b[j][key]: +# arr[k] = a[i] +# i+=1 +# else: +# arr[k] = b[j] +# j+=1 +# k+=1 + +# while i < len_a: +# arr[k] = a[i] +# i+=1 +# k+=1 + +# while j < len_b: +# arr[k] = b[j] +# j+=1 +# k+=1 + +def merge_sort_by_key(arr, key,descending=False): + if len(arr) <= 1: + return arr + + mid = len(arr)//2 + + left = arr[:mid] + right = arr[mid:] + + left = merge_sort_by_key(left,key,descending) + right = merge_sort_by_key(right,key,descending) + + return merge_two_sorted_lists_by_key(left, right,key,descending) + +def merge_two_sorted_lists_by_key(a,b,key,descending): + sorted_list = [] + + len_a = len(a) + len_b = len(b) + + i = j = 0 + + while i < len_a and j < len_b: + if descending: + condition = a[i][key] > b[j][key] # Note the change here for descending + else: + condition = a[i][key] <= b[j][key] + + if condition: + sorted_list.append(a[i]) + i += 1 + else: + sorted_list.append(b[j]) + j += 1 + + while i < len_a: + sorted_list.append(a[i]) + i+=1 + + while j < len_b: + sorted_list.append(b[j]) + j+=1 + + return sorted_list + + +elements = [ + { 'name': 'vedanth', 'age': 17, 'time_hours': 1}, + { 'name': 'rajab', 'age': 12, 'time_hours': 3}, + { 'name': 'vignesh', 'age': 21, 'time_hours': 2.5}, + { 'name': 'chinmay', 'age': 24, 'time_hours': 1.5}, + ] + +print(merge_sort_by_key(elements,key="age",descending=False)) \ No newline at end of file From db3eaecded87498b8996c7662388a7b26dae6140 Mon Sep 17 00:00:00 2001 From: rohansaxena2020 Date: Wed, 27 Mar 2024 23:49:16 -0400 Subject: [PATCH 6/9] added shell sort code --- 6.py | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 6.py diff --git a/6.py b/6.py new file mode 100644 index 0000000..65f8a10 --- /dev/null +++ b/6.py @@ -0,0 +1,109 @@ +# # Exercise: Shell Sort + +# Sort the elements of a given list using shell sort, but with a slight modification. Remove all the repeating occurances of elements while sorting. + +# Traditionally, when comparing two elements in shell sort, we swap if first element is bigger than second, and do nothing otherwise. + +# In this modified shell sort with duplicate removal, we will swap if first element is bigger than second, and do nothing if element is smaller, but if values are same, we will delete one of the two elements we are comparing before starting the next pass for the reduced gap. + + + +# For example, given the unsorted list `[2, 1, 5, 7, 2, 0, 5, 1, 2, 9, 5, 8, 3]`, after sorting using shell sort without duplicates, the sorted list would be: + +# ``` +# [0, 1, 2, 3, 5, 7, 8, 9] +# ``` + +# [23,3,1,56,34] + +def shell_sort(arr): + size = len(arr) + gap = size//2 + + while gap > 0: + for i in range(gap,size): + anchor = arr[i] + j = i + while j>=gap and arr[j-gap]>anchor: + arr[j] = arr[j-gap] + j -= gap + arr[j] = anchor + gap = gap // 2 + +# [23,3,1,56,34] + +# def shell_sort_remove_duplicates(arr): +# size = len(arr) +# gap = size//2 + +# while gap > 0: +# i = gap +# while i < size: +# anchor = arr[i] +# j = i +# while j>=gap: +# if arr[j-gap] > anchor: +# arr[j] = arr[j-gap] +# elif arr[j - gap] == anchor: # If elements are the same, prepare to delete one +# del arr[j] # Delete the current element +# size -= 1 # Decrease the size because we've removed an element +# i -= 1 # Adjust i since we've shifted the array elements +# break # Exit the inner while loop since we've handled the duplicate +# else: +# break +# j -= gap +# if j != i: # Ensures that we don't reset anchor if it was deleted +# arr[j] = anchor +# i += 1 +# gap = gap // 2 + +def shell_sort_remove_duplicates(arr): + size = len(arr) + gap = size // 2 + + while gap > 0: + for i in range(gap, size): + anchor = arr[i] + j = i + while j >= gap and arr[j - gap] > anchor: + arr[j] = arr[j - gap] + j -= gap + + # Place anchor at its correct position + arr[j] = anchor + + # After each gap reduction, remove duplicates + i = 0 + while i < (size - 1): + # If duplicate found + if arr[i] == arr[i+1]: + del arr[i+1] + size -= 1 # Reduce size after deletion + else: + i += 1 # Only increase i if no deletion happened to check next element + + gap = gap // 2 + + return arr + + +tests = [ + [89, 78, 61, 53, 23, 21, 17, 12, 9, 7, 6, 2, 1], + [], + [1,5,8,9], + [234,3,1,56,34,12,9,12,1300], + [5] + ] + +# for elements in tests: +# shell_sort(elements) +# print(elements) + + +elements2 = [2, 1, 5, 7, 2, 0, 5, 1, 2, 9, 5, 8, 3] +shell_sort_remove_duplicates(elements2) +print(elements2) + +elements = [23,12,23,1,1,1,56,34,34] +shell_sort_remove_duplicates(elements) +print(elements) \ No newline at end of file From c13c86f558f87c143c6a9a831b56b367214d207f Mon Sep 17 00:00:00 2001 From: rohansaxena2020 Date: Thu, 28 Mar 2024 22:01:32 -0400 Subject: [PATCH 7/9] added selection sort code --- 7.py | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 7.py diff --git a/7.py b/7.py new file mode 100644 index 0000000..953f811 --- /dev/null +++ b/7.py @@ -0,0 +1,155 @@ +# # Exercise: Selection Sort + +# Implement a Multi-Level Sort of a given list of dictionaries based on a given sorting order. +# If user wants to sort dictionary based on First Key 'A', Then Key 'B', +# they shall pass list of keys in the order of preference as a list ['A','B']. +# Your code should be able to sort list of dictionaries for any number of keys in sorting order list. + +# Using this multi-level sort, you should be able to sort any list of dictionaries based on sorting order preference + +# Example: +# A single dictionary entry contains two keys 'First Name' and 'Last Name'. the list should be sorted first based on 'First Name', then based on 'Last Name', w.r.t. common/same 'First Name' entries. + +# for this, one shall past sorting order of preference list [ 'First Name' , 'Last Name' ] + +# For this, Given the following sequence List: + +# ``` +# [ +# {'First Name': 'Raj', 'Last Name': 'Nayyar'}, +# {'First Name': 'Suraj', 'Last Name': 'Sharma'}, +# {'First Name': 'Karan', 'Last Name': 'Kumar'}, +# {'First Name': 'Jade', 'Last Name': 'Canary'}, +# {'First Name': 'Raj', 'Last Name': 'Thakur'}, +# {'First Name': 'Raj', 'Last Name': 'Sharma'}, +# {'First Name': 'Kiran', 'Last Name': 'Kamla'}, +# {'First Name': 'Armaan', 'Last Name': 'Kumar'}, +# {'First Name': 'Jaya', 'Last Name': 'Sharma'}, +# {'First Name': 'Ingrid', 'Last Name': 'Galore'}, +# {'First Name': 'Jaya', 'Last Name': 'Seth'}, +# {'First Name': 'Armaan', 'Last Name': 'Dadra'}, +# {'First Name': 'Ingrid', 'Last Name': 'Maverick'}, +# {'First Name': 'Aahana', 'Last Name': 'Arora'} +# ] +# ``` + + +# Your algorithm should generate sorted list: + +# ``` +# [ +# {'First Name': 'Aahana', 'Last Name': 'Arora'} +# {'First Name': 'Armaan', 'Last Name': 'Dadra'} +# {'First Name': 'Armaan', 'Last Name': 'Kumar'} +# {'First Name': 'Ingrid', 'Last Name': 'Galore'} +# {'First Name': 'Ingrid', 'Last Name': 'Maverick'} +# {'First Name': 'Jade', 'Last Name': 'Canary'} +# {'First Name': 'Jaya', 'Last Name': 'Seth'} +# {'First Name': 'Jaya', 'Last Name': 'Sharma'} +# {'First Name': 'Karan', 'Last Name': 'Kumar'} +# {'First Name': 'Kiran', 'Last Name': 'Kamla'} +# {'First Name': 'Raj', 'Last Name': 'Nayyar'} +# {'First Name': 'Raj', 'Last Name': 'Sharma'} +# {'First Name': 'Raj', 'Last Name': 'Thakur'} +# {'First Name': 'Suraj', 'Last Name': 'Sharma'} +# ] +# ``` + +def selection_sort(arr): + size = len(arr) + for i in range(size-1): + min_index = i + for j in range(min_index+1,size): + if arr[j] < arr[min_index]: + min_index = j + if i != min_index: + arr[i], arr[min_index] = arr[min_index], arr[i] + +# def selection_sort_multi(arr,keys): +# size = len(arr) +# # for key in keys[-1::-1]: +# # for i in range(size): +# # min_index = i +# # for j in range(i+1,size): +# # if arr[j][key] < arr[min_index][key]: +# # min_index = j +# # arr[i], arr[min_index] = arr[min_index], arr[i] +# for key in reversed(keys): +# for i in range(size): +# min_or_max_index = i +# for j in range(i+1, size): +# if arr[j][key] < arr[min_or_max_index][key]: +# min_or_max_index = j +# # Swap the found minimum element with the first element +# arr[i], arr[min_or_max_index] = arr[min_or_max_index], arr[i] + +# def selection_sort_multi(elements, sort_by_list): +# for sort_by in sort_by_list[-1::-1]: +# for x in range(len(elements)): +# min_index = x +# for y in range(x, len(elements)): +# if elements[y][sort_by] < elements[min_index][sort_by]: +# min_index = y +# if x != min_index: +# elements[x], elements[min_index] = elements[min_index], elements[x] + +def selection_sort_multi(arr, keys): + size = len(arr) + + # Adjusted loop to clearly iterate through keys in reversed order + for key in reversed(keys): + # Selection sort adapted for multiple keys + for i in range(size): + # Initially, min_index is the starting index + min_index = i + # Find the minimum element in remaining unsorted array + for j in range(i+1, size): + # Check condition for current sorting key + if arr[j][key] < arr[min_index][key]: + min_index = j + + # Swap the found minimum element with the first element + arr[i], arr[min_index] = arr[min_index], arr[i] + +def multilevel_selection_sort(elements, sort_by_list): + for sort_by in sort_by_list[-1::-1]: + for x in range(len(elements)): + min_index = x + for y in range(x, len(elements)): + if elements[y][sort_by] < elements[min_index][sort_by]: + min_index = y + if x != min_index: + elements[x], elements[min_index] = elements[min_index], elements[x] + +tests = [ + [89, 78, 61, 53, 23, 21, 17, 12, 9, 7, 6, 2, 1], + [], + [1,5,8,9], + [234,3,1,56,34,12,9,12,1300], + [78, 12, 15, 8, 61, 53, 23, 27], + [5] + ] + +# for elements in tests: +# selection_sort(elements) +# print(elements) + +element2 = [ + {'First Name': 'Raj', 'Last Name': 'Nayyar'}, + {'First Name': 'Suraj', 'Last Name': 'Sharma'}, + {'First Name': 'Karan', 'Last Name': 'Kumar'}, + {'First Name': 'Jade', 'Last Name': 'Canary'}, + {'First Name': 'Raj', 'Last Name': 'Thakur'}, + {'First Name': 'Raj', 'Last Name': 'Sharma'}, + {'First Name': 'Kiran', 'Last Name': 'Kamla'}, + {'First Name': 'Armaan', 'Last Name': 'Kumar'}, + {'First Name': 'Jaya', 'Last Name': 'Sharma'}, + {'First Name': 'Ingrid', 'Last Name': 'Galore'}, + {'First Name': 'Jaya', 'Last Name': 'Seth'}, + {'First Name': 'Armaan', 'Last Name': 'Dadra'}, + {'First Name': 'Ingrid', 'Last Name': 'Maverick'}, + {'First Name': 'Aahana', 'Last Name': 'Arora'} +] + +multilevel_selection_sort(element2,['First Name','Last Name']) +print(element2) \ No newline at end of file From f779d04d264837b95891623892260c07ee8be9fa Mon Sep 17 00:00:00 2001 From: rohansaxena2020 Date: Sun, 31 Mar 2024 22:42:13 -0400 Subject: [PATCH 8/9] added 8.py - final exercises code --- 8.py | 204 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 8.py diff --git a/8.py b/8.py new file mode 100644 index 0000000..836b400 --- /dev/null +++ b/8.py @@ -0,0 +1,204 @@ +def find_sum(n): + if n==1: + return 1 + return n + find_sum(n-1) + +def fib(n): + # 0,1,1,2,3,5,8 <-- fibonacci numbers + # -------------- + # 0,1,2,3,4,5,6 <-- index + if n==0 or n==1: + return n + return fib(n-1) + fib(n-2) + +# print(find_sum(5)) +# print(fib(10)) + +# 1. Write a Python program to calculate the sum of a list of numbers using recursion. +# Click me to see the sample solution + +def find_list_sum(list): + if len(list) == 0: + return 0 + if len(list) == 1: + return list[0] + return list[len(list)-1] + find_list_sum(list[0:len(list)-1]) + +# print(find_list_sum([4,2,3,8,13,10,10])) + +# 2. Write a Python program to convert an integer to a string in any base using recursion . +# Click me to see the sample solution + +#IDK what that means + +# 3. Write a Python program to sum recursion lists using recursion. +# Test Data: [1, 2, [3,4], [5,6]] +# Expected Result: 21 +# Click me to see the sample solution + +def find_list_of_lists_sum(list): + for item in list: + if type(item) == int: + return item + elif type(item) == list: + find_list_of_lists_sum(item) + return + +# print(find_list_of_lists_sum([1, 2, [3,4], [5,6]])) + +# 4. Write a Python program to get the factorial of a non-negative integer using recursion. +# Click me to see the sample solution + +def find_factorial(int): + if int == 0 or int ==1: + return 1 + return int * find_factorial(int-1) + +# print(find_factorial(6)) + +# 5. Write a Python program to solve the Fibonacci sequence using recursion. +# Click me to see the sample solution + +def fib2(n): + # 0,1,1,2,3,5,8 <-- fibonacci numbers + # -------------- + # 0,1,2,3,4,5,6 <-- index + if n==0 or n==1: + return n + return fib2(n-1) + fib2(n-2) + +# print(fib(7)) + +# 6. Write a Python program to get the sum of a non-negative integer using recursion. +# Test Data: +# sumDigits(345) -> 12 +# sumDigits(45) -> 9 +# Click me to see the sample solution + +def sumDigits(number): + str_num = str(number) + digits = [int(num) for num in str_num] + + if len(digits) == 0: + return 0 + if len(digits) == 1: + return int(digits[0]) + + updated_list = digits[:-1] + str_digits = ''.join(str(digit) for digit in updated_list) + new_number = int(str_digits) + return int(digits[len(digits)-1]) + sumDigits(new_number) + +# print(sumDigits(345)) + + +# 7. Write a Python program to calculate the sum of the positive integers of n+(n-2)+(n-4)... (until n-x =< 0) using recursion . +# Test Data: +# sum_series(6) -> 12 +# sum_series(10) -> 30 +# Click me to see the sample solution + +def sum_minus_twos(num): + if num == 0 or num == 1 or num == 2: + return num + return num + sum_minus_twos(num-2) + +# print(sum_minus_twos(15)) + + +# 8. Write a Python program to calculate the sum of harmonic series upto n terms. +# Note: The harmonic sum is the sum of reciprocals of the positive integers. +# Example : +# harmonic series +# Click me to see the sample solution + +def harmonic_sum(num): + if num == 0: + return "n/a" + if num == 1: + return 1 + return (1/num) + harmonic_sum(num-1) + +# print(harmonic_sum(5)) + +# 9. Write a Python program to calculate the geometric sum up to 'n' terms. +# Note: In mathematics, a geometric series is a series with a constant ratio between successive terms. + +# Ex: geometric_sum(3) = 1+ 1/2 + 1/4 + +def geometric_sum(num): + if num == 0: + return 1 + return 1/(pow(2,num)) + geometric_sum(num-1) + +# print(geometric_sum(4)) + +# Click me to see the sample solution + +# 10. Write a Python program to calculate the value of 'a' to the power of 'b' using recursion. +# Test Data : +# (power(3,4) -> 81 +# Click me to see the sample solution + +def calc_exponent(a,b): + if b == 1: + return a + return a * calc_exponent(a,b-1) + +# print(calc_exponent(4,3)) + +# 11. Write a Python program to find the greatest common divisor (GCD) of two integers using recursion. +# Click me to see the sample solution + +def Recurgcd(a, b): + # Determine the lower and higher values between 'a' and 'b' + low = min(a, b) + high = max(a, b) + + # Check if the lower value is 0 (base case for GCD calculation) + if low == 0: + # If the lower value is 0, return the higher value (GCD is the non-zero value) + return high + # Check if the lower value is 1 (base case for GCD calculation) + elif low == 1: + # If the lower value is 1, return 1 (GCD of any number with 1 is 1) + return 1 + else: + # If neither base case is met, recursively call the Recurgcd function + # with the lower value and the remainder of the higher value divided by the lower value + return Recurgcd(low, high % low) + +# Print the result of calling the Recurgcd function with the input values 12 and 14 +print(Recurgcd(24, 64)) + + +def find_GCF(a,b): + if a % b == 0: #if b is divisible by a + return b + if b%a == 0: #if a is divisible by b + return a + + factors_a = [] + for i in range (1,a+1): + if a % i == 0: + factors_a.append(i) + + factors_b = [] + for i in range (1,b+1): + if b % i == 0: + factors_b.append(i) + + common_factors = [] + + if len(factors_a) < len(factors_b): #use the shorter list to save run time + for factor in factors_a: + if factor in factors_b: + common_factors.append(factor) + else: + for factor in factors_b: + if factor in factors_a: + common_factors.append(factor) + + return max(common_factors) + +print(find_GCF(24,64)) \ No newline at end of file From 10187c9e21d07d4e998af90dcad4bdf76265e363 Mon Sep 17 00:00:00 2001 From: rohansaxena2020 Date: Sun, 31 Mar 2024 22:45:36 -0400 Subject: [PATCH 9/9] minor tweak --- 8.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/8.py b/8.py index 836b400..0648268 100644 --- a/8.py +++ b/8.py @@ -201,4 +201,5 @@ def find_GCF(a,b): return max(common_factors) -print(find_GCF(24,64)) \ No newline at end of file +print(find_GCF(24,64)) +print(find_GCF(24,16)) \ No newline at end of file