How to resolve the algorithm Write language name in 3D ASCII step by step in the Python programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Write language name in 3D ASCII step by step in the Python programming language

Table of Contents

Problem Statement

Write/display a language's name in 3D ASCII.

(We can leave the definition of "3D ASCII" fuzzy, so long as the result is interesting or amusing, not a cheap hack to satisfy the task.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Write language name in 3D ASCII step by step in the Python programming language

Code Explanation:

1. Initializing the Python String py:

  • The code defines a string py representing a text pyramid of the word "PYTHON".
  • '#' characters in py represent pyramid blocks, ' ' (spaces) represent empty spaces, and '\n' (newlines) represent line breaks.

2. Generating Lines for the Pyramid:

  • The string py is modified using string manipulation techniques to create a list of lines for the pyramid:
    • replace is used to replace '#' with '<<<', ' ' with 'X', 'X' with ' ', and '\n' with ' Y'.
    • split is used to divide the resulting string into lines by splitting on 'Y'.

3. Printing the Lines in a Pyramid Shape:

  • A for loop iterates through the lines in reverse order (from bottom to top).
  • For each line, it prints leading spaces to align the pyramid correctly.
  • The line is then printed with the appropriate white spaces and characters.

4. Character Table for 3D Letters:

  • Four character sets are defined, each representing a different row of a 3D letter.
  • The character sets are combined into a single charTable using a list comprehension.

5. Main Function with User Input:

  • The program enters the main function when executed.
  • It prompts the user for text to convert and stores it in text. If no input is provided, it defaults to "PYTHON".

6. Generating the 3D Character Pyramid:

  • For each row in the 3D character table, the program iterates through the characters in text.
  • It determines the character's position in the alphabet and calculates the corresponding beginning and ending string positions in the character table.
  • It then extracts the portion of the character table from the beginning to the ending position and appends it to the output line.

7. Printing the 3D Character Pyramid:

  • The output line is printed for each row in the character table.
  • This creates a 3D pyramid representation of the input text.

8. Using an Online ASCII Art Generator:

  • The program makes an HTTP request to a website that generates ASCII art from text.
  • It specifies the text ("Python") and font ("larry3d") as parameters.
  • The response is parsed to extract the ASCII art, which is then printed on the screen.

Source code in the python programming language

py = '''\
 ####   #   #  #####  #   #   ###   #   #
 #   #   # #     #    #   #  #   #  ##  #
 ####     #      #    #####  #   #  # # #
 #        #      #    #   #  #   #  #  ##
 #        #      #    #   #   ###   #   #'''

lines = py.replace('#', '<<<').replace(' ','X') \
        .replace('X', '   ').replace('\n', ' Y') \
        .replace('< ', '<>').split('Y')

for i, l in enumerate(lines): 
    print('  ' * (len(lines) - i) + l)


charWidth = 10
charHeight = 8

# char table is split into sets to prevent very long lines...
charSet1 = [
"   ######  /#######    ######  /######   /######## /########   ######  /##   /##",
" /##__  ##| ##__  ## /##__  ##| ##__ ## | ##_____/| ##_____/ /##__  ##| ##  | ##",
"| ##  | ##| ##  | ##| ##  \__/| ##  \ ##| ##      | ##      | ##  \__/| ##  | ##",
"| ########| ####### | ##      | ##  | ##| ########| ########| ## #####| ########",
"| ##__  ##| ##__  ##| ##      | ##  | ##| ##_____/| ##_____/| ##|_  ##| ##__  ##",
"| ##  | ##| ##  | ##| ##   /##| ##  /##/| ##      | ##      | ##  | ##| ##  | ##",
"| ##  | ##| #######/|  ######/| ######/ | ########| ##      |  ######/| ##  | ##",
"|__/  |__/|_______/  \______/ |______/  |________/|__/       \______/ |__/  |__/",
]

charSet2 = [
" /########       /## /##   /## /##       /###  ### /##   /##   ######  /####### ",
"|__  ##__/      | ##| ##  /##/| ##      | ########| ### | ## /##__  ##| ##__  ##",
"   | ##         | ##| ## /##/ | ##      | ## ## ##| ####| ##| ##  | ##| ##  | ##",
"   | ##         | ##| #####/  | ##      | ## ## ##| ## ## ##| ##  | ##| #######/",
"   | ##         | ##| ##  ##  | ##      | ## ## ##| ##  ####| ##  | ##| ##____/ ",
"   | ##    /##  | ##| ##\  ## | ##      | ##__/ ##| ##\  ###| ##  | ##| ##      ",
" /########\  ######/| ## \  ##| ########| ##  | ##| ## \  ##|  ######/| ##      ",
"|________/ \______/ |__/  \__/|________/|__/  |__/|__/  \__/ \______/ |__/      ",
]

charSet3 = [
"   ######  /#######    ######  /######## /##   /## /##   /## /##   /## /##   /##",
" /##__  ##| ##__  ## /##__  ##|__  ##__/| ##  | ##| ##  | ##| ##  | ##\  ## /##/",
"| ##  | ##| ##  | ##| ##  \__/   | ##   | ##  | ##| ##  | ##| ## ## ## \  ####/ ",
"| ##  | ##| #######/ \ ######    | ##   | ##  | ##| ##  | ##| ## ## ##  \  ##/  ",
"| ## ## ##| ##  ##    \___  ##   | ##   | ##  | ##|  ##  ##/| ## ## ##  / ####  ",
"| ##\ ###/| ##\  ##  /##  \ ##   | ##   | ##  | ## \  ####/ | ######## / ##  ## ",
"|  #### ##| ## \  ##\  ######/   | ##   |  ######/  \  ##/  | ###| ###/ ## \  ##",
" \____\__/|__/  \__/ \______/    |__/    \______/    \__/   |___/|___/\__/  \__/",
]

charSet4 = [
" /##   /## /########   ######           ",
"\  ## /##/|____  ##/ /##__  ##          ",
" \  ####/     / ##/ | ##  | ##          ",
"  \  ##/     / ##/  |__//####/          ",
"   | ##     / ##/      | ##_/           ",
"   | ##    / ##/       |__/             ",
"   | ##   / ########    /##             ",
"   |__/   \________/   |__/             ",
]

# ...then the sets are combined back by barbequing them together!
charTable = [(charSet1[i] +
              charSet2[i] +
              charSet3[i] +
              charSet4[i]) for i in range(charHeight)]

if __name__ == '__main__':
    text = input("Enter the text to convert:\n")
    if not text: 
        text = "PYTHON"

    for i in range(charHeight):
        lineOut = ""
        for chr in text:
            # get value of character 'chr' in alphabet
            if chr.isalpha():
                val = ord(chr.upper()) - 65
            elif chr == " ":
                val= 27
            else:
                val = 26
            beginStr = val * charWidth   # begin string position of 3D letter
            endStr = (val + 1) * charWidth   # end string position of 3D letter
            lineOut += charTable[i][beginStr:endStr]
        print(lineOut)


import requests
import html

text = "Python"
font = "larry3d"
url = f"http://www.network-science.de/ascii/ascii.php?TEXT={text}&FONT={font}&RICH=no&FORM=left&WIDT=1000"

r = requests.get(url)
r.raise_for_status()

ascii_text = html.unescape(r.text)
pre_ascii = "<TD><PRE>"
post_ascii = "\n</PRE>"
ascii_text = ascii_text[ascii_text.index(pre_ascii) + len(pre_ascii):]
ascii_text = ascii_text[:ascii_text.index(post_ascii)]

print(ascii_text)


  

You may also check:How to resolve the algorithm Loops/Downward for step by step in the Ada programming language
You may also check:How to resolve the algorithm Percolation/Site percolation step by step in the Racket programming language
You may also check:How to resolve the algorithm Delete a file step by step in the BASIC programming language
You may also check:How to resolve the algorithm Named parameters step by step in the XSLT programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the R programming language