From 8b22a04e1c322bc8d3078545f9bcbf85f3ae84b7 Mon Sep 17 00:00:00 2001 From: ycchen00 Date: Fri, 17 Mar 2023 21:55:20 +0530 Subject: [PATCH 1/9] Found index --- Find Index.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 Find Index.py diff --git a/Find Index.py b/Find Index.py new file mode 100644 index 000000000..9d87eb54a --- /dev/null +++ b/Find Index.py @@ -0,0 +1,10 @@ +haystack = "sadbutsad" +needle = "sad" + +# Create a list of indices where the needle matches the haystack +indices = [i for i, _ in enumerate(haystack) if haystack[i:i+len(needle)] == needle] + +if indices: + print("The index of the first occurrence of the needle is:", indices[0]) +else: + print("The needle was not found in the haystack.") From 234e5b372e6bd0494e57fe344d965ee83a2ac90b Mon Sep 17 00:00:00 2001 From: ycchen00 Date: Sat, 18 Mar 2023 01:02:26 +0530 Subject: [PATCH 2/9] efficient Nqueens --- Nqueens.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Nqueens.py diff --git a/Nqueens.py b/Nqueens.py new file mode 100644 index 000000000..85a046333 --- /dev/null +++ b/Nqueens.py @@ -0,0 +1,54 @@ +n=int(input('Enter number of queens:')) +def solveNQueens(n): + # Initialize the board as a 2D list of zeros + board = [[0 for _ in range(n)] for _ in range(n)] + + def isSafe(row, col): + # Check if any queens already placed in the same row + for i in range(col): + if board[row][i] == 1: + return False + + # Check if any queens already placed in the same upper diagonal + i, j = row, col + while i >= 0 and j >= 0: + if board[i][j] == 1: + return False + i -= 1 + j -= 1 + + # Check if any queens already placed in the same lower diagonal + i, j = row, col + while i < n and j >= 0: + if board[i][j] == 1: + return False + i += 1 + j -= 1 + + # If no conflicts found, then the position is safe + return True + + def solveHelper(col): + # Base case: If all queens are placed, return True + if col == n: + return True + + # Recursive case: Try placing queen in each row of current column + for row in range(n): + if isSafe(row, col): + board[row][col] = 1 + if solveHelper(col + 1): + return True + board[row][col] = 0 + + # If no safe position found, backtrack + return False + + # Call the helper function to solve N-Queens problem + if solveHelper(0): + # Print the board + for row in board: + print(row) + else: + print("No solution exists for N =", n) +solveNQueens(n) From 27378d5ee81a8ec095306763710c6d780778bc68 Mon Sep 17 00:00:00 2001 From: ycchen00 Date: Sat, 18 Mar 2023 02:11:10 +0530 Subject: [PATCH 3/9] Algorithm added --- Moore's algorithm.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Moore's algorithm.py diff --git a/Moore's algorithm.py b/Moore's algorithm.py new file mode 100644 index 000000000..dd272ca67 --- /dev/null +++ b/Moore's algorithm.py @@ -0,0 +1,34 @@ +def majority_element(nums): + """ + Returns the majority element in a given list using Moore's Voting Algorithm. + + Args: + nums (list): List of integers. + + Returns: + int: Majority element, if it exists. None otherwise. + """ + # initialize variables + candidate = None + count = 0 + + # iterate through list + for num in nums: + if count == 0: + # set new candidate + candidate = num + count += 1 if num == candidate else -1 + + # verify candidate is actually the majority element + if nums.count(candidate) > len(nums) // 2: + return candidate + else: + return None +nums = [2,2,1,1,1,2,2] +print(majority_element(nums)) # output: 2 + +nums = [3,3,4,2,4,4,2,4,4] +print(majority_element(nums)) # output: 4 + +nums = [1,2,3,4,5] +print(majority_element(nums)) # output: None From c9bccea61a384a33c67aac27f482cd4cda0f8144 Mon Sep 17 00:00:00 2001 From: ycchen00 Date: Sat, 18 Mar 2023 02:27:09 +0530 Subject: [PATCH 4/9] found the leaders --- Leaders.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Leaders.py diff --git a/Leaders.py b/Leaders.py new file mode 100644 index 000000000..fe31e31c7 --- /dev/null +++ b/Leaders.py @@ -0,0 +1,17 @@ +def find_leaders(arr): + n = len(arr) + leaders = [arr[n-1]] + + # Traverse the array from right to left + max_so_far = arr[n-1] + for i in range(n-2, -1, -1): + if arr[i] >= max_so_far: + leaders.append(arr[i]) + max_so_far = arr[i] + + return leaders[::-1] # reverse the order to get the leaders in left to right order + +# Example usage +arr = [16, 17, 4, 3, 5, 2] +leaders = find_leaders(arr) +print(leaders) # Output: [17, 5, 2] From 93170448c66fcc71cddbf77a58feea4be05f07b6 Mon Sep 17 00:00:00 2001 From: Saanthosh24 <104246893+Saanthosh24@users.noreply.github.com> Date: Sat, 18 Mar 2023 12:06:49 +0530 Subject: [PATCH 5/9] Delete Find Index.py --- Find Index.py | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 Find Index.py diff --git a/Find Index.py b/Find Index.py deleted file mode 100644 index 9d87eb54a..000000000 --- a/Find Index.py +++ /dev/null @@ -1,10 +0,0 @@ -haystack = "sadbutsad" -needle = "sad" - -# Create a list of indices where the needle matches the haystack -indices = [i for i, _ in enumerate(haystack) if haystack[i:i+len(needle)] == needle] - -if indices: - print("The index of the first occurrence of the needle is:", indices[0]) -else: - print("The needle was not found in the haystack.") From df2afd10f6e63fe224786d3ff914691c7e0519db Mon Sep 17 00:00:00 2001 From: Saanthosh24 <104246893+Saanthosh24@users.noreply.github.com> Date: Sat, 18 Mar 2023 12:07:07 +0530 Subject: [PATCH 6/9] Delete Nqueens.py --- Nqueens.py | 54 ------------------------------------------------------ 1 file changed, 54 deletions(-) delete mode 100644 Nqueens.py diff --git a/Nqueens.py b/Nqueens.py deleted file mode 100644 index 85a046333..000000000 --- a/Nqueens.py +++ /dev/null @@ -1,54 +0,0 @@ -n=int(input('Enter number of queens:')) -def solveNQueens(n): - # Initialize the board as a 2D list of zeros - board = [[0 for _ in range(n)] for _ in range(n)] - - def isSafe(row, col): - # Check if any queens already placed in the same row - for i in range(col): - if board[row][i] == 1: - return False - - # Check if any queens already placed in the same upper diagonal - i, j = row, col - while i >= 0 and j >= 0: - if board[i][j] == 1: - return False - i -= 1 - j -= 1 - - # Check if any queens already placed in the same lower diagonal - i, j = row, col - while i < n and j >= 0: - if board[i][j] == 1: - return False - i += 1 - j -= 1 - - # If no conflicts found, then the position is safe - return True - - def solveHelper(col): - # Base case: If all queens are placed, return True - if col == n: - return True - - # Recursive case: Try placing queen in each row of current column - for row in range(n): - if isSafe(row, col): - board[row][col] = 1 - if solveHelper(col + 1): - return True - board[row][col] = 0 - - # If no safe position found, backtrack - return False - - # Call the helper function to solve N-Queens problem - if solveHelper(0): - # Print the board - for row in board: - print(row) - else: - print("No solution exists for N =", n) -solveNQueens(n) From ad14b893884be8226cef86f75562b5358d22f727 Mon Sep 17 00:00:00 2001 From: Saanthosh24 <104246893+Saanthosh24@users.noreply.github.com> Date: Sat, 18 Mar 2023 12:07:22 +0530 Subject: [PATCH 7/9] Delete Leaders.py --- Leaders.py | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 Leaders.py diff --git a/Leaders.py b/Leaders.py deleted file mode 100644 index fe31e31c7..000000000 --- a/Leaders.py +++ /dev/null @@ -1,17 +0,0 @@ -def find_leaders(arr): - n = len(arr) - leaders = [arr[n-1]] - - # Traverse the array from right to left - max_so_far = arr[n-1] - for i in range(n-2, -1, -1): - if arr[i] >= max_so_far: - leaders.append(arr[i]) - max_so_far = arr[i] - - return leaders[::-1] # reverse the order to get the leaders in left to right order - -# Example usage -arr = [16, 17, 4, 3, 5, 2] -leaders = find_leaders(arr) -print(leaders) # Output: [17, 5, 2] From 36921e1bdb44b3d8ed2694d366bd788e4f8ac3d7 Mon Sep 17 00:00:00 2001 From: Saanthosh24 <104246893+Saanthosh24@users.noreply.github.com> Date: Sat, 18 Mar 2023 12:07:38 +0530 Subject: [PATCH 8/9] Delete Moore's algorithm.py --- Moore's algorithm.py | 34 ---------------------------------- 1 file changed, 34 deletions(-) delete mode 100644 Moore's algorithm.py diff --git a/Moore's algorithm.py b/Moore's algorithm.py deleted file mode 100644 index dd272ca67..000000000 --- a/Moore's algorithm.py +++ /dev/null @@ -1,34 +0,0 @@ -def majority_element(nums): - """ - Returns the majority element in a given list using Moore's Voting Algorithm. - - Args: - nums (list): List of integers. - - Returns: - int: Majority element, if it exists. None otherwise. - """ - # initialize variables - candidate = None - count = 0 - - # iterate through list - for num in nums: - if count == 0: - # set new candidate - candidate = num - count += 1 if num == candidate else -1 - - # verify candidate is actually the majority element - if nums.count(candidate) > len(nums) // 2: - return candidate - else: - return None -nums = [2,2,1,1,1,2,2] -print(majority_element(nums)) # output: 2 - -nums = [3,3,4,2,4,4,2,4,4] -print(majority_element(nums)) # output: 4 - -nums = [1,2,3,4,5] -print(majority_element(nums)) # output: None From e5d398f7c2e40816563753dbbcf623a1baaadf89 Mon Sep 17 00:00:00 2001 From: Saanthosh24 <104246893+Saanthosh24@users.noreply.github.com> Date: Sat, 18 Mar 2023 12:08:21 +0530 Subject: [PATCH 9/9] Add files via upload --- Equilibrium.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Equilibrium.py diff --git a/Equilibrium.py b/Equilibrium.py new file mode 100644 index 000000000..72a2db7c9 --- /dev/null +++ b/Equilibrium.py @@ -0,0 +1,13 @@ +def equilibrium_point(arr): + n = len(arr) + total_sum = sum(arr) + left_sum = 0 + for i in range(n): + if left_sum == total_sum - arr[i] - left_sum: + return i + left_sum += arr[i] + return -1 +a=int(input("Number of elements in the array:-")) +arr=list(map(int, input("elements of array:-").strip().split())) +equilibrium_index = equilibrium_point(arr) +print(f"Equilibrium index is {equilibrium_index}") \ No newline at end of file