From efd2b529653e3fc7e420cf296557f97c22d5e19a Mon Sep 17 00:00:00 2001 From: Rhinemann Date: Sat, 17 Jun 2023 21:41:51 +0300 Subject: [PATCH] Initial prototype. --- BasicRegister.py | 24 ++++++++++++++++++++++++ main.py | 23 +++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 BasicRegister.py create mode 100644 main.py diff --git a/BasicRegister.py b/BasicRegister.py new file mode 100644 index 0000000..0e55563 --- /dev/null +++ b/BasicRegister.py @@ -0,0 +1,24 @@ + + +class BasicRegister: + + def __init__(self, memory: list[bool]): + self.memory: list[bool] = memory + + def __str__(self) -> str: + return f"Memory: [{', '.join([str(int(value)) for value in self.memory])}]" + + def reverse(self): + self.memory = [not value for value in self.memory] + + def left_shift(self, steps_shifted: int) -> list[bool]: + self.memory.extend([False] * steps_shifted) + shifted_radices: list[bool] = self.memory[:steps_shifted] + del self.memory[:steps_shifted] + return shifted_radices + + def right_shift(self, steps_shifted: int) -> list[bool]: + self.memory[:0] = [False] * steps_shifted + shifted_radices: list[bool] = self.memory[-steps_shifted:] + del self.memory[-steps_shifted:] + return shifted_radices diff --git a/main.py b/main.py new file mode 100644 index 0000000..7cdde79 --- /dev/null +++ b/main.py @@ -0,0 +1,23 @@ +from BasicRegister import BasicRegister + + +def get_memory(variable_name: str) -> list[bool]: + while True: + input_chars: list[str] = list(input(f"Enter {variable_name}: ")) + + if all(character.isnumeric() and (character == "1" or character == "0") for character in input_chars): + return [True if character == "1" else False for character in input_chars] + else: + print(f"[ERROR] The {variable_name} may contain only 1-s and 0-s!") + + +if __name__ == '__main__': + reg: BasicRegister = BasicRegister(get_memory("memory")) + + print(reg) + + print(reg.left_shift(3)) + print(reg) + + print(reg.right_shift(3)) + print(reg)