From 7e3fdbe5cb36d9209ba60a62de48995bf561ec21 Mon Sep 17 00:00:00 2001 From: Rishabh Aggarwal <31319270+altruistcoder@users.noreply.github.com> Date: Wed, 11 Dec 2024 00:05:25 +0530 Subject: [PATCH 1/3] Added the code for minimu swapping --- README.md | 4 ++++ minimum_swapping.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 minimum_swapping.py diff --git a/README.md b/README.md index abd044d..96df174 100644 --- a/README.md +++ b/README.md @@ -208,3 +208,7 @@ This is a repository containing various python programs to understand the basic Python Code for solving a popular problem of the Pavement of Roads to connect to Libraries in the City/Cities. +* [Minimum Swapping Problem](https://github.com/altruistcoder/Data-Structures-Python/blob/master/minimum_swapping.py): + + Python Code for finding the minimum number of swapping of numbers required to sort the array given as input. + diff --git a/minimum_swapping.py b/minimum_swapping.py new file mode 100644 index 0000000..f2bda76 --- /dev/null +++ b/minimum_swapping.py @@ -0,0 +1,20 @@ +def minSwapping(a, n): + res = 0 + temp = a.copy() + ihash = {} + temp.sort() + for i in range(n): + ihash[a[i]] = i + start = 0 + for i in range(n): + if (a[i] != temp[i]): + res += 1 + start = a[i] + a[i], a[ihash[temp[i]]] = a[ihash[temp[i]]], a[i] + ihash[start] = ihash[temp[i]] + ihash[temp[i]] = i + return res + +a = list(map(int, input("Enter the elements of the input array: ").split())) +res = minSwapping(a, len(a)) +print("The minimum number of steps required for sorting:", res) \ No newline at end of file From 99ae78f118bb2dc7df896e4fa50877986b2147c3 Mon Sep 17 00:00:00 2001 From: Rishabh Aggarwal <31319270+altruistcoder@users.noreply.github.com> Date: Mon, 10 Feb 2025 23:59:20 +0530 Subject: [PATCH 2/3] Added a new program --- max_consecutive_ones_zeroes.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 max_consecutive_ones_zeroes.py diff --git a/max_consecutive_ones_zeroes.py b/max_consecutive_ones_zeroes.py new file mode 100644 index 0000000..95b82b9 --- /dev/null +++ b/max_consecutive_ones_zeroes.py @@ -0,0 +1,16 @@ +def getMaxLength(a, n): + + c = 0 + res = 0 + + for i in range(0, n): + if (arr[i] == 0): + c = 0 + else: + c+= 1 + res = max(res, c) + return res + +a = list(map(int, input("Enter a list of elements: ").split())) +n = len(a) +print(getMaxLength(a, n)) From f06bd76046f85396255b9df39fd6aa50d97f2842 Mon Sep 17 00:00:00 2001 From: Rishabh Aggarwal <31319270+altruistcoder@users.noreply.github.com> Date: Tue, 11 Feb 2025 00:55:26 +0530 Subject: [PATCH 3/3] Added a python program to find consecutive ones in an array of ones and zeroes --- README.md | 3 +++ max_consecutive_ones_zeroes.py | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 96df174..f7fda78 100644 --- a/README.md +++ b/README.md @@ -212,3 +212,6 @@ This is a repository containing various python programs to understand the basic Python Code for finding the minimum number of swapping of numbers required to sort the array given as input. +* [Consecutive Ones in Zeroes Ones Array Problem](https://github.com/altruistcoder/Data-Structures-Python/blob/master/max_consecutive_ones_zeroes.py): + + Python Code for finding the count of maximum number of consecutive 1's present in an array consisting of a 1's and 0's. \ No newline at end of file diff --git a/max_consecutive_ones_zeroes.py b/max_consecutive_ones_zeroes.py index 95b82b9..f59b5b7 100644 --- a/max_consecutive_ones_zeroes.py +++ b/max_consecutive_ones_zeroes.py @@ -1,10 +1,9 @@ -def getMaxLength(a, n): - +def getMaxConsecutiveOne(a, n): c = 0 res = 0 for i in range(0, n): - if (arr[i] == 0): + if (a[i] == 0): c = 0 else: c+= 1 @@ -13,4 +12,5 @@ def getMaxLength(a, n): a = list(map(int, input("Enter a list of elements: ").split())) n = len(a) -print(getMaxLength(a, n)) +maxOnes = getMaxConsecutiveOne(a, n) +print("The maximum number of consecutive ones in the array is:", maxOnes)