'Print Diamond shape - Solution in Recursion [closed]

  • Each line has size 2n+2

  • The top line begins with n+2 spaces, followed by the uppercase 'o' character, followed by +2 spaces

  • Each lines from 1 up to 1 in the top half starts with (+2)-1 spaces followed by an uppercase character followed by i-1 vertical bar

The last +2 lines are a reflection of the first +2 lines

 def Diamond_pattern(n):
        n1=(2*n+5)/2
        n1=int(n1)
         
  # This is responsible to print first half of the diamond     
 #Outer loop 
    for i in range(0, n1): 
        k=((2*n+5)/2)-i;
        k=int(k)
    # This Inner loop is used to print number of space  
        for j in range(0, k):  
            print(end=" ")  
          
        # This inner loop is used to print characters based on condition  
        for j in range(0, 2*i + 1):
            if j==0 or j==2*i:
                print("O",end="")
            elif j==int((2*i+1)/2):
                print("o",end="")
            else:
                print("|", end="")  
        print("")  # This line will change the flow to next line
        
    # This is responsible to print the middle of the two triangles
    for i in range(2*n+5):
        if i==int((2*n+5)/2):
            print("o",end="")
        elif i==0 or i==2*n+4:
            print("O",end="")
        else:
            print("|",end="")
    print("") # This line will change the flow to next line
  
    # Second half Triangle of the pyramid 
     
    n2=int((2*n+5)/2)
    # Output for downward triangle pyramid  
    for i in range(n2):  
        # inner loop will print the spaces 
        k=i+1;
        for j in range(k):  
            print(end=" ")  
         
        n3=((2*n+5)-(2*k)) 
        # This inner loop is used to print characters based on condition   
        for j in range(n3):
            if j==n3-1:
                print("O",end="")
            elif j==int(n3/2):
                print("o",end="")
            elif j==0 or j==int(n3-1):
                print("O",end="")
            else:
                print("|",end="")
            
              
        print("")  

This is the Solution to the program

Diamond_pattern(5)

       O
      OoO
     O|o|O
    O||o||O
   O|||o|||O
  O||||o||||O
 O|||||o|||||O
O||||||o||||||O
 O|||||o|||||O
  O||||o||||O
   O|||o|||O
    O||o||O
     O|o|O
      OoO
       O

If anybody can provide me the solution in recursion, please help. The length of the diamond is (2*n + 5). I am looking for an accurate solution, it will help me a lot.



Solution 1:[1]

The recursive solution below prints a single row of the diamond at each call. The format of the row is determined by a running count that the recursion keeps in order to track both the number of | to add as well as the orientation of the diamond (up or down):

def display_offset(s, r):
   #helper function to display a row of the diamond with proper whitespace padding
   t_w = 2*(r+1)+3
   print(f'{(v:=" "*(int((t_w - len(s))/2)))}{s}{v}')

def Diamond_pattern(n):
   def display_diamond(n, m, f = True):
      if not n:
         display_offset('O' if f else 'OoO', m)
         display_offset('OoO' if f else 'O', m)
      else:
         display_offset(f'O{(l:="|"*(n))}o{l}O', m)
      if n == m and f:
         display_offset(f'O{(l:="|"*(m+1))}o{l}O', m)
      if (n <= m and f) or (not f and n):
         display_diamond(n + (1 if f and n < m else (-1 if not f else 0)), m, True if n < m and f else False)
   display_diamond(0, n)
      

Diamond_pattern(5)

Output:

       O       
      OoO      
     O|o|O     
    O||o||O    
   O|||o|||O   
  O||||o||||O  
 O|||||o|||||O 
O||||||o||||||O
 O|||||o|||||O 
  O||||o||||O  
   O|||o|||O   
    O||o||O    
     O|o|O     
      OoO      
       O

Solution 2:[2]

If we agree that this shorter iterative solution produces the same diamond:

def diamond_pattern(n):
    print(" " * (n + 2), "O")

    for repeat in range(n + 2):
        print(" " * (n + 2 - repeat), "O", "|" * repeat, 'o', "|" * repeat, "O", sep='')

    for repeat in range(n, -1, -1):
        print(" " * (n + 2 - repeat), "O", "|" * repeat, 'o', "|" * repeat, "O", sep='')

    print(" " * (n + 2), "O")

diamond_pattern(5)

Then from that I can derive a recursive solution of the form:

def diamond_pattern(n, m=-1):
    if m == -1 or n == -1:  # base case(s)
        print(" " * (max(n + 1, m) + 2), "O")

        if m == -1:
            diamond_pattern(n + 1, 0)
    elif m >= n:  # shrinking diamond
        print(" " * (m + 2 - n), "O", "|" * n, 'o', "|" * n, "O", sep='')
        diamond_pattern(n - 1, m)
    else:  # expanding diamond
        print(" " * (n + 2 - m), "O", "|" * m, 'o', "|" * m, "O", sep='')
        diamond_pattern(n, m + 1)

diamond_pattern(5)

OUTPUT

% python3 test.py
         O
        OoO
       O|o|O
      O||o||O
     O|||o|||O
    O||||o||||O
   O|||||o|||||O
  O||||||o||||||O
   O|||||o|||||O
    O||||o||||O
     O|||o|||O
      O||o||O
       O|o|O
        OoO
         O
% 

When the diamond is expanding, we count up on m. When we reach the middle and m == n, we count down on n.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ajax1234
Solution 2 cdlane