Wednesday, September 25, 2024
Google search engine
HomeLanguagesBackward iteration in Python

Backward iteration in Python

The iteration of numbers is done by looping techniques in Python. There are many techniques in Python which facilitate looping. Sometimes we require to perform the looping backward and having short hands to do so can be quite useful. Let’s discuss certain ways in which this can be done.

Method #1 : Using reversed() The simplest way to perform this is to use the reversed function for the for loop and the iteration will start occurring from the rear side than the conventional counting. 

Python3




# Python3 code to demonstrate
# backward iteration
# using reversed()
 
# Initializing number from which
# iteration begins
N = 6
 
# using reversed() to perform the back iteration
print ("The reversed numbers are : ", end = "")
for num in reversed(range(N + 1)) :
    print(num, end = " ")


Output

The reversed numbers are : 6 5 4 3 2 1 0 

Time complexity: O(N) where N is the value of N
Auxiliary space: O(1) as constant space is used for initialization and printing.

Method #2 : Using range(N, -1, -1) This particular task can also be performed using the conventional range function which, if provided with the third argument performs the skip and second argument is used to start from backwards. 

Python3




# Python3 code to demonstrate
# backward iteration
# using range(N, -1, -1)
 
# Initializing number from which
# iteration begins
N = 6
 
# without using reversed() to perform the back iteration
print ("The reversed numbers are : ", end = "")
for num in range(N, -1, -1) :
    print(num, end = " ")


Output

The reversed numbers are : 6 5 4 3 2 1 0 

Time complexity: O(N)
Auxiliary space: O(1)

Method#3: Using slice syntax This particular task can also be performed using the slice syntax which, is used for reversing the list. We used it for reversing the range class in the for loop then we perform the backward iteration.

Python3




# # Python3 code to demonstrate
# # backward iteration
# # using slice syntax
 
# # Initializing number from which
# # iteration begins
N = 6
 
 
# Using slice syntax perform the backward iteration
k = range(N+1)[::-1]
print("The reversed numbers are : ",end='')
for i in k:
    print(i, end=' ')


Output

The reversed numbers are : 6 5 4 3 2 1 0 

Method #4:Using while loop

Python3




# # Python3 code to demonstrate
# # backward iteration
 
# Initializing number from which
# iteration begins
N = 6
print("The reversed numbers are : ", end='')
while(N >= 0):
    print(N, end=' ')
    N -= 1


Output

The reversed numbers are : 6 5 4 3 2 1 0 

METHOD 5:Using the join() function 

APPROACH:

This program performs backward iteration from N to 0 and concatenates the reversed numbers as a string using the join() function.

ALGORITHM:

1.Assign the value of N to a variable N.
2.Use the reversed() function and range() function to generate a reversed range of numbers from N to 0.
3.Use the map() function to convert each number in the reversed range to a string.
4.Use the join() function to concatenate the reversed numbers as a string separated by space.
5.Print the concatenated string of reversed numbers.

Python3




# Python program to perform backward iteration
 
# Given input
N = 6
 
# Concatenating reversed numbers as a string using join() function
reversed_str = ' '.join(map(str, reversed(range(N+1))))
 
# Printing the reversed numbers as a string
print("The reversed numbers are :", reversed_str)


Output

The reversed numbers are : 6 5 4 3 2 1 0

Time complexity: O(N), where N is the value of the given input. The reversed range is generated in O(N) time complexity and converting each number to string using map() function takes O(N) time complexity. The join() function also takes O(N) time complexity to concatenate the reversed numbers as a string.

Auxiliary Space: O(N), where N is the value of the given input. The reversed range of numbers and the concatenated string of reversed numbers occupy O(N) space in memory.

RELATED ARTICLES

Most Popular

Recent Comments