From 9baccf0a60b35624e92aa9ceb911828b569619fa Mon Sep 17 00:00:00 2001 From: kosar Date: Thu, 18 Apr 2024 15:16:47 +0330 Subject: [PATCH 1/6] complete FindMissing & RotateArray Function --- Answers/Find-The-Missing-Number | 12 ++++++++++++ Answers/Rotate-Array | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 Answers/Find-The-Missing-Number create mode 100644 Answers/Rotate-Array diff --git a/Answers/Find-The-Missing-Number b/Answers/Find-The-Missing-Number new file mode 100644 index 0000000..8bd42d9 --- /dev/null +++ b/Answers/Find-The-Missing-Number @@ -0,0 +1,12 @@ + +def find_missing_number(Array): + n = len(Array) + sum = n * (n + 1) // 2 + sumOfElementArray = 0 + for element in Array: + sumOfElementArray = sumOfElementArray + element + missingNumber = sum - sumOfElementArray + return missingNumber + +array = [0 , 1 , 2 , 3 , 5] +print(find_missing_number(array)) \ No newline at end of file diff --git a/Answers/Rotate-Array b/Answers/Rotate-Array new file mode 100644 index 0000000..19bdb84 --- /dev/null +++ b/Answers/Rotate-Array @@ -0,0 +1,20 @@ +def rotate_array(arr, k): + n = len(arr) + k = k % n + rotated_arr = [0] * n + for i in range(k): + rotated_arr[i] = arr[n-k+i] + + for i in range(k, n): + rotated_arr[i] = arr[i-k] + + return rotated_arr + +array = [1, 2, 3, 4, 5] +k = 2 +print(rotate_array(array, k)) + + + + + \ No newline at end of file From 2938c2653cd2477f1f895bdbab9788f708cef652 Mon Sep 17 00:00:00 2001 From: kosar Date: Thu, 18 Apr 2024 15:30:45 +0330 Subject: [PATCH 2/6] complete RemoveDuplicates function --- Answers/Remove_Duplicates.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Answers/Remove_Duplicates.py diff --git a/Answers/Remove_Duplicates.py b/Answers/Remove_Duplicates.py new file mode 100644 index 0000000..272649a --- /dev/null +++ b/Answers/Remove_Duplicates.py @@ -0,0 +1,9 @@ +def RemoveDuplicates(Array): + lenOfArray=0 + for i in range(0 , len(Array)): + if(Array[i]!= Array[i-1]): + lenOfArray = lenOfArray+1 + return lenOfArray + +array = [1 , 1, 2 , 2 , 3, 4 , 5, 5] +print(RemoveDuplicates(array)) \ No newline at end of file From ec64f8d4f501c79c119a4de722d529cd295ea827 Mon Sep 17 00:00:00 2001 From: kosar Date: Thu, 18 Apr 2024 16:48:58 +0330 Subject: [PATCH 3/6] All Function completed --- ...sing-Number => Find-The-Missing-Number.py} | 0 Answers/Maximum Subarray Sum.py | 16 ++++++++++ Answers/Merge-Sorted-Array.py | 29 +++++++++++++++++++ Answers/Rotate-Array.py | 20 +++++++++++++ Answers/tempCodeRunnerFile.py | 16 ++++++++++ 5 files changed, 81 insertions(+) rename Answers/{Find-The-Missing-Number => Find-The-Missing-Number.py} (100%) create mode 100644 Answers/Maximum Subarray Sum.py create mode 100644 Answers/Merge-Sorted-Array.py create mode 100644 Answers/Rotate-Array.py create mode 100644 Answers/tempCodeRunnerFile.py diff --git a/Answers/Find-The-Missing-Number b/Answers/Find-The-Missing-Number.py similarity index 100% rename from Answers/Find-The-Missing-Number rename to Answers/Find-The-Missing-Number.py diff --git a/Answers/Maximum Subarray Sum.py b/Answers/Maximum Subarray Sum.py new file mode 100644 index 0000000..b3aacf0 --- /dev/null +++ b/Answers/Maximum Subarray Sum.py @@ -0,0 +1,16 @@ +def MaximumSubarraySum(Array): + n=len(Array) + Max=Array[0] + for i in range(0,n): + j=i+1 + s=Array[i] + while(j < n): + if ( s >= Max ): + Max=s + s = s + Array[j] + j=j+1 + + return Max + +array = [-2, -1, -3, 4, -1, 2, 1, -5, 4] +print(MaximumSubarraySum(array)) diff --git a/Answers/Merge-Sorted-Array.py b/Answers/Merge-Sorted-Array.py new file mode 100644 index 0000000..2ba6a59 --- /dev/null +++ b/Answers/Merge-Sorted-Array.py @@ -0,0 +1,29 @@ +# def MergeSortedArray(Array): +def merge_sorted_arrays(array1, array2): + counter1=len(array1) - 1 + counter2=len(array2) - 1 + counterMerge =len(array1) + len(array2) - 1 + + array1.extend([0] * len(array2)) + + while counter1 >= 0 and counter2 >= 0: + + if array1[counter1] > array2[counter2]: + array1[counterMerge] = array1[counter1] + counter1 -= 1 + else: + array1[counterMerge] = array2[counter2] + counter2 -= 1 + counterMerge -= 1 + + while counter2 >= 0: + array1[counterMerge] = array2[counter2] + counter2 -= 1 + counterMerge -= 1 + + return array1 + +Array1 = [1, 2, 3] +Array2 = [2, 5, 6] +merge_sorted_arrays(Array1, Array2) +print(Array1) \ No newline at end of file diff --git a/Answers/Rotate-Array.py b/Answers/Rotate-Array.py new file mode 100644 index 0000000..19bdb84 --- /dev/null +++ b/Answers/Rotate-Array.py @@ -0,0 +1,20 @@ +def rotate_array(arr, k): + n = len(arr) + k = k % n + rotated_arr = [0] * n + for i in range(k): + rotated_arr[i] = arr[n-k+i] + + for i in range(k, n): + rotated_arr[i] = arr[i-k] + + return rotated_arr + +array = [1, 2, 3, 4, 5] +k = 2 +print(rotate_array(array, k)) + + + + + \ No newline at end of file diff --git a/Answers/tempCodeRunnerFile.py b/Answers/tempCodeRunnerFile.py new file mode 100644 index 0000000..f4686eb --- /dev/null +++ b/Answers/tempCodeRunnerFile.py @@ -0,0 +1,16 @@ +def MaximumSubarraySum(Array): +# n=len(Array) +# Max=Array[0] +# for i in range(0 ,n): +# j=i+1 +# s=Array[i] +# while(j < n): +# if ( s >= Max ): +# Max=s +# s = s + Array[j] +# j=j+1 + +# return Max + +# array = [-2, -1, -3, 4, -1, 2, 1, -5, 4] +# print(MaximumSubarraySum(array)) \ No newline at end of file From bc77b16625022ba6bdc783246e4e92de2a187cc9 Mon Sep 17 00:00:00 2001 From: kosar Date: Thu, 18 Apr 2024 17:27:30 +0330 Subject: [PATCH 4/6] fix some problem --- Answers/Find-The-Missing-Number.py | 1 - Answers/Maximum Subarray Sum.py | 9 ++++++--- Answers/Remove_Duplicates.py | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Answers/Find-The-Missing-Number.py b/Answers/Find-The-Missing-Number.py index 8bd42d9..029ccf4 100644 --- a/Answers/Find-The-Missing-Number.py +++ b/Answers/Find-The-Missing-Number.py @@ -1,4 +1,3 @@ - def find_missing_number(Array): n = len(Array) sum = n * (n + 1) // 2 diff --git a/Answers/Maximum Subarray Sum.py b/Answers/Maximum Subarray Sum.py index b3aacf0..7683942 100644 --- a/Answers/Maximum Subarray Sum.py +++ b/Answers/Maximum Subarray Sum.py @@ -2,15 +2,18 @@ def MaximumSubarraySum(Array): n=len(Array) Max=Array[0] for i in range(0,n): - j=i+1 s=Array[i] + if ( s >= Max ): + Max=s + j=i+1 while(j < n): + s = s + Array[j] if ( s >= Max ): Max=s - s = s + Array[j] j=j+1 return Max -array = [-2, -1, -3, 4, -1, 2, 1, -5, 4] +# array = [-2, -1, -3, 4, -1, 2, 1, -5, 4] +array=[1 ,5 ,-1 , -50 , 3 , 4 ,7 , -5] print(MaximumSubarraySum(array)) diff --git a/Answers/Remove_Duplicates.py b/Answers/Remove_Duplicates.py index 272649a..a3f6b54 100644 --- a/Answers/Remove_Duplicates.py +++ b/Answers/Remove_Duplicates.py @@ -2,7 +2,7 @@ def RemoveDuplicates(Array): lenOfArray=0 for i in range(0 , len(Array)): if(Array[i]!= Array[i-1]): - lenOfArray = lenOfArray+1 + lenOfArray +=1 return lenOfArray array = [1 , 1, 2 , 2 , 3, 4 , 5, 5] From e7e53d4da4aba0b620acf7b89296d63270d8c528 Mon Sep 17 00:00:00 2001 From: kosar Date: Thu, 18 Apr 2024 17:29:01 +0330 Subject: [PATCH 5/6] completed all functions --- Answers/Rotate-Array | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 Answers/Rotate-Array diff --git a/Answers/Rotate-Array b/Answers/Rotate-Array deleted file mode 100644 index 19bdb84..0000000 --- a/Answers/Rotate-Array +++ /dev/null @@ -1,20 +0,0 @@ -def rotate_array(arr, k): - n = len(arr) - k = k % n - rotated_arr = [0] * n - for i in range(k): - rotated_arr[i] = arr[n-k+i] - - for i in range(k, n): - rotated_arr[i] = arr[i-k] - - return rotated_arr - -array = [1, 2, 3, 4, 5] -k = 2 -print(rotate_array(array, k)) - - - - - \ No newline at end of file From f88647c2bddaa4837a37a3cb202dd26102ccdd00 Mon Sep 17 00:00:00 2001 From: kosar Date: Thu, 18 Apr 2024 17:32:02 +0330 Subject: [PATCH 6/6] ... --- {Answers => 40130112056}/Find-The-Missing-Number.py | 0 {Answers => 40130112056}/How To.txt | 0 {Answers => 40130112056}/Maximum Subarray Sum.py | 0 {Answers => 40130112056}/Merge-Sorted-Array.py | 0 {Answers => 40130112056}/Remove_Duplicates.py | 0 {Answers => 40130112056}/Rotate-Array.py | 0 {Answers => 40130112056}/tempCodeRunnerFile.py | 0 7 files changed, 0 insertions(+), 0 deletions(-) rename {Answers => 40130112056}/Find-The-Missing-Number.py (100%) rename {Answers => 40130112056}/How To.txt (100%) rename {Answers => 40130112056}/Maximum Subarray Sum.py (100%) rename {Answers => 40130112056}/Merge-Sorted-Array.py (100%) rename {Answers => 40130112056}/Remove_Duplicates.py (100%) rename {Answers => 40130112056}/Rotate-Array.py (100%) rename {Answers => 40130112056}/tempCodeRunnerFile.py (100%) diff --git a/Answers/Find-The-Missing-Number.py b/40130112056/Find-The-Missing-Number.py similarity index 100% rename from Answers/Find-The-Missing-Number.py rename to 40130112056/Find-The-Missing-Number.py diff --git a/Answers/How To.txt b/40130112056/How To.txt similarity index 100% rename from Answers/How To.txt rename to 40130112056/How To.txt diff --git a/Answers/Maximum Subarray Sum.py b/40130112056/Maximum Subarray Sum.py similarity index 100% rename from Answers/Maximum Subarray Sum.py rename to 40130112056/Maximum Subarray Sum.py diff --git a/Answers/Merge-Sorted-Array.py b/40130112056/Merge-Sorted-Array.py similarity index 100% rename from Answers/Merge-Sorted-Array.py rename to 40130112056/Merge-Sorted-Array.py diff --git a/Answers/Remove_Duplicates.py b/40130112056/Remove_Duplicates.py similarity index 100% rename from Answers/Remove_Duplicates.py rename to 40130112056/Remove_Duplicates.py diff --git a/Answers/Rotate-Array.py b/40130112056/Rotate-Array.py similarity index 100% rename from Answers/Rotate-Array.py rename to 40130112056/Rotate-Array.py diff --git a/Answers/tempCodeRunnerFile.py b/40130112056/tempCodeRunnerFile.py similarity index 100% rename from Answers/tempCodeRunnerFile.py rename to 40130112056/tempCodeRunnerFile.py