我需要以有限的宽度有效地存储/打印 6 行符号,并以有效的时间来构建表示和打印。目前,它们存储为对象列表,其中 str 属性是六行六角形。
我尝试过嵌套 for 循环,结果发现我的编码效率消失了。
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
============== ================================================================
==== ================================================ ================== ====
============== ============================ ======== ======== ======== ====
============== ======== ======== ======== ======== ======== ==============
==== ======== ======== ======== ============================ ======== ====
==== ======== ====================================== ======== ======== ====
==== ======== ======== ======== ============================ ====
==== ======== ====================================== ======== ====
============== ============================ ======== ==============
============== ================== ======== ======== ==============
==== ============================ ============================ ====
============== ====================================== ==============
最佳答案
由于该项目只是发出随机的六角星,那么简单的方法是生成六行,每行三个符号。生成相同的 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"
关于python - 如何在有限的宽度内高效地横写6行卦?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56710282/