1 parent
081154012d
commit
405f518d77
2 files changed
+157
No files matched your search
+26
@@ -0,0 +1,26 @@
|
||||
def e1():
|
||||
a = 123
|
||||
b = 543
|
||||
ab = a^b
|
||||
print(f"{a = :b}, {b = :b}\n{ab = :b} ({ab})")
|
||||
|
||||
def e2():
|
||||
a = [105, 110, 116, 114, 111, 50, 99, 114, 121, 112, 116, 111, 123, 119, 51,
|
||||
108, 99, 48, 109, 51, 95, 116, 48, 95, 106, 48, 117, 114, 110, 51, 121, 125]
|
||||
|
||||
print(''.join(map(chr, a)))
|
||||
|
||||
def e3():
|
||||
b = [99, 100, 126, 120, 101, 56, 105, 120, 115, 122, 126, 101, 113, 59, 121,
|
||||
126, 57, 122, 85, 58, 100, 85, 96, 58, 127, 120, 100, 57, 115, 119]
|
||||
|
||||
k = 10
|
||||
|
||||
print(''.join(map(lambda n: chr(n^k), b)))
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
e1()
|
||||
e2()
|
||||
e3()
|
||||
@@ -0,0 +1,131 @@
|
||||
import collections.abc
|
||||
from collections.abc import Sequence, Iterable
|
||||
from typing import Literal
|
||||
from functools import reduce
|
||||
from operator import xor
|
||||
|
||||
|
||||
def maj(a, b, c):
|
||||
a, b, c = bool(a), bool(b), bool(c)
|
||||
return 1 * (a + b + c >= 2)
|
||||
|
||||
|
||||
class Bih(Sequence):
|
||||
def __init__(
|
||||
self, length: Literal[19, 22, 23], initializer: Iterable | None = None
|
||||
):
|
||||
match length:
|
||||
case 19:
|
||||
self.m = 8
|
||||
self.n = (13, 16, 17, 18)
|
||||
self.g = 18
|
||||
case 22:
|
||||
self.m = 10
|
||||
self.n = (20, 21)
|
||||
self.g = 21
|
||||
case 23:
|
||||
self.m = 10
|
||||
self.n = (7, 20, 21, 22)
|
||||
self.g = 22
|
||||
case _:
|
||||
raise ValueError()
|
||||
self.l = length
|
||||
|
||||
if initializer is None:
|
||||
initializer = [0] * length
|
||||
self.q = list(initializer)
|
||||
if len(self.q) != length:
|
||||
raise ValueError()
|
||||
|
||||
def __len__(self):
|
||||
return len(self.q)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.q)
|
||||
|
||||
@property
|
||||
def maj(self):
|
||||
return self[self.m]
|
||||
|
||||
@property
|
||||
def st(self):
|
||||
return self[self.g]
|
||||
|
||||
@property
|
||||
def element_new(self):
|
||||
return tuple(self[n] for n in self.n)
|
||||
|
||||
@property
|
||||
def n_new(self):
|
||||
return reduce(xor, self.element_new)
|
||||
|
||||
def roll(self):
|
||||
assert len(self) == self.l
|
||||
self.q = [self.n_new, *self[:-1]]
|
||||
|
||||
def __getitem__(self, bro):
|
||||
return self.q[bro]
|
||||
|
||||
|
||||
def dihcheese(n: int, l: int):
|
||||
return map(int, f"{n:b}".zfill(l))
|
||||
|
||||
|
||||
def dih(n: int, l: int):
|
||||
return Bih(l, dihcheese(n, l))
|
||||
|
||||
|
||||
def majroll(a: Bih, b: Bih, c: Bih):
|
||||
r = maj(a.maj, b.maj, c.maj)
|
||||
for i in (a, b, c):
|
||||
if i.maj == r:
|
||||
i.roll()
|
||||
|
||||
|
||||
def s(a: Bih, b: Bih, c: Bih):
|
||||
return reduce(xor, (i.st for i in (a, b, c)))
|
||||
|
||||
|
||||
def cih(n):
|
||||
n = [int(i) for i in str(n)]
|
||||
return Bih(len(n), n)
|
||||
|
||||
|
||||
def op(a: Bih, b: Bih, c: Bih, n: int):
|
||||
for i in range(n):
|
||||
majroll(a, b, c)
|
||||
yield s(a, b, c)
|
||||
|
||||
|
||||
def opdih(a, b, c, n):
|
||||
a = dih(a, 19)
|
||||
b = dih(b, 22)
|
||||
c = dih(c, 23)
|
||||
return op(a, b, c, n)
|
||||
|
||||
|
||||
def opcih(a, b, c, n):
|
||||
a = cih(a)
|
||||
b = cih(b)
|
||||
c = cih(c)
|
||||
return op(a, b, c, n)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
a = dih(513365, 19)
|
||||
b = dih(3355443, 22)
|
||||
c = dih(7401712, 23)
|
||||
d = list(op(a, b, c, 10))
|
||||
|
||||
print(d)
|
||||
|
||||
e = list(
|
||||
opcih(
|
||||
1010101010101010101,
|
||||
1100110011001100110011,
|
||||
11100001111000011110000,
|
||||
8,
|
||||
)
|
||||
)
|
||||
print(e)
|
||||
|
||||
Reference in new issue
Block a user