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:

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:

Potential Pitfalls:

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:

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:

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

Testing

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

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

Thinking and Problem-Solving Tips

When approaching such problems:

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: