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

  1. Set the value of frst_ele to 'G'.
  2. Set the value of secnd_ele to '*'.
  3. Set the value of N to 4.
  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.
  5. Flatten the list of tuples using itertools.chain.from_iterable() function and convert it to a string using join().
  6. Remove the last character from the resulting string to avoid having an extra frst_ele character at the end.
  7. 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