Python | Counting sign change in list containing Positive and Negative Integers
Given a list containing Positive and Negative integers, We have to find number of times the sign(Positive or Negative) changes in the list.
Input: [-1, 2, 3, -4, 5, -6, 7, 8, -9, 10, -11, 12] Output:9 Explanation : Sign change from -1 to 2, ans = 1 Sign change from 3 to -4, ans = 2 Sign change from -4 to 5, ans = 3 Sign change from 5 to -6, ans = 4 Sign change from -6 to 7, ans = 5 Sign change from 8 to -9, ans = 6 Sign change from -9 to 10, ans = 7 Sign change from 10 to -11 ans = 8 Sign change from -11 to 12, ans = 9 Input: [-1, 2, 3, -4, 5, -11, 12] Output:5 Explanation : Sign change from -1 to 2, ans = 1 Sign change from 3 to -4, ans = 2 Sign change from -4 to 5, ans = 3 Sign change from 5 to -11, ans = 4 Sign change from -11 to 12, ans = 5
Let’s discuss certain ways in which this task is performed.
Method #1: Using Iteration
Using Iteration to find number of time sign changes in the list.
# Python Code to find number of # time sign changes in the list. # Input list Initialization Input = [-1, 2, 3, -4, 5, -6, 7, 8, -9, 10, -11, 12] # Variable Initialization prev = Input[0] ans = 0 # Using Iteration for elem in Input: if elem == 0: sign = -1 else: sign = elem / abs(elem) if sign == -prev: ans = ans + 1 prev = sign # Printing answer print(ans) |
Output :
9
Method #2: Using Itertools and groupby
This is yet another way to perform this particular task using itertools.
# Python Code to find number of # time sign changes in the list. # Input list Initialization Input = [-1, 2, 3, -4, 5, -6, 7, 8, -9, 10, -11, 12] # Importing import itertools # Using groupby Output = len(list(itertools.groupby(Input, lambda Input: Input > 0))) Output = Output -1 # Printing output print(Output) |
Output :
9
Method #3: Using Zip
The most concise and readable way to find number of time sign changes in the list is using zip.
# Python Code to find number of # time sign changes in the list. # Using zip to check def check(Input): Input = [-1 if not x else x for x in Input] # zip with leading 1, so that opening negative value is # treated as sign change return sum((x ^ y)<0 for x, y in zip([1]+Input, Input)) # Input list Initialization Input = [-1, 2, 3, -4, 5, -6, 7, 8, -9, 10, -11, 12] Output = check(Input) Output = Output -1 # Printing output print(Output) |
Output :
9
Recommended Posts:
- Python PIL | BoxBlur() method
- Python | sympy.ones() method
- Python | sympy.zeros() method
- Python | sympy.eye() method
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2
- Time Functions in Python | Set-2 (Date Manipulations)
- Send mail from your Gmail account using Python
- Python – The new generation Language
- Print Single and Multiple variable in Python
- Increment and Decrement Operators in Python
- str() vs repr() in Python
- Swap two variables in one line in C/C++, Python, PHP and Java
- Generate all permutation of a set in Python
- Class or Static Variables in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



