如何有效地写六行六边形水平在一个封顶宽度?

2024-09-29 22:30:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要有效地存储/打印6行符号在一个封顶宽度与有效的时间,以建立表示和打印。当前它们存储为对象列表,其中str属性是一个六行六边形

我尝试了嵌套循环,并看着我的编码效率消失

from enum import Enum
import base64
import random

SOLID = '==========\n'
BROKEN = '====  ====\n'

# Snipped 2D array of hexagrams for lookup table called HEXAGRAMS

SORTED_HEXAGRAMS = sorted(sum(HEXAGRAMS, []), key=lambda h: h.value)

def build_hexagram_output(hexagrams):
    output = "\n"
    for hexagram in hexagrams:
        output += str(hexagram) + "\n"
    return output


def encode(msg, shuffle=False, file=False):
    if shuffle:
        print("Shuffling...")
        shuffled = ''.join(random.sample(B64_CHARACTERS, len(B64_CHARACTERS)))
        print("Key: {}".format(shuffled))
        KEYMAP.update(zip(shuffled, SORTED_HEXAGRAMS))
    else:
        KEYMAP.update(zip(B64_CHARACTERS, SORTED_HEXAGRAMS))
    if file:
        msg = "./" + msg
        with open(msg, 'rb') as f:
            b64_encoded = base64.b64encode(f.read()).decode('utf-8')
    else:
        b64_encoded = base64.b64encode(bytes(msg, encoding='utf-8')).decode('utf-8')
    hexagrams = []
    for letter in b64_encoded.replace('=', ''):
        hexagrams.append(KEYMAP[letter])
    return build_hexagram_output(hexagrams)


class Trigram(Enum):
    HEAVEN = 0
    LAKE = 1
    FIRE = 2
    THUNDER = 3
    WIND = 4
    WATER = 5
    MOUNTAIN = 6
    EARTH = 7

    def __str__(self):
        if self.value == 0:
            return SOLID + SOLID + SOLID
        elif self.value == 1:
            return BROKEN + SOLID + SOLID
        elif self.value == 2:
            return SOLID + BROKEN + SOLID
        elif self.value == 3:
            return BROKEN + BROKEN + SOLID
        elif self.value == 4:
            return SOLID + SOLID + BROKEN
        elif self.value == 5:
            return BROKEN + SOLID + BROKEN
        elif self.value == 6:
            return SOLID + BROKEN + BROKEN
        elif self.value == 7:
            return BROKEN + BROKEN + BROKEN

class Hexagram:
    def __init__(self, upper, lower, value):
        self.upper = upper
        self.lower = lower
        self.value = value

    def __str__(self):
        return str(self.upper) + str(self.lower)

我想要它的电流输出:

====  ====
==========
====  ====
====  ====
====  ====
====  ====

==========
==========
==========
====  ====
====  ====
==========

====  ====
==========
==========
==========
====  ====
==========

水平显示:

====  ==== ========== ====  ====
========== ========== ==========
====  ==== ====  ==== ==========
====  ==== ====  ==== ==========
====  ==== ========== ====  ====
====  ==== ========== ==========

编辑:

感谢Prune给了我正确的答案,让我走上了正确的道路。这些不是随机的,而是一个移位密码-因此在整个消息编码之前,我不一定要构建行/列。这是我最后的解决办法。不是最漂亮的-但它工作得很好,我放弃了一个更模糊的输出空间

def build_hexagram_output(hexagrams):
    output = ""
    lines = [str()] * 6
    for hexagram in hexagrams:
        split_hexagram = str(hexagram).split("\n")
        for i in range(6):
            lines[i] += split_hexagram[i]
    position = 0
    total_position = 0
    while total_position <= len(lines[0]) - 1:
        for line in lines:
            output += line[total_position: total_position + MAX_WIDTH] + "\n"
            if position == 5:
                position = 0
            else:
                position += 1
        total_position += MAX_WIDTH
    return output
02:27:10 [jonesy@yeetbook] iching > python main.py -e ThanksPrune
==============  ================================================================
====  ================================================  ==================  ====
==============  ============================  ========  ========  ========  ====
==============  ========  ========  ========  ========  ========  ==============
====  ========  ========  ========  ============================  ========  ====
====  ========  ======================================  ========  ========  ====
====  ========  ========  ========  ============================  ====
====  ========  ======================================  ========  ====
==============  ============================  ========  ==============
==============  ==================  ========  ========  ==============
====  ============================  ============================  ====
==============  ======================================  ==============


Tags: inselfforoutputreturnvaluedefposition
1条回答
网友
1楼 · 发布于 2024-09-29 22:30:55

既然这个项目只是简单地发出随机的卦,那么简单的方法就是生成六行,每行三个符号。生成相同的18个符号,但按生成的顺序打印,每组3个符号后有一个换行符

import random

for row in range(6):
    line = ""
    for col in range(3):
        line += random.choice(["========== ", "====  ==== "])
    print(line)

输出:

========== ====  ==== ========== 
========== ========== ========== 
====  ==== ====  ==== ====  ==== 
========== ====  ==== ========== 
====  ==== ====  ==== ====  ==== 
========== ========== ====  ==== 

你已经做了很多无关的工作来维护你的程序不使用的表示。如果您需要计算生成的三叉树,那么使用底层二进制代码就很容易了。保留每个选定符号的位,并在事实之后将其转换回三元和六元代码:

symbol = ["========== ", "====  ==== "]
gram = [""] * 6
for row in range(6):
    line = ""
    for col in range(3):
        bar = random.randint(0, 1)
        line += symbol[bar]
        gram[col] += str(bar)
    print(line)

print(col)

现在有了col作为三个二进制字符串,每个十六进制对应一个。简单地按你的意愿分成三角形;把每一个数字从二进制转换成十进制,你就有了用于分析的索引。对于上面的示例,col显示为

"001010" "101110" "001011"

相关问题 更多 >

    热门问题