Swapped list for decue for BasicRegister's memory to optimise shifting methods for both directions.
Moved get_memory to the end.
This commit is contained in:
parent
198e984e32
commit
e5a88058ed
|
@ -1,3 +1,33 @@
|
|||
from collections import deque
|
||||
|
||||
|
||||
class BasicRegister:
|
||||
"""The BasicRegister represents a hardware register capable of manipulating multiple bits at a time.
|
||||
|
||||
:param list[bool] memory: The bits stored inside the register.
|
||||
"""
|
||||
|
||||
def __init__(self, memory: list[bool]):
|
||||
"""Constructor method"""
|
||||
self.memory: deque[bool] = deque(memory)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"Memory: {[int(value) for value in self.memory]}"
|
||||
|
||||
def reverse(self):
|
||||
self.memory = [not value for value in self.memory]
|
||||
|
||||
def left_shift(self, digit_to_fill: bool = False, steps_shifted: int = 1) -> deque[bool]:
|
||||
self.memory.extend([digit_to_fill] * steps_shifted)
|
||||
shifted_radices: deque[bool] = deque([self.memory.popleft() for _i in range(steps_shifted)])
|
||||
return shifted_radices
|
||||
|
||||
def right_shift(self, digit_to_fill: bool = False, steps_shifted: int = 1) -> deque[bool]:
|
||||
self.memory.extendleft([digit_to_fill] * steps_shifted)
|
||||
shifted_radices: deque[bool] = deque([self.memory.pop() for _i in range(steps_shifted)])
|
||||
return shifted_radices
|
||||
|
||||
|
||||
def get_memory(variable_name: str) -> list[bool]:
|
||||
"""
|
||||
Reads user input to be used as a memory array.
|
||||
|
@ -14,32 +44,3 @@ def get_memory(variable_name: str) -> list[bool]:
|
|||
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!")
|
||||
|
||||
|
||||
class BasicRegister:
|
||||
"""The BasicRegister represents a hardware register capable of manipulating multiple bits at a time.
|
||||
|
||||
:param list[bool] memory: The bits stored inside the register.
|
||||
"""
|
||||
|
||||
def __init__(self, memory: list[bool]):
|
||||
"""Constructor method"""
|
||||
self.memory: list[bool] = memory
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"Memory: {[int(value) for value in self.memory]}"
|
||||
|
||||
def reverse(self):
|
||||
self.memory = [not value for value in self.memory]
|
||||
|
||||
def left_shift(self, digit_to_fill: bool = False, steps_shifted: int = 1) -> list[bool]:
|
||||
self.memory.extend([digit_to_fill] * steps_shifted)
|
||||
shifted_radices: list[bool] = self.memory[:steps_shifted]
|
||||
del self.memory[:steps_shifted]
|
||||
return shifted_radices
|
||||
|
||||
def right_shift(self, digit_to_fill: bool = False, steps_shifted: int = 1) -> list[bool]:
|
||||
self.memory[:0] = [digit_to_fill] * steps_shifted
|
||||
shifted_radices: list[bool] = self.memory[-steps_shifted:]
|
||||
del self.memory[-steps_shifted:]
|
||||
return shifted_radices
|
||||
|
|
Loading…
Reference in New Issue