سكين سويسري الذراع لأعداد من أنظمة الأرقام المخصصة.
Evgueni Antonov 2023.
تم إنشاء هذه الوحدة واختبارها على Python 3.10.6 ، PIP 23.1.2.
أول مرة نشرت في مايو 2023.
pip3 install custom-numbers
# or
python3 -m pip install custom-numbers
للحصول على تفاصيل حول كيفية استخدامه ، راجع قسم الاستخدام أدناه.
from custom_numbers import custom_numbers as cn
my_custom_numeral_system = cn.CustomNumeralSystem("paf")
my_number = cn.CustomNumber(my_custom_numeral_system, "a")
# Now if you type in REPL:
# help(cn)
# You will get the help for the module and the classes.
تحتوي هذه الحزمة على وحدة واحدة مع عدد قليل من الفئات المصنوعة لمساعدتك على إعلان نظام أرقام مخصص ، مصنوع من الرموز المخصصة مع قاعدة مخصصة. نظرًا لأن هذا قد يبدو غريباً ، في الواقع هذا هو كيف يبدو:
ملاحظة: لا يتم دعم الأنظمة الفرعية ولا أخطط لتنفيذ هذه الميزة.
ملاحظة: تدعم هذه الوحدة أرقام عدد صحيح مخصص فقط. أرقام النقاط العائمة المخصصة غير مدعومة ولن تكون أبدًا.
لنبدأ بشيء تعرفه بالفعل - رقم ثنائي. هذه مصنوعة من الأصفار فقط والأفراد وبالتالي القاعدة 2.
مثال: 1100101
وهذا هو في الواقع عنوان أغنية رائعة حقا من التسعينيات. أيضًا 1100101 ثنائي ، تم تحويله إلى عشري هو 101 (مائة وواحد) ، تمامًا مثل (ثنائي) 10 2 == 2 10 (عشري).
الآن - رقم سداسي عشري - مصنوعة من الأرقام 0 ، 1 ، 2 ، 3 ، 4 ، 5 ، 6 ، 7 ، 8 ، 9 والحروف A ، B ، C ، D ، E ، F ولها قاعدة 16.
لذلك يبدو أن الرقم السداسي عشري هكذا:
F 16 == 15 10 ، 10 16 == 16 10 ، F0 16 == 240 10
نظام الأرقام المعروف الآخر هو نظام الثماني ، الذي يتكون من أرقام فقط من الصفر إلى السبع والقاعدة 8 ، لكننا سنخطي هذا الآن.
وأخيراً ، فإن النظام العشري الذي نعرفه جميعًا منذ الطفولة لديه قاعدة 10 ويصنع من الأرقام من الصفر إلى تسعة.
حسنًا ، حتى الآن أنت معتاد على أنظمة الأرقام الأكثر استخدامًا - الثنائي ، الثمن ، العشري والسداسي.
لنبدأ بإعادة تحديد النظام الثنائي. لذلك لديها قاعدة 2 ولن نغير ذلك. ولكن ماذا عن استخدام 0 و 1 لاستخدام P و A بدلاً من ذلك؟ لذلك سيكون P لدينا Zero New Zero وسيكون A الجديد لدينا. لذلك ، نظرًا لأننا عادة ما نكتب 1100101 ، سيتم كتابة هذا الآن على أنه AAPPAPA . مربك ، أليس كذلك؟ ولكن ماذا - لا يزال نظامًا ثنائيًا ، قمنا بتغيير الرموز.
ولكن الآن ماذا عن شيء أكثر جنونًا - نظام أرقام به قاعدة 3 ، باستخدام الرموز P ، A ، F كرقم ، لذلك سيكون 2 عشريًا ، سيكون P غير عشري. لذلك:
AA 3 == 4 10 ، AF 3 == 5 10 ، FP 3 == 6 10
الآن دعونا نرى هذا في العمل:
from custom_numbers import custom_numbers as cn
sys3 = cn.CustomNumeralSystem("paf")
num3 = cn.CustomNumber(sys3, "aa")
num10 = num3.to_decimal()
print(num10) # Prints "4"
أفضل طريقة لاختبار ما إذا كانت الفصول الدراسية كما هو متوقع هي إعلان أنظمة الأرقام مع الرموز التي نعرفها جميعًا:
from custom_numbers import custom_numbers as cn
sys2 = cn.CustomNumeralSystem("01")
num2 = cn.CustomNumber(sys2, "1100101")
num10 = num2.to_decimal()
print(num10) # Prints "101"
sys16 = cn.CustomNumeralSystem("0123456789abcdef")
num16 = cn.CustomNumber(sys16, "f0")
num10 = num16.to_decimal()
print(num10) # Prints "240"
حتى الان جيدة جدا. الآن دعنا نذهب إلى المكسرات تمامًا وأعلن أن نظامًا به شحن أساسي من 16 ومجموعة غريبة تمامًا من الرموز للأرقام. ويمكننا فعل ذلك في الواقع وحتى أشياء مجنونة تمامًا:
sysN = cn.CustomNumeralSystem("kje5nCs21Q9vW0KMqc")
ملاحظة: الأرقام المخصصة حساسة للحالة! لذلك n! = n !!
ملاحظة: هناك أحرف محظورة ، والتي لا يمكن استخدامها في نظام أرقام ولا يمكن استخدامها في رقم. يمكنك دائمًا الحصول عليها كسلسلة باستخدام خاصية Forbiddddden_characters لفئة CustomNumerAlsystem .
في فئة CustomNumerAlsystem لتلبية صحة الأساس ، تم تنفيذ مشغلي المساواة والبيثون ، حتى تتمكن من مقارنة كائنين.
ومع ذلك ، سيكون المقارنة من خلال القائمة (أساسًا السلسلة) للأحرف التي تمثل الأرقام ، بدلاً من مقارنة كائن بيثون القياسي (المرجع).
sys1 = cn.CustomNumeralSystem("paf")
sys2 = cn.CustomNumeralSystem("paf")
# The two objects are different, despite being initialized with
# the same value
id(sys1) == id(sys2) # False
# However the set of characters (the digits) is the same, the
# base is the same, so I accept they are the same numeral systems
sys1 == sys2 # True
# And you could also test for inequality
sys1 = cn.CustomNumeralSystem("paf")
sys2 = cn.CustomNumeralSystem("paz")
sys1 != sys2 # True
يتم دعم الأرقام المخصصة الموقعة أيضًا.
sysN = cn.CustomNumeralSystem("paf")
numN1 = cn.CustomNumber(sysN, "-a") # A negative number
numN2 = cn.CustomNumber(sysN, "a") # A positive number
numN3 = cn.CustomNumber(sysN, "+a") # A positive number
يتم دعم عمليات الرياضيات الأساسية للمشغلين القياسيين في بيثون.
sysN = cn.CustomNumeralSystem("paf")
numN1 = cn.CustomNumber(sysN, "-a")
numN2 = cn.CustomNumber(sysN, "a")
numN3 = cn.CustomNumber(sysN, "+a")
# Comparisson
numN1 == numN2
numN1 != numN2
numN1 > numN2
numN1 < numN2
numN1 >= numN2
numN1 <= numN2
# Basic mathematical operations
numN1 + numN2 # Addition
numN1 += numN2 # Augmented addition
numN1 - numN2 # Subtraction
numN1 -= numN2 # Augmented subtraction
numN1 // numN2 # Floor division
numN1 / numN2 # NOTE: This will perform floor division as well!
# as floating point numbers are not supported by this class and will
# never be.
numN1 * numN2 # Multiplication
numN1 ** numN2 # Power
numN1 % numN2 # Modulo division
abs(numN) # Absolute value
باستخدام التكرار:
sysN = cn.CustomNumeralSystem("paf")
it = cn.GearIterator(sysN, 0, 2)
next(it) # "p" # "p" assumes to be the analog of the zero
next(it) # "a"
next(it) # "f"
next(it) # "ap"
next(it) # "aa"
# and so on. You get the idea.
# The iterator could also be initialized with an init_value which is
# de-facto a custom number from the chosen CustomNumeralSystem,
# but for convenience I left the number to be a string, as you may
# wish or not to initialize at all:
it = cn.GearIterator(sysN, 0, 2, "af")
ملاحظة: إذا تم تهيئتها ، فسيقوم المكرر بتجريد أي "أصفار" رائدة (إذا جاز التعبير) من init_value المعطى.
يحدد ويعلن نظام الأرقام المخصص.
CustomNumeralSystem(digits: str)
Args:
digits: The symbols to be used as digits. The string length defines
the numeral system base.
ملكيات:
forbidden_characters -> str
base -> int
طُرق:
valid_number(number: str) -> bool
Tests if the given "number" is valid for the current numeral system.
Should not contain forbidden characters.
Should contain only characters defined in the numeral system.
يحدد ويعلن رقمًا من نظام أرقام مخصص.
CustomNumber(numeral_system: CustomNumeralSystem, value: str)
Args:
numeral_system: A previously defined custom numeral system.
value: The value (the number itself).
ملكيات:
init_value -> str
Returns the initial value the class was initialized with.
طُرق:
digit_to_int(digit: str) -> int
Converts the given digit from the custom numeral system to a
decimal integer.
to_decimal() -> int
Converts the current number value to a decimal integer.
يتكرر على أرقام نظام الأرقام المخصص Eiter بدءًا من الرقم الأول (المكافئ الصفري) أو بدءًا من init_value معين.
يحاكي بإيجاز عدادات التروس القديمة ، مثل عداد المسافات القديم للسيارات.
GearIterator(numeral_system: CustomNumeralSystem, min_length: int = 0, max_length: int = 0, init_value: str = "")
Args:
numeral_system: Custom numeral system. Mind the order of symbols!
min_length: Minimum length, default is zero.
max_length: Maximum length, default is zero - means no limit.
init_value: Value to initialize with.
ملكيات:
combinations -> int
Returns the number of possible combinations (iterations).
ينفذ الفصل بروتوكول إدارة سياق بيثون.