Python Iterative Pair Pattern (original) (raw)
Last Updated : 01 Jun, 2023
Sometimes, while working with Python, we can have problem in which we need to perform the pattern construction or iterative pair string in which second element keeps increasing. This kind of problem can have application in day-day programming and school programming. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop
This is brute force way in which this task can be performed. In this, we manually check for the second element and perform an increment on each iteration and store.
Python3 `
Python3 code to demonstrate working of
Iterative Pair Pattern
Using loop
initializing 1st element
frst_ele = 'G'
initializing 2nd element
secnd_ele = '*'
initializing N
N = 4
Iterative Pair Pattern
Using loop
res = frst_ele + secnd_ele for idx in range(1, N): res += frst_ele + secnd_ele * (idx + 1)
printing result
print("The constructed pattern is : " + str(res))
`
Output:
The constructed pattern is : GGGG****
Method #2 : Using join() + generator expression
The combination of above methods can be used to perform this task. In this, we perform the task of increment and pattern construction in one liner logic in generator expression.
Python3 `
Python3 code to demonstrate working of
Iterative Pair Pattern
Using join() + generator expression
initializing 1st element
frst_ele = 'G'
initializing 2nd element
secnd_ele = '*'
initializing N
N = 4
Iterative Pair Pattern
Using join() + generator expression
res = frst_ele.join(secnd_ele * idx for idx in range(N + 1))
printing result
print("The constructed pattern is : " + str(res))
`
Output:
The constructed pattern is : GGGG****
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Method #3:Using itertools
- Set the value of frst_ele to 'G'.
- Set the value of secnd_ele to '*'.
- Set the value of N to 4.
- Create a list of tuples with each tuple containing a string of secnd_ele characters of length i and frst_ele character. Iterate over the range of numbers from 0 to N+1, using the range number to set the length of the secnd_ele string.
- Flatten the list of tuples using itertools.chain.from_iterable() function and convert it to a string using join().
- Remove the last character from the resulting string to avoid having an extra frst_ele character at the end.
- Print the final string. Python3 `
import itertools
frst_ele = 'G' secnd_ele = '*' N = 4
res = ''.join(list(itertools.chain.from_iterable((secnd_ele*i, frst_ele) for i in range(N+1))))
res = res[0:len(res)-1]
print("The constructed pattern is : " + str(res)) #This code is contributed by vinay Pinjala.
`
Output:
The constructed pattern is : GGGG****
Time complexity: O(N), where N is the length of the output string. This is because the algorithm simply iterates over the range of N, and performs constant-time operations (string concatenation and list slicing) on each iteration.
Space complexity: O(N), where N is the length of the output string. This is because the algorithm creates a list of length N to hold the intermediate string parts, and then joins these parts together into a final string of length N. In the worst case, if the output string is very large, the algorithm will require O(N) space to store the intermediate and final strings.
Similar Reads
- Python - Cross Pattern Pairs in List Given two lists, pair them diagonally, in cross pattern [works for even and equi-length lists]. Input : test_list1 = [4, 5, 6, 2], test_list2 = [9, 1, 4, 7] Output : [(4, 1), (5, 9), (6, 7), (2, 4)] Explanation : Crosslinked diagonally, 4->1, 5->9. Input : test_list1 = [4, 5], test_list2 = [9, 6 min read
- Python - Pair iteration in list Pair iteration involves accessing consecutive or specific pairs of elements from a list. It is a common task particularly in scenarios such as comparing neighboring elements, creating tuple pairs, or analyzing sequential data. Python provides several ways to iterate through pairs efficiently ranging 2 min read
- Python - Interlist Perfect Square Pairs Sometimes, while working in Mathematics domain, we can have a problem in which we need to check if product of elements from different lists can lead to perfect square or not. This can have application in many domains including mathematics and web development. Lets discuss certain ways in which this 5 min read
- Python | Pair Product combinations Sometimes, while working with data, we can have a problem in which we need to perform tuple multiplication among all the tuples in list. This can have application in many domains. Let’s discuss certain ways in which this task can be performed. Method #1 : Using combinations() + list comprehension Th 4 min read
- Python - Extract Paired Rows Given a Matrix, the task is to write a Python program to extract all the rows which have paired elements. i.e elements have a frequency of mod 2. Input : test_list = [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2]] Output : [[5, 5, 4, 7, 7, 4], [1, 1, 2, 2]] Explanation : All rows have p 4 min read
- Python | Increasing alternate element pattern in list This particular article solves a very specific issue in which we need to insert every alternate element as the increased size pattern of repeated element to form a pattern. This can have a utility in demonstrative projects. Let's discuss certain ways in which this can be done. Method #1 : Using list 7 min read
- Python | Consecutive Pair Minimums Sometimes, while working with Python list, one can have a problem in which one needs to find perform the minimum of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Let’s discuss certain ways in which this problem can be solved. 4 min read
- Python - Product of consecutive pairs in list Sometimes, while working with Python list, one can have a problem in which one needs to find perform the product of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Let’s discuss certain ways in which this problem can be solved. 7 min read
- Python | Triplet iteration in List List iteration is common in programming, but sometimes one requires to print the elements in consecutive triplets. This particular problem is quite common and having a solution to it always turns out to be handy. Lets discuss certain way in which this problem can be solved. Method #1 : Using list co 6 min read
- Python | Custom Consecutive Character Pairing Sometimes, while working with Python Strings, we can have problem in which we need to perform the pairing of consecutive strings with deliminator. This can have application in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using join() + list comprehension T 4 min read