我的Python程序将放大的单词以垂直方式打印而不是水平方式。

huangapple go评论62阅读模式
英文:

My python prgram print the enlarged word vertically instead of horizontally

问题

Sure, here's the translation of the part you requested:

当用户输入一个单词时,我的Python程序会用星号放大它。但是,当用户输入一个单词时,它会垂直打印,而不是水平的。

我的问题是 - 如何横向(并排)打印放大的字母,而不是垂直的?

这是我的代码 -

Please note that I've only translated the description and code structure, excluding the code within functions for individual letters.

英文:

When a user enters a word, my Python program enlarges it with asterisks.But, when the user enters a word, it prints vertically instead of horizontally.

My question is - How to pring the enlarged letter horizantally (SIDE BY SIDE) instead of vertically?

Here is my code -

def a():
row1 = [3]
row2 = [2, 4]
row3 = [1, 2, 3, 4, 5]
row4 = [1, 5]
row5 = [1, 5]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def b():
row1 = [1, 2, 3, 4]
row2 = [1, 5]
row3 = [1, 2, 3, 4]
row4 = [1, 5]
row5 = [1, 2, 3, 4]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def c():
row1 = [2, 3, 4, 5]
row2 = [1]
row3 = [1]
row4 = [1]
row5 = [2, 3, 4, 5]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def d():
row1 = [1, 2, 3, 4]
row2 = [1, 5]
row3 = [1, 5]
row4 = [1, 5]
row5 = [1, 2, 3, 4]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def e():
row1 = [1, 2, 3, 4, 5]
row2 = [1]
row3 = [1, 2, 3, 4, 5]
row4 = [1]
row5 = [1, 2, 3, 4, 5]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def f():
row1 = [1, 2, 3, 4, 5]
row2 = [1]
row3 = [1, 2, 3, 4, 5]
row4 = [1]
row5 = [1]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def g():
row1 = [2, 3, 4, 5]
row2 = [1]
row3 = [1, 3, 4, 5]
row4 = [1, 5]
row5 = [2, 3, 4, 5]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def h():
row1 = [1, 5]
row2 = [1, 5]
row3 = [1, 2, 3, 4, 5]
row4 = [1, 5]
row5 = [1, 5]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def i():
row1 = [1, 2, 3, 4, 5]
row2 = [3]
row3 = [3]
row4 = [3]
row5 = [1, 2, 3, 4, 5]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def j():
row1 = [1, 2, 3, 4, 5]
row2 = [3]
row3 = [3]
row4 = [1, 3]
row5 = [2, 3]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def k():
row1 = [1, 4]
row2 = [1, 3]
row3 = [1, 2]
row4 = [1, 3]
row5 = [1, 4]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def l():
row1 = [1]
row2 = [1]
row3 = [1]
row4 = [1]
row5 = [1, 2, 3]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def m():
row1 = [1, 5]
row2 = [1, 2, 4, 5]
row3 = [1, 3, 5]
row4 = [1, 5]
row5 = [1, 5]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def n():
row1 = [1, 5]
row2 = [1, 2, 5]
row3 = [1, 3, 5]
row4 = [1, 4, 5]
row5 = [1, 5]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def o():
row1 = [2, 3, 4]
row2 = [1, 5]
row3 = [1, 5]
row4 = [1, 5]
row5 = [2, 3, 4]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def p():
row1 = [1, 2, 3, 4]
row2 = [1, 5]
row3 = [1, 2, 3, 4]
row4 = [1]
row5 = [1]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def q():
row1 = [3]
row2 = [2, 4]
row3 = [1, 3, 5]
row4 = [2, 4]
row5 = [3, 5]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def r():
row1 = [1, 2, 3, 4]
row2 = [1, 5]
row3 = [1, 2, 3, 4]
row4 = [1, 3]
row5 = [1, 4, 5]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def s():
row1 = [2, 3, 4, 5]
row2 = [1]
row3 = [2, 3, 4]
row4 = [5]
row5 = [1, 2, 3, 4]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def t():
row1 = [1, 2, 3, 4, 5]
row2 = [3]
row3 = [3]
row4 = [3]
row5 = [3]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def u():
row1 = [1, 5]
row2 = [1, 5]
row3 = [1, 5]
row4 = [1, 5]
row5 = [2, 3, 4]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def v():
row1 = [1, 5]
row2 = [1, 5]
row3 = [1, 5]
row4 = [2, 4]
row5 = [3]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def w():
row1 = [1, 5]
row2 = [1, 5]
row3 = [1, 3, 5]
row4 = [1, 2, 4, 5]
row5 = [1, 5]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def x():
row1 = [1, 5]
row2 = [2, 4]
row3 = [3]
row4 = [2, 4]
row5 = [1, 5]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def y():
row1 = [1, 5]
row2 = [2, 4]
row3 = [3]
row4 = [3]
row5 = [3]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
def z():
row1 = [1, 2, 3, 4, 5]
row2 = [4]
row3 = [3]
row4 = [2]
row5 = [1, 2, 3, 4, 5]
coordinates = [row1, row2, row3, row4, row5]
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
print("*", end="")
else:
print(" ", end="")
print("")
word = input('Enter a word - ').lower().strip()
while True:
if not word.isalpha():
print("Sorry, but you need to enter a valid word containing only letters!")
print(" ")
word = input("Enter a word - ").lower().strip()
else:
break
for letter in word:
if letter == "a":
a()
elif letter == "b":
b()
elif letter == "c":
c()
elif letter == "d":
d()
elif letter == "e":
e()
elif letter == "f":
f()
elif letter == "g":
g()
elif letter == "h":
h()
elif letter == "i":
i()
elif letter == "j":
j()
elif letter == "k":
k()
elif letter == "l":
l()
elif letter == "m":
m()
elif letter == "n":
n()
elif letter == "o":
o()
elif letter == "p":
p()
elif letter == "q":
q()
elif letter == "r":
r()
elif letter == "s":
s()
elif letter == "t":
t()
elif letter == "u":
u()
elif letter == "v":
v()
elif letter == "w":
w()
elif letter == "x":
x()
elif letter == "y":
y()
elif letter == "z":
z()

This program prints the word vertically.

here is an example of how it prints -
C:\Users\yaswa\PycharmProjects\pythonProject\venv\Scripts\python.exe "C:\py letters\blah.py"

Enter a word - hi
*   *
*   *
*****
*   *
*   *
*****
*  
*  
*  
*****
Process finished with exit code 0

What I expect -
I expect it to print "HI" side by side.
Like this -

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

答案1

得分: 0

你的代码有太多的冗余,所以我重新编写了它。

alphabets = {
    "a": [
        [3],
        [2, 4],
        [1, 2, 3, 4, 5],
        [1, 5],
        [1, 5]
    ],
    "b": [
        [1, 2, 3, 4],
        [1, 5],
        [1, 2, 3, 4],
        [1, 5],
        [1, 2, 3, 4]
    ],
    # 其他字母的定义...
}

def add_alphabet(coordinates, output):
    for i in range(5):
        x = coordinates[i]
        for j in range(6):
            if j in x:
                output[i].append("*")
            else:
                output[i].append(" ")
    
    return output

word = input('输入一个单词 - ').lower().strip()

while True:
    if not word.isalpha():
        print("抱歉,您需要输入一个只包含字母的有效单词!")
        print(" ")
        word = input("输入一个单词 - ").lower().strip()
    else:
        break

output = [[], [], [], [], []]

for letter in word:
    output = add_alphabet(alphabets[letter], output)

for line in output:
    print(''.join(line))

在这个代码中,我去掉了HTML编码,并进行了一些简化和清理,以使其更加可读。

英文:

Your code was too redundant, so I rewrote it.

alphabets = {
"a" : [
[3],
[2, 4],
[1, 2, 3, 4, 5],
[1, 5],
[1, 5]
],
"b" : [
[1, 2, 3, 4],
[1, 5],
[1, 2, 3, 4],
[1, 5],
[1, 2, 3, 4]
],
"c" : [
[2, 3, 4, 5],
[1],
[1],
[1],
[2, 3, 4, 5]
],
"d" : [
[1, 2, 3, 4],
[1, 5],
[1, 5],
[1, 5],
[1, 2, 3, 4]
],
"e" : [
[1, 2, 3, 4, 5],
[1],
[1, 2, 3, 4, 5],
[1],
[1, 2, 3, 4, 5]
],
"f" : [
[1, 2, 3, 4, 5],
[1],
[1, 2, 3, 4, 5],
[1],
[1]
],
"g" : [
[2, 3, 4, 5],
[1],
[1, 3, 4, 5],
[1, 5],
[2, 3, 4, 5]
],
"h" : [
[1, 5],
[1, 5],
[1, 2, 3, 4, 5],
[1, 5],
[1, 5]
],
"i" : [
[1, 2, 3, 4, 5],
[3],
[3],
[3],
[1, 2, 3, 4, 5]
],
"j" : [
[1, 2, 3, 4, 5],
[3],
[3],
[1, 3],
[2, 3]
],
"k" : [
[1, 4],
[1, 3],
[1, 2],
[1, 3],
[1, 4]
],
"l" : [
[1],
[1],
[1],
[1],
[1, 2, 3]
],
"m" : [
[1, 5],
[1, 2, 4, 5],
[1, 3, 5],
[1, 5],
[1, 5]
],
"n" : [
[1, 5],
[1, 2, 5],
[1, 3, 5],
[1, 4, 5],
[1, 5]
],
"o" : [
[2, 3, 4],
[1, 5],
[1, 5],
[1, 5],
[2, 3, 4]
],
"p" : [
[1, 2, 3, 4],
[1, 5],
[1, 2, 3, 4],
[1],
[1]
],
"q" : [
[3],
[2, 4],
[1, 3, 5],
[2, 4],
[3, 5]
],
"r" : [
[1, 2, 3, 4],
[1, 5],
[1, 2, 3, 4],
[1, 3],
[1, 4, 5]
],
"s" : [
[2, 3, 4, 5],
[1],
[2, 3, 4],
[5],
[1, 2, 3, 4]
],
"t" : [
[1, 2, 3, 4, 5],
[3],
[3],
[3],
[3]
],
"u" : [
[1, 5],
[1, 5],
[1, 5],
[1, 5],
[2, 3, 4]
],
"v" : [
[1, 5],
[1, 5],
[1, 5],
[2, 4],
[3]
],
"w" : [
[1, 5],
[1, 5],
[1, 3, 5],
[1, 2, 4, 5],
[1, 5]
],
"x" : [
[1, 5],
[2, 4],
[3],
[2, 4],
[1, 5]
],
"y" : [
[1, 5],
[2, 4],
[3],
[3],
[3]
],
"z" : [
[1, 2, 3, 4, 5],
[4],
[3],
[2],
[1, 2, 3, 4, 5]
],
}
def add_alphabet(coordinates, output):
for i in range(5):
x = coordinates[i]
for j in range(6):
if j in x:
output[i].append("*")
else:
output[i].append(" ")
return output
word = input('Enter a word - ').lower().strip()
while True:
if not word.isalpha():
print("Sorry, but you need to enter a valid word containing only letters!")
print(" ")
word = input("Enter a word - ").lower().strip()
else:
break
output = [[], [], [], [], []]
for letter in word:
output = add_alphabet(alphabets[letter], output)
for line in output:
print(''.join(line))

In this code, I am keeping each of the 5 lines in a buffer before print, updating it with values from each alphabet from the input and printing at the end.

Output:

Enter a word - Hi
*   * *****
*   *   *  
*****   *  
*   *   *  
*   * *****

答案2

得分: 0

以下是已翻译的代码部分:

相同的想法如其他人所提出的这里的关键概念是为每个字母创建一个值的映射星号应该放在哪里)。

然后您需要构建一个允许您迭代放大字母的每一的列表

类似于这样

LMAP = {
    'a': [[3], [2, 4], [1, 2, 3, 4, 5], [1, 5], [1, 5]],
    'b': [[1, 2, 3, 4], [1, 5], [1, 2, 3, 4], [1, 5], [1, 2, 3, 4]],
    'c': [[2, 3, 4, 5], [1], [1], [1], [2, 3, 4, 5]],
    'd': [[1, 2, 3, 4], [1, 5], [1, 5], [1, 5], [1, 2, 3, 4]],
    'e': [[1, 2, 3, 4, 5], [1], [1, 2, 3, 4, 5], [1], [1, 2, 3, 4, 5]],
    'f': [[1, 2, 3, 4, 5], [1], [1, 2, 3, 4, 5], [1], [1]],
    'g': [[2, 3, 4, 5], [1], [1, 3, 4, 5], [1, 5], [2, 3, 4, 5]],
    'h': [[1, 5], [1, 5], [1, 2, 3, 4, 5], [1, 5], [1, 5]],
    'i': [[1, 2, 3, 4, 5], [3], [3], [3], [1, 2, 3, 4, 5]],
    'j': [[1, 2, 3, 4, 5], [3], [3], [1, 3], [2, 3]],
    'k': [[1, 4], [1, 3], [1, 2], [1, 3], [1, 4]],
    'l': [[1], [1], [1], [1], [1, 2, 3, 4]],
    'm': [[1, 5], [1, 2, 4, 5], [1, 3, 5], [1, 5], [1, 5]],
    'n': [[1, 5], [1, 2, 5], [1, 3, 5], [1, 4, 5], [1, 5]],
    'o': [[2, 3, 4], [1, 5], [1, 5], [1, 5], [2, 3, 4]],
    'p': [[1, 2, 3, 4], [1, 5], [1, 2, 3, 4], [1], [1]],
    'q': [[3], [2, 4], [1, 5], [2, 4], [3, 5]],
    'r': [[1, 2, 3, 4], [1, 5], [1, 2, 3, 4], [1, 3], [1, 4, 5]],
    's': [[2, 3, 4, 5], [1], [2, 3, 4], [5], [1, 2, 3, 4]],
    't': [[1, 2, 3, 4, 5], [3], [3], [3], [3]],
    'u': [[1, 5], [1, 5], [1, 5], [1, 5], [2, 3, 4]],
    'v': [[1, 5], [1, 5], [1, 5], [2, 4], [3]],
    'w': [[1, 5], [1, 5], [1, 3, 5], [1, 2, 4, 5], [1, 5]],
    'x': [[1, 5], [2, 4], [3], [2, 4], [1, 5]],
    'y': [[1, 5], [2, 4], [3], [3], [3]],
    'z': [[1, 2, 3, 4, 5], [4], [3], [2], [1, 2, 3, 4, 5]],
}

