Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using astericks and spaces print the letter R [ 21*20]

I want to print the letter r as shown in image using asterisk and spaces

enter image description here

So far i was only able to do this shape

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

rows = 21  # Height of the letter R
cols = 20  # Width of the letter R

for i in range(rows + 2):  #     Adding 2 for top and bottom border
for j in range(cols + 2):  # Adding 2 for left and right border
    # Print a border of asterisks
    if i == 0 or i == rows + 1 or j == 0 or j == cols + 1:
        print("*", end=" ")
    # Print the letter "R"
    elif j == 1 and (i > 0 and i < rows + 1):  # Left vertical line
        print("*", end=" ")
    elif i == 1 and j < cols:  # Top horizontal line of R
        print("*", end=" ")
    elif i == rows // 2 and j < cols:  # Middle horizontal line of R
        print("*", end=" ")
    elif (i - j == 1) and (i > rows // 2):  # Diagonal line of R
        print("*", end=" ")
    else:
        print(" ", end=" ")  # Space inside the R
print()  # Move to the next line

>Solution :

Given that the letter always alternates a sequence of stars and spaces, you can encode the letter using a list for each line containing the number of stars then, if there are spaces, the number of spaces and number of stars and repeat for each gap:

letter = [
  *[[20]]*3,
  *[[3, 14, 3]]*2, 
  *[[3, 2, 10, 2, 3]]*4, 
  *[[3, 14, 3]]*2, 
  *[[3, 2, 3 + i, 3, 9-i] for i in range(7)],
  *[[20]]*3,
]

Then to generate the output you can join the alternating sequences of stars-spaces-stars together and then join the lines with new line characters:

output = "\n".join(
  "".join((" " if i%2 else "*")*n for i, n in enumerate(r))
  for r in letter
)

Then, if you want to print it:

print(output + "\n")

Which outputs:

********************
********************
********************
***              ***
***              ***
***  **********  ***
***  **********  ***
***  **********  ***
***  **********  ***
***              ***
***              ***
***  ***   *********
***  ****   ********
***  *****   *******
***  ******   ******
***  *******   *****
***  ********   ****
***  *********   ***
********************
********************
********************

If you want spaces between each character then:

output = "\n".join(
  "".join(("  " if i%2 else "* ")*n for i, n in enumerate(r))
  for r in letter
)
    
print(output + "\n")

Which outputs:

* * * * * * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * * * * * 
* * *                             * * * 
* * *                             * * * 
* * *     * * * * * * * * * *     * * * 
* * *     * * * * * * * * * *     * * * 
* * *     * * * * * * * * * *     * * * 
* * *     * * * * * * * * * *     * * * 
* * *                             * * * 
* * *                             * * * 
* * *     * * *       * * * * * * * * * 
* * *     * * * *       * * * * * * * * 
* * *     * * * * *       * * * * * * * 
* * *     * * * * * *       * * * * * * 
* * *     * * * * * * *       * * * * * 
* * *     * * * * * * * *       * * * * 
* * *     * * * * * * * * *       * * * 
* * * * * * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * * * * * 

If you want to use loops rather than str.join() then:

output = ""
for r in letter:
    for i, n in enumerate(r):
        output += (" " if i%2 else "*")*n
    output += "\n"

print(output)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading