我需要有效地存储/打印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
============== ================================================================
==== ================================================ ================== ====
============== ============================ ======== ======== ======== ====
============== ======== ======== ======== ======== ======== ==============
==== ======== ======== ======== ============================ ======== ====
==== ======== ====================================== ======== ======== ====
==== ======== ======== ======== ============================ ====
==== ======== ====================================== ======== ====
============== ============================ ======== ==============
============== ================== ======== ======== ==============
==== ============================ ============================ ====
============== ====================================== ==============
既然这个项目只是简单地发出随机的卦,那么简单的方法就是生成六行,每行三个符号。生成相同的18个符号,但按生成的顺序打印,每组3个符号后有一个换行符
输出:
你已经做了很多无关的工作来维护你的程序不使用的表示。如果您需要计算生成的三叉树,那么使用底层二进制代码就很容易了。保留每个选定符号的位,并在事实之后将其转换回三元和六元代码:
现在有了
col
作为三个二进制字符串,每个十六进制对应一个。简单地按你的意愿分成三角形;把每一个数字从二进制转换成十进制,你就有了用于分析的索引。对于上面的示例,col
显示为相关问题 更多 >
编程相关推荐