def lmap(lst):
    m = []
    for r in lst:
        line = [' ']*5
        for p in r:
            line

= '*' m.append(line) return m word = 'stackoverflow' patterns = [lmap(LMAP[letter]) for letter in word] for row in range(5): print(' '.join(''.join(p[row]) for p in patterns))

输出:

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

希望这有帮助!如果您需要进一步的翻译,请告诉我。

英文:

Same kind of idea as proposed by others. The key concept here is to have a map of values (where the asterisks are to be) for each letter.

Then you need to build a list that allows you to iterate over each "row" of the "enlarged" letters.

Something like this:

LMAP = {
'a': [[3], [2, 4], [1, 2, 3, 4, 5], [1, 5], [1, 5]],
'b': [[1, 2, 3, 4], [1, 5], [1, 2, 3, 4], [1, 5], [1, 2, 3, 4]],
'c': [[2, 3, 4, 5], [1], [1], [1], [2, 3, 4, 5]],
'd': [[1, 2, 3, 4], [1, 5], [1, 5], [1, 5], [1, 2, 3, 4]],
'e': [[1, 2, 3, 4, 5], [1], [1, 2, 3, 4, 5], [1], [1, 2, 3, 4, 5]],
'f': [[1, 2, 3, 4, 5], [1], [1, 2, 3, 4, 5], [1], [1]],
'g': [[2, 3, 4, 5], [1], [1, 3, 4, 5], [1, 5], [2, 3, 4, 5]],
'h': [[1, 5], [1, 5], [1, 2, 3, 4, 5], [1, 5], [1, 5]],
'i': [[1, 2, 3, 4, 5], [3], [3], [3], [1, 2, 3, 4, 5]],
'j': [[1, 2, 3, 4, 5], [3], [3], [1, 3], [2, 3]],
'k': [[1, 4], [1, 3], [1, 2], [1, 3], [1, 4]],
'l': [[1], [1], [1], [1], [1, 2, 3, 4]],
'm': [[1, 5], [1, 2, 4, 5], [1, 3, 5], [1, 5], [1, 5]],
'n': [[1, 5], [1, 2, 5], [1, 3, 5], [1, 4, 5], [1, 5]],
'o': [[2, 3, 4], [1, 5], [1, 5], [1, 5], [2, 3, 4]],
'p': [[1, 2, 3, 4], [1, 5], [1, 2, 3, 4], [1], [1]],
'q': [[3], [2, 4], [1, 5], [2, 4], [3, 5]],
'r': [[1, 2, 3, 4], [1, 5], [1, 2, 3, 4], [1, 3], [1, 4, 5]],
's': [[2, 3, 4, 5], [1], [2, 3, 4], [5], [1, 2, 3, 4]],
't': [[1, 2, 3, 4, 5], [3], [3], [3], [3]],
'u': [[1, 5], [1, 5], [1, 5], [1, 5], [2, 3, 4]],
'v': [[1, 5], [1, 5], [1, 5], [2, 4], [3]],
'w': [[1, 5], [1, 5], [1, 3, 5], [1, 2, 4, 5], [1, 5]],
'x': [[1, 5], [2, 4], [3], [2, 4], [1, 5]],
'y': [[1, 5], [2, 4], [3], [3], [3]],
'z': [[1, 2, 3, 4, 5], [4], [3], [2], [1, 2, 3, 4, 5]],
}
def lmap(lst):
m = []
for r in lst:
line = [' ']*5
for p in r:
line[p-1] = '*'
m.append(line)
return m
word = 'stackoverflow'
patterns = [lmap(LMAP[letter]) for letter in word]
for row in range(5):
print(' '.join(''.join(p[row]) for p in patterns))

