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.
Method #2: Using Itertools and groupby
This is yet another way to perform this particular task using itertools.
Method #3: Using Zip
The most concise and readable way to find number of time sign changes in the list is using zip.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Segregate Positive and Negative Integers from mixed string
- Program to display Astrological sign or Zodiac sign for given date of birth
- Python program to count positive and negative numbers in a list
- Python | Rearrange Positive and Negative Elements
- Lambda expression in Python to rearrange positive and negative numbers
- Python Program to Check Whether a Number is Positive or Negative or zero
- Highlight the negative values red and positive values black in Pandas Dataframe
- Program to check if a number is Positive, Negative, Odd, Even, Zero
- Python | Reverse sign of each element in given list
- Counting the frequencies in a list using dictionary in Python
- Counting number of unique values in a Python list
- Python - Distinct Positive Integers Sum to K
- Permutation of first N positive integers such that prime numbers are at prime indices
- Django Sign Up and login with confirmation Email | Python
- Python | Solve given list containing numbers and arithmetic operators
- numpy.sign() in Python
- Python - tensorflow.math.sign()
- Sign in Geeksforgeeks using Python Selenium
- Python - Group Consecutive elements by Sign
- Python | Sort list containing alphanumeric values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.