1.

What could be the shortest as well as easiest approach in solving the given pattern? (See the attachment)Language: Python. Explain your answer.​

Answer»

\underline{\underline{\maltese\:\:\textbf{\textsf{Question}}}}

  • What could be the shortest as well as easiest APPROACH in solving the given PATTERN ? \sf{(R}\mathfrak{efer\:\: in\:\: Attachment - 1)}

\underline{\underline{\maltese\:\:\textbf{\textsf{Conditions}}}}

  • Need Shortest and Easiest Approach in Solving the Given Pattern
  • Language to be USED : Python

\underline{\underline{\maltese\:\:\textbf{\textsf{Algorithm}}}}

\tt{1.} Read INPUT(n).  

\tt{2.} find spaces using formula : 2*n-2

\tt{3.} for i in range(0, n), do:  

  •    print spaces and the string.
  •    If i%2
  •        then c = A
  •        ELSE c = B

\tt{4.}str=c+' '+str+' '+c

\tt{5.} spaces-= 2

\underline{\underline{\maltese\:\:\textbf{\textsf{Required Program}}}}

n = int(input("Enter the number of rows:"))

spaces = 2 * n - 2

str = 'A'

for i in range(n):

  • print(spaces*' '+str)
  • c = 'A' if i % 2 else 'B'
  • str = c+' '+str+' '+c
  • spaces-= 2

print("[Program finished]")

\underline{\underline{\maltese\:\:\textbf{\textsf{Out Put}}}}

  • For the Output of the above Program, \sf{(R}\mathfrak{efer\:\: in\:\: Attachment - 2)}      


Discussion

No Comment Found