Postingan lainnya
np.set_printoptions(current_printoptions) typeError : precision must be an integer
Ada yang bisa kasih masukan supaya hasilnya tidak error? saya juga sudah mencoba memodifnya tapi tidak menemukan titik terangnya, kesalahan pada np.set_printoptions(current_printoptions) . Terimakasih
import numpy as np
class RubikCube:
def __init__(self,size):
self.size = size
self.A = np.zeros((size,size), dtype = np.int8)
self.B = np.zeros((size,size), dtype = np.int8)
self.C = np.zeros((size,size), dtype = np.int8)
self.D = np.zeros((size,size), dtype = np.int8)
self.E = np.zeros((size,size), dtype = np.int8)
self.F = np.zeros((size,size), dtype = np.int8)
self.faces = [self.A, self.B, self.C, self.D, self.E, self.F]
def __str__(self):
current_printoptions = np.get_printoptions()
np.set_printoptions(formatter={'str': '{: 3d}'.format})
output = ""
indent = " " * (1 + self.size * 4)
for row in self.A:
output += indent + str(row) + "\n"
for zipped_row in zip(self.B, self.C, self.D, self.E):
for row in zipped_row:
output += str(row)
output += "\n"
for row in self.F:
output += indent + str(row) + "\n"
np.set_printoptions(current_printoptions)
return output
def init_arange(self):
i = 0
for face in self.faces:
for y in range(self.size):
for x in range(self.size):
face[y][x] = i
i += 1
def init_plainteks(self, teks):
i = 0
for face in self.faces:
for y in range(self.size):
for x in range(self.size):
if len(teks)>i:
face[y][x] = ord(teks[i])
else:
face[y][x] = 37
i += 1
def encrypt(self,key):
for move in key.split('-'):
if move[0] == 'R':
self.rotate_row(int(move[1]))
if move[0] == 'C':
self.rotate_column(int(move[1]))
if move[0] == 'L':
self.rotate_level(int(move[1]))
def decrypt(self,key):
rev_moves = key.split('-')
new_moves = list()
for i in range(1, len(rev_moves) + 1):
new_moves.append(rev_moves[-1 * i])
for move in new_moves:
if move[0] == 'R':
self.rev_rotate_row(int(move[1]))
if move[0] == 'C':
self.rev_rotate_column(int(move[1]))
if move[0] == 'L':
self.rev_rotate_level(int(move[1]))
def rotate_row(self, n):
# B -> C -> D -> E -> B
if n in (1,2,3):
for i in range(n):
tmpB = np.copy(self.B[-1])
self.B[-1] = self.E[-1]
self.E[-1] = self.D[-1]
self.D[-1] = self.C[-1]
self.C[-1] = tmpB
# rotate F by - 90*n
self.F = np.rot90(self.F, 4-n)
elif n in (4,5,6):
for i in range(n-3):
tmpB = np.copy(selef.B[:1])
self.B[:1] = self.E[:1]
self.E[:1] = self.D[:1]
self.D[:1] = self.C[:1]
self.C[:1] = tmpB
# rotate A by 90 derajat*n
self.A = np.rot90(self.A, n-3)
def rev_rotate_row(self,n):
# B -> C -> D -> E -> B
if n in (1,2,3):
for i in range(n):
tmpB = np.copy(self.B[-1])
self.B[-1] = self.C[-1]
self.C[-1] = self.D[-1]
self.D[-1] = self.E[-1]
self.E[-1] = tmpB
# rotate F by -90*n
self.F = np.rot90(self.F, n-4)
elif n in (4,5,6):
for i in range(n-3):
tmpB = np.copy(self.B[-1])
self.B[:1] = self.C[:1]
self.C[:1] = self.D[:1]
self.D[:1] = self.E[:1]
self.E[:1] = tmpB
# rotate A by 90*n
self.A = np.rot90(self.A, 3-n)
def rotate_column(self, n):
# A -> E -> F -> C -> A
if n in (1,2,3):
for i in range(n):
tmpA = np.copy(self.A[:,-1])
self.A[:,-1] = self.C[:,-1]
self.C[:,-1] = self.F[:,-1]
self.F[:,-1] = np.flip(self.E[:,0])
self.E[:,0] = np.flip(tmpA)
# rotate D by -90*n
self.D = np.rot90(self.D, 4-n)
elif n in (4,5,6):
for i in range(n-3):
tmpA = np.copy(self.A[:,:1])
self.A[:,:1] = self.C[:,:1]
self.C[:,:1] = self.F[:,:1]
self.F[:,:1] = np.flip(self.E[:,-1:])
self.E[:,-1:] = np.flip(tmpA)
# rotate B by 90*n
self.B = np.rot90(self.B, n-3)
def rev_rotate_column(self,n):
# A -> E -> F -> C -> A
if n in (1,2,3):
for i in range(n):
tmpA = np.copy(self.A[:,-1])
self.A[:,-1] = self.E[:,-1]
self.E[:,-1] = self.F[:,-1]
self.F[:,-1] = np.flip(self.C[:,0])
self.C[:,0] = np.flip(tmpA)
# rotate D by -90*n
self.D = np.rot90(self.D, 4-n)
elif n in (4,5,6):
for i in range(n-3):
tmpA = np.copy(self.A[:,:1])
self.A[:,:1] = self.E[:,:1]
self.E[:,:1] = self.F[:,:1]
self.F[:,:1] = np.flip(self.C[:,-1:])
self.C[:,-1:] = np.flip(tmpA)
# rotate B by 90*n
self.B = np.rot90(self.B, n-3)
def rotate_level(self,n):
# A -> B -> F -> D -> A
if n in range(n):
for i in range(n):
tmpA = np.copy(self.A[0])
self.A[0] = self.D[:,-1]
self.D[:,-1] = np.flip(self.F[-1])
self.F[-1] = self.B[:,0]
self.B[:,0] = np.flip(tmpA)
# rotate E by -90*n
self.E = np.rot90(self.E, 4-n)
elif n in (4,5,6):
for i in range(n-3):
tmpA = np.copy(self.A[1:])
self.A[-1:] = np.rot90(self.D[:,:1])
self.D[:,:1] = np.rot90(self.F[:1])
self.F[:1] = np.rot90(self.B[:,-1:])
self.B[:,1:] = np.rot90(tmpA)
# rotate C by 90*n
self.C = np.rot90(self.C, n-3)
def rev_rotate_level(self,n):
# A -> B -> F -> D -> A
if n in (1,2,3):
for i in range(n):
tmpA = np.copy(self.A[-1])
self.A[0] = self.B[:,-1]
self.B[:, -1] = np.flip(self.F[-1])
self.F[-1] = self.D[:,0]
self.D[:,0] = np.flip(tmpA)
# rotate E by -90*n
self.E = np.rot90(self.E, n-4)
elif n in (4,5,6):
for i in range(n-3):
tmpA = np.copy(self.A[-1:])
self.A[-1:] = np.rot90(self.B[:,:1])
self.B[:,:1] = np.rot90(self.F[:1])
self.F[:1] = np.rot90(self.D[:,-1:])
self.D[:,-1:] = np.rot90(tmpA)
# rotate C by 90*n
self.C = np.rot90(self.C, 3-n)
if __name__ == "__main__":
msg = input("Message: ")
rubikcube = RubikCube(size=3)
rubikcube.init_plainteks(msg)
# rubikcube.init_arange()
print(rubikcube)
key = input("Key: ")
rubikcube.encrypt(key)
print(rubikcube)
1 Jawaban:
<div>Yang bagian ini, <br>Kenapa harus di set_printoptions lagi akhiran nya? jika pada akhirnya memakai yang<br><br></div><pre>formatter={'str': '{: 3d}'.format}</pre><div> </div><pre>def str(self): current_printoptions = np.get_printoptions() np.set_printoptions(formatter={'str': '{: 3d}'.format})
output = ""
indent = " " * (1 + self.size * 4)
for row in self.A:
output += indent + str(row) + "\n"
for zipped_row in zip(self.B, self.C, self.D, self.E):
for row in zipped_row:
output += str(row)
output += "\n"
for row in self.F:
output += indent + str(row) + "\n"
np.set_printoptions(current_printoptions)
return output</pre><div>Edit =======<br>Oh sorry, kek nya ga ada kaitannya ama error nya rupanya.<br><br>Edit 2 ======<br>Kendala nya itu ada pada Numpy. Entah karena perubahan apa yang terjadi&nbsp;<br>Mengembalikan nilai default set_printoptions menggunakan value sebelum nya terjadi kesalahan/</div><div><br><br>Edit 3 =====</div><pre> current_printoptions = np.get_printoptions()
print(np.get_printoptions())
print(current_printoptions)
np.set_printoptions(formatter={'str': '{: 3d}'.format})
print(np.get_printoptions());
print(current_printoptions);
keluarnya
{'edgeitems': 3, 'threshold': 1000, 'floatmode': 'maxprec', 'precision': 8, 'suppress': False, 'linewidth': 75, 'nanstr': 'nan', 'infstr': 'inf', 'sign': '-', 'formatter': None, 'legacy': False} {'edgeitems': 3, 'threshold': 1000, 'floatmode': 'maxprec', 'precision': 8, 'suppress': False, 'linewidth': 75, 'nanstr': 'nan', 'infstr': 'inf', 'sign': '-', 'formatter': None, 'legacy': False} {'edgeitems': 3, 'threshold': 1000, 'floatmode': 'maxprec', 'precision': 8, 'suppress': False, 'linewidth': 75, 'nanstr': 'nan', 'infstr': 'inf', 'sign': '-', 'formatter': {'str': <built-in method format of str object at 0x0000017C39E67A70>}, 'legacy': False} {'edgeitems': 3, 'threshold': 1000, 'floatmode': 'maxprec', 'precision': 8, 'suppress': False, 'linewidth': 75, 'nanstr': 'nan', 'infstr': 'inf', 'sign': '-', 'formatter': None, 'legacy': False} <br></pre><div><br><br>Jadi dari pada</div><pre>np.set_printoptions(current_printoptions)</pre><div>gunakan ini aja mungkin </div><pre>np.set_printoptions(formatter=None)</pre><div><br><br></div>
Tanggapan
Terimakasih kak, outputnya sudah keluar tapi ga sesuai sama yang manual.
Outputnya kira2 seperti ini kak :
input Message : POIMSQ&P7-D&X?(<3J*Q Key : C1-R4-C6-R6-C3-L1-R1-C4-C3-L4
input Message : POIMSQ&P7-D&X?(<3J*Q Key : C1-R4-C6-R6-C3-L1-R1-C4-C3-L4
https://i.stack.imgur.com/7LMIj.png
Output : 3%%O*%%%%-%%7&Q*%%%M?J%X*(<%%%Q%%%%P%%S%%IP%%&%%D%%%%%
Yg di dalam function init_plaintext nya.. mundurkan jarak yg increment i nya.
Yg baris tmpB = np.copy(selef.B[:1]) ini pun typo 'selef'
Wahh iya kak, terimakasih banyak ya bantuannya