Given start and end of a range, write a Python program to print all positive numbers in given range.
Example:
Input: start = -4, end = 5 Output: 0, 1, 2, 3, 4, 5 Input: start = -3, end = 4 Output: 0, 1, 2, 3, 4
Example #1: Print all positive numbers from given list using for loop Define start and end limit of range. Iterate from start till the range in the list using for loop and check if num is greater than or equal to 0. If the condition satisfies, then only print the number.
Python3
# Python program to print positive Numbers in given range start, end = - 4 , 19 # iterating each number in list for num in range (start, end + 1 ): # checking condition if num > = 0 : print (num, end = " " ) |
Output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Time Complexity: O(N)
Here N is value of range, i.e. (end – start).
Auxiliary Space: O(1)
As constant extra space is used.
Example #2: Taking range limit from user input
Python3
# Python program to print positive Numbers in given range start = int ( input ( "Enter the start of range: " )) end = int ( input ( "Enter the end of range: " )) # iterating each number in list for num in range (start, end + 1 ): # checking condition if num > = 0 : print (num, end = " " ) |
Output:
Enter the start of range: -215 Enter the end of range: 5 0 1 2 3 4 5
Time Complexity: O(N)
Here N is value of range, i.e. (end – start).
Auxiliary Space: O(1)
As constant extra space is used.
Method: Using the lambda function
Python3
# Python code To print all positive numbers # in a given range using the lambda function a = - 4 ;b = 5 li = [] for i in range (a,b + 1 ): li.append(i) # printing positive numbers using the lambda function positive_num = list ( filter ( lambda x: (x> = 0 ),li)) print (positive_num) |
[0, 1, 2, 3, 4, 5]
Time Complexity: O(N)
Here N is value of range, i.e. (end – start).
Auxiliary Space: O(1)
As constant extra space is used.
Method: Using the list comprehension
Python3
# Python code # To print all positive numbers in a given range a = - 4 ;b = 5 out = [i for i in range (a,b + 1 ) if i> 0 ] # print the all positive numbers print ( * out) |
Output
[0, 1, 2, 3, 4, 5]
Time Complexity: O(N)
Here N is value of range, i.e. (end – start).
Auxiliary Space: O(1)
As constant extra space is used.
Method: Using enumerate function
Python3
a = - 4 ;b = 5 ;l = [] for i in range (a,b + 1 ): l.append(i) print ([a for j,a in enumerate (l) if a> = 0 ]) |
[0, 1, 2, 3, 4, 5]
Time Complexity: O(N)
Here N is value of range, i.e. (end – start).
Auxiliary Space: O(1)
As constant extra space is used.
Method: Using pass()
Python3
a = - 4 ;b = 5 for i in range (a,b + 1 ): if i< 0 : pass else : print (i,end = " " ) |
0 1 2 3 4 5
Time Complexity: O(N)
Here N is value of range, i.e. (end – start).
Auxiliary Space: O(1)
As constant extra space is used.
Method: Using recursion
Python3
def printPositives(start,end): #defining recursive function to print positives if start = = end: return #base condition if start> = 0 : #check for positive number print (start,end = ' ' ) printPositives(start + 1 ,end) # recursive calling a,b = - 5 , 10 printPositives(a,b) #function calling |
0 1 2 3 4 5 6 7 8 9
Time Complexity: O(N)
Here N is value of range, i.e. (end – start).
Auxiliary Space: O(N)
As we call recursively N extra space is used.
Method : Using filter() function:
- The range() function produces a range of integers from a to b (inclusive).
- The filter() function is used with a lambda function as its first argument and the range as its second argument. The lambda function returns True for any integer that is greater than or equal to 0.
- The filter() function returns an iterator, which is converted to a list using the list() function.
- The resulting list contains only the positive integers within the range [a, b], which are printed to the console using the print() function.
So, the overall approach is to use the range() function to generate a range of integers, filter out the negative integers using the filter() function with a lambda function, and convert the resulting iterator to a list using the list() function.
Python3
a = - 4 b = 5 positive_nums = list ( filter ( lambda x: x > = 0 , range (a, b + 1 ))) print (positive_nums) |
[0, 1, 2, 3, 4, 5]
Time Complexity: O(N) as we are iterating to the range between a and b.
Space Complexity: O(N) as list() function creates a new list with O(n) space complexity.