Shaivi Connect

Day 8 β€” Pattern Problems β€” Practice & Quiz

πŸ“ Practice Problems

1. Easy β€” Right-angled Star Triangle

Given n, print a right-angled triangle of stars with n rows.

Input: n (integer)
Sample Output (n=4):
*
**
***
****
        
Use a nested loop where the inner loop prints i stars for row i.

2. Easy β€” Floyd’s Triangle

Print Floyd's triangle for n rows starting from 1.

Sample Output (n=4):
1
2 3
4 5 6
7 8 9 10
        
Maintain a counter that increments each time you print a number.

3. Easy β€” Inverted Right Triangle of Numbers

Print an inverted right triangle of numbers starting from 1 to the current row length.

Sample Output (n=4):
1234
123
12
1
        
Outer loop decreases from n to 1, inner loop runs from 1 to i.

4. Medium β€” Centered Pyramid of Numbers

Print a pyramid of numbers with each row showing 1 to row number, centered with spaces.

Sample Output (n=4):
   1
  1 2
 1 2 3
1 2 3 4
        
Use string center alignment or space padding before the numbers.

5. Medium β€” Diamond of Stars

Print a diamond of stars with a total of (2n - 1) rows.

Sample Output (n=3):
  *
 ***
*****
 ***
  *
        
First build the upper pyramid, then the inverted one.

6. Medium β€” Numeric Palindrome Pyramid

Create a numeric pyramid where numbers increase and then decrease symmetrically.

Sample Output (n=5):
1
121
12321
1234321
123454321
        
Use one loop to build ascending numbers, then another for descending.

7. Medium β€” Hollow Diamond

Print a hollow diamond pattern of stars for given odd size.

Print stars only at the borders and spaces inside.

8. Hard β€” Zig-Zag / Wave Pattern

Print a zig-zag pattern across k columns and n rows.

Alternate upward and downward diagonals.

9. Hard β€” Pascal’s Triangle

Generate Pascal's triangle formatted as centered numbers for n rows.

Use the formula C(n, k) = n! / (k! * (n-k)!).

10. Hard β€” Alternating 0s and 1s Pyramid

Print a pyramid pattern where each row alternates between 0 and 1.

Use (i + j) % 2 to decide whether to print 0 or 1.

🧠 Quick Quiz

  1. What is the typical time complexity of most pattern problems?
  2. Which method can be used to center a string in Python?
  3. True/False: Nested loops are always required for pattern problems.
  4. What does `str.join()` do?
  5. Which operator helps in alternating 0s and 1s in a pattern?

πŸ“₯ Download Solutions