Print Rhombus in Python with Time Complexity O(n)


Given an odd positive integer n print a rhombus of stars as in the example below

Example:

Input: n = 5
Output:
  *  
 *** 
*****
 *** 
  *  

Explanation:
There are n = 5 lines. Each line contains exactly n = 5 characters.
1st line contains two spaces, one '*' and two spaces.
2nd line contains one space, three '*' and one space.
3rd line contains five stars.
4th line contains one space, three '*' and one space.
5th line contains two spaces, one '*' and two spaces.
Note: Be careful to add the necessary spaces at the end of every line!


Problem Definition

The task is to print a rhombus pattern using stars ('*') for a given odd positive integer n. The rhombus should have n lines, and each line should contain exactly n characters, including spaces.

Input:

An odd positive integer n.

Output:

A rhombus pattern of stars with n lines.

Constraints:

  • n is an odd positive integer.

Example:

Input: n = 5
Output:
  *  
 *** 
*****
 *** 
  *  

Understanding the Problem

The core challenge is to correctly position the stars and spaces to form a rhombus shape. This problem is significant in understanding how to manipulate strings and spaces to create specific patterns, which is a common task in programming and algorithm design.

Common Applications:

  • Pattern generation in console applications.
  • Understanding loops and string manipulation.

Potential Pitfalls:

  • Incorrectly calculating the number of spaces and stars for each line.
  • Not maintaining the exact number of characters per line.

Approach

To solve this problem, we need to determine the number of spaces and stars for each line. The pattern is symmetric, so we can break it down into two parts: the top half and the bottom half.

Naive Solution:

A naive solution would involve manually calculating the number of spaces and stars for each line, but this approach is not scalable for larger values of n.

Optimized Solution:

An optimized solution involves using loops to dynamically calculate the number of spaces and stars for each line. This approach is more efficient and scalable.

Thought Process:

  • For the top half (including the middle line), the number of stars increases by 2 for each subsequent line, starting from 1 star.
  • For the bottom half, the number of stars decreases by 2 for each subsequent line, starting from n - 2 stars.
  • The number of spaces is calculated as the difference between n and the number of stars, divided by 2.

Algorithm

Here is a step-by-step breakdown of the algorithm:

  1. Loop through the first half of the lines (including the middle line).
  2. For each line, calculate the number of spaces and stars.
  3. Print the line with the calculated number of spaces and stars.
  4. Loop through the second half of the lines.
  5. For each line, calculate the number of spaces and stars.
  6. Print the line with the calculated number of spaces and stars.

Code Implementation

def print_rhombus(n):
    # Ensure n is an odd positive integer
    if n % 2 == 0 or n <= 0:
        raise ValueError("n must be an odd positive integer")
    
    # Calculate the middle index
    mid = n // 2
    
    # Loop through each line
    for i in range(n):
        # Calculate the number of stars
        if i <= mid:
            stars = 2 * i + 1
        else:
            stars = 2 * (n - i - 1) + 1
        
        # Calculate the number of spaces
        spaces = (n - stars) // 2
        
        # Print the line with spaces and stars
        print(' ' * spaces + '*' * stars + ' ' * spaces)

# Example usage
print_rhombus(5)

Complexity Analysis

The time complexity of this solution is O(n) because we loop through each of the n lines once. The space complexity is O(1) as we only use a constant amount of extra space for variables.

Edge Cases

Potential edge cases include:

  • n = 1: The smallest odd positive integer.
  • n = 3: The next smallest odd positive integer.

For these cases, the algorithm should still correctly print the rhombus pattern.

Testing

To test the solution comprehensively, consider the following test cases:

  • n = 1
  • n = 3
  • n = 5
  • n = 7

Each test case should be verified to ensure the correct rhombus pattern is printed.

Thinking and Problem-Solving Tips

When approaching such problems:

  • Break down the problem into smaller parts.
  • Identify patterns and symmetries.
  • Use loops and conditionals to dynamically calculate values.
  • Practice similar pattern generation problems to improve your skills.

Conclusion

Understanding how to generate patterns using loops and string manipulation is a fundamental skill in programming. This problem helps reinforce these concepts and provides a basis for tackling more complex pattern generation tasks.

Additional Resources

For further reading and practice: