มีดสวิส-แขนสำหรับจำนวนระบบตัวเลขที่กำหนดเอง
Evgueni Antonov 2023
โมดูลนี้ถูกสร้างและทดสอบใน Python 3.10.6, Pip 23.1.2
เผยแพร่ครั้งแรกในเดือนพฤษภาคม 2566
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
และนี่คือชื่อของเพลงที่ยอดเยี่ยมจริงๆจากยุค 90 1,100101 ไบนารีที่แปลงเป็นทศนิยมคือ 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
ระบบตัวเลขที่รู้จักกันดีอื่น ๆ คือระบบ octal ซึ่งประกอบด้วยตัวเลขเฉพาะจากศูนย์ถึงเจ็ดและฐาน 8 แต่เราจะข้ามระบบนี้ในตอนนี้
และในที่สุดระบบทศนิยมที่เราทุกคนรู้ตั้งแต่วัยเด็กมีฐาน 10 และทำจากตัวเลขจากศูนย์ถึงเก้า
โอเคจนถึงตอนนี้คุณคุ้นเคยกับระบบตัวเลขที่ใช้กันอย่างแพร่หลายมากที่สุด - ไบนารี, แปดค่า, ทศนิยมและเลขฐานสิบหก
เริ่มต้นด้วยการกำหนดระบบไบนารีอีกครั้ง ดังนั้นจึงมีฐาน 2 และเราจะไม่เปลี่ยนแปลงสิ่งนั้น อย่างไรก็ตามแทนที่จะใช้ 0 และ 1 เพื่อใช้ P และ A แทน? ดังนั้น P จะเป็นศูนย์ใหม่ของเรา และ จะเป็นใหม่ของเรา ดังนั้นตามปกติแล้วเราจะเขียน 1100101 ตอนนี้จะถูกเขียนเป็น AAPPAPA สับสนใช่มั้ย แต่สิ่งที่ - มันยังคงเป็นระบบไบนารีเราเพิ่งเปลี่ยนสัญลักษณ์
แต่ตอนนี้สิ่งที่เกี่ยวกับความบ้าคลั่ง - ระบบตัวเลขที่มีฐาน 3 โดยใช้สัญลักษณ์ P, A, F เป็นตัวเลขดังนั้น F จะเป็นทศนิยม 2 ทศนิยม p จะเป็น 0 ทศนิยม ดังนั้น:
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 !!
หมายเหตุ: มีอักขระต้องห้ามซึ่งไม่สามารถใช้ในระบบตัวเลขและไม่สามารถใช้ในตัวเลขได้ คุณสามารถรับพวกเขาเป็นสตริงได้เสมอโดยใช้คุณสมบัติ Forbidden_Characters ของคลาส CustomNumeralsystem
ในคลาส CustomNumeralsystem สำหรับความต้องการของการตรวจสอบขั้นพื้นฐานตัวดำเนินการ Equality และ Iequality Python ถูกนำไปใช้ดังนั้นคุณสามารถเปรียบเทียบวัตถุสองชิ้นได้
อย่างไรก็ตามการเปรียบเทียบจะเป็นโดยรายการ (โดยทั่วไปคือสตริง) ของอักขระที่แสดงถึงตัวเลขแทนที่จะเปรียบเทียบวัตถุ Python มาตรฐาน (อ้างอิง)
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
การดำเนินการทางคณิตศาสตร์ขั้นพื้นฐานได้รับการสนับสนุนผู้ให้บริการ Python มาตรฐาน Trough
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.
วนซ้ำตัวเลขของระบบตัวเลขตัวเลขที่กำหนดเองเริ่มต้นที่หมายเลขแรก (เทียบเท่าศูนย์) หรือเริ่มต้นจาก 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).
คลาสใช้โปรโตคอลการจัดการบริบท Python