Output:

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

答案3

得分: 0

这是您要求的代码的翻译部分:

好吧,“DarkKnight的上面的答案看起来是最优化的但这很有趣我只是想按照您编写的方式修复您的代码以下是修复后的代码仅修复不是优化😁)。

def a():
    row1 = [3]
    row2 = [2, 4]
    row3 = [1, 2, 3, 4, 5]
    row4 = [1, 5]
    row5 = [1, 5]
    return [row1, row2, row3, row4, row5]
    
def b():
    row1 = [1, 2, 3, 4]
    row2 = [1, 5]
    row3 = [1, 2, 3, 4]
    row4 = [1, 5]
    row5 = [1, 2, 3, 4]
    return [row1, row2, row3, row4, row5]
    
# 以下省略其他函数的翻译...

word = "helloworld"
word_split = list(word)
each_char_length = 6
word_length = len(word_split)

full_word_cord_array = []
for each_alphabet in word_split:
    full_word_cord_array.append(get_cordinates_of_char(each_alphabet))

for row in range(5):
    new_cords = []
    for col in range(word_length):
        new_values_array = [(num + (col *  each_char_length)) for num in full_word_cord_array[col][row]]
        new_cords.append(new_values_array)
    flattened_array = [element for sublist in new_cords for element in sublist]
    print_line(flattened_array)

希望这能满足您的要求。如果有其他翻译需求,请告诉我。

英文:

Well, the above answer by "DarkKnight" looks to be the most optimised one, but this was fun, I just wanted to fix your code in the way you have written. Heres the fixed code (just fixed, not optimised 😃).

def a():
row1 = [3]
row2 = [2, 4]
row3 = [1, 2, 3, 4, 5]
row4 = [1, 5]
row5 = [1, 5]
return [row1, row2, row3, row4, row5]
def b():
row1 = [1, 2, 3, 4]
row2 = [1, 5]
row3 = [1, 2, 3, 4]
row4 = [1, 5]
row5 = [1, 2, 3, 4]
return [row1, row2, row3, row4, row5]
def c():
row1 = [2, 3, 4, 5]
row2 = [1]
row3 = [1]
row4 = [1]
row5 = [2, 3, 4, 5]
return [row1, row2, row3, row4, row5]
def d():
row1 = [1, 2, 3, 4]
row2 = [1, 5]
row3 = [1, 5]
row4 = [1, 5]
row5 = [1, 2, 3, 4]
return [row1, row2, row3, row4, row5]
def e():
row1 = [1, 2, 3, 4, 5]
row2 = [1]
row3 = [1, 2, 3, 4, 5]
row4 = [1]
row5 = [1, 2, 3, 4, 5]
return [row1, row2, row3, row4, row5]
def f():
row1 = [1, 2, 3, 4, 5]
row2 = [1]
row3 = [1, 2, 3, 4, 5]
row4 = [1]
row5 = [1]
return [row1, row2, row3, row4, row5]
def g():
row1 = [2, 3, 4, 5]
row2 = [1]
row3 = [1, 3, 4, 5]
row4 = [1, 5]
row5 = [2, 3, 4, 5]
return [row1, row2, row3, row4, row5]
def h():
row1 = [1, 5]
row2 = [1, 5]
row3 = [1, 2, 3, 4, 5]
row4 = [1, 5]
row5 = [1, 5]
return [row1, row2, row3, row4, row5]
def i():
row1 = [1, 2, 3, 4, 5]
row2 = [3]
row3 = [3]
row4 = [3]
row5 = [1, 2, 3, 4, 5]
return [row1, row2, row3, row4, row5]
def j():
row1 = [1, 2, 3, 4, 5]
row2 = [3]
row3 = [3]
row4 = [1, 3]
row5 = [2, 3]
return [row1, row2, row3, row4, row5]
def k():
row1 = [1, 4]
row2 = [1, 3]
row3 = [1, 2]
row4 = [1, 3]
row5 = [1, 4]
return [row1, row2, row3, row4, row5]
def l():
row1 = [1]
row2 = [1]
row3 = [1]
row4 = [1]
row5 = [1, 2, 3]
return [row1, row2, row3, row4, row5]
def m():
row1 = [1, 5]
row2 = [1, 2, 4, 5]
row3 = [1, 3, 5]
row4 = [1, 5]
row5 = [1, 5]
return [row1, row2, row3, row4, row5]
def n():
row1 = [1, 5]
row2 = [1, 2, 5]
row3 = [1, 3, 5]
row4 = [1, 4, 5]
row5 = [1, 5]
return [row1, row2, row3, row4, row5]
def o():
row1 = [2, 3, 4]
row2 = [1, 5]
row3 = [1, 5]
row4 = [1, 5]
row5 = [2, 3, 4]
return [row1, row2, row3, row4, row5]
def p():
row1 = [1, 2, 3, 4]
row2 = [1, 5]
row3 = [1, 2, 3, 4]
row4 = [1]
row5 = [1]
return [row1, row2, row3, row4, row5]
def q():
row1 = [3]
row2 = [2, 4]
row3 = [1, 3, 5]
row4 = [2, 4]
row5 = [3, 5]
return [row1, row2, row3, row4, row5]
def r():
row1 = [1, 2, 3, 4]
row2 = [1, 5]
row3 = [1, 2, 3, 4]
row4 = [1, 3]
row5 = [1, 4, 5]
return [row1, row2, row3, row4, row5]
def s():
row1 = [2, 3, 4, 5]
row2 = [1]
row3 = [2, 3, 4]
row4 = [5]
row5 = [1, 2, 3, 4]
return [row1, row2, row3, row4, row5]
def t():
row1 = [1, 2, 3, 4, 5]
row2 = [3]
row3 = [3]
row4 = [3]
row5 = [3]
return [row1, row2, row3, row4, row5]
def u():
row1 = [1, 5]
row2 = [1, 5]
row3 = [1, 5]
row4 = [1, 5]
row5 = [2, 3, 4]
return [row1, row2, row3, row4, row5]
def v():
row1 = [1, 5]
row2 = [1, 5]
row3 = [1, 5]
row4 = [2, 4]
row5 = [3]
return [row1, row2, row3, row4, row5]
def w():
row1 = [1, 5]
row2 = [1, 5]
row3 = [1, 3, 5]
row4 = [1, 2, 4, 5]
row5 = [1, 5]
return [row1, row2, row3, row4, row5]
def x():
row1 = [1, 5]
row2 = [2, 4]
row3 = [3]
row4 = [2, 4]
row5 = [1, 5]
return [row1, row2, row3, row4, row5]
def y():
row1 = [1, 5]
row2 = [2, 4]
row3 = [3]
row4 = [3]
row5 = [3]
return [row1, row2, row3, row4, row5]
def z():
row1 = [1, 2, 3, 4, 5]
row2 = [4]
row3 = [3]
row4 = [2]
row5 = [1, 2, 3, 4, 5]
return [row1, row2, row3, row4, row5]
def get_cordinates_of_char(char):
match(char):
case 'a' : return a()
case 'b' : return b()
case 'c' : return c()
case 'd' : return d()
case 'e' : return e()
case 'f' : return f()
case 'g' : return g()
case 'h' : return h()
case 'i' : return i()
case 'j' : return j()
case 'k' : return k()
case 'l' : return l()
case 'm' : return m()
case 'n' : return n()
case 'o' : return o()
case 'p' : return p()
case 'q' : return q()
case 'r' : return r()
case 's' : return s()
case 't' : return t()
case 'u' : return u()
case 'v' : return v()
case 'w' : return w()
case 'x' : return x()
case 'y' : return y()
case 'z' : return z()
def print_line(line_cord):
for cord in range(line_cord[-1]+1):
print("*", end="") if cord in line_cord else  print(" ", end="")
print("")
word = "helloworld"
word_split = list(word)
each_char_length = 6
word_length = len(word_split)
full_word_cord_array = []
for each_alphabet in word_split:
full_word_cord_array.append(get_cordinates_of_char(each_alphabet))
for row in range(5):
new_cords = []
for col in range(word_length):
new_values_array = [(num + (col *  each_char_length)) for num in full_word_cord_array[col][row]]
new_cords.append(new_values_array)
flattened_array = [element for sublist in new_cords for element in sublist]
print_line(flattened_array)

huangapple
  • 本文由 发表于 2023年7月5日 00:23:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定