Create a base 12 calculator with different limits at diferent digits with python

2024/11/17 21:21:32

I want o create a calculator that can add (and multiply, divide, etc) numbers in base 12 and with different limits at the different digits.

Base 12 sequence: [0,1,2,3,4,5,6,7,8,9,"A","B"]

The limits must be:

First digit: limit "B" Second digit: limit 4 Third digit: limit "B"

(The idea would be that it follows the hourly System limits but in base 12 so for example in base 12 there are 50 seconds in a minute)

That means you would count like this:[1,2,3,4,5,6,7,8,9,A,B,10,11,...48,49,4A,4B,100,101,...14B,200,201,...B4B,1000,1001..]

So I made the following code

 import string
digs = string.digits + string.ascii_uppercasedef converter(number):#split number in figuresfigures = [int(i,12) for i in str(number)]#invert oder of figures (lowest count first)figures = figures[::-1]result = 0#loop over all figuresfor i in range(len(figures)):#add the contirbution of the i-th figureresult += figures[i]*12**ireturn resultdef int2base(x):if x < 0:sign = -1elif x == 0:return digs[0]else:sign = 1x *= signdigits = []while x:digits.append(digs[int(x % 12)])x = int(x / 12)if sign < 0:digits.append('-')digits.reverse()return ''.join(digits)def calculator (entry1, operation, entry2):value1=float(converter(entry1))value2=float(converter(entry2))if operation == "suma" or "+":resultvalue=value1+value2else:print("operación no encontrada, porfavor ingrese +,-,")result=int2base(resultvalue)return resultprint(calculator(input("Ingrese primer valor"), input("ingrese operación"), input("Ingrese segundo valor")))

The thing is that I dont know how to establish the limits to the different digits If someone could help me I would be extreamly greatful

Answer

You can define two converters:

class Base12Convert:d = {hex(te)[2:].upper():te for te in range(0,12)}d.update({val:key for key,val in d.items()})d["-"] = "-"@staticmethoddef text_to_int(text):"""Converts a base-12 text into an int."""if not isinstance(text,str):raise ValueError(f"Only strings allowed: '{text}' of type '{type(text)}' is invalid")t = text.strip().upper()if any (x not in Base12Convert.d for x in t):raise ValueError(f"Only [-0123456789abAB] allowed in string: '{t}' is invalid")            if "-" in t.lstrip("-"):raise ValueError(f"Sign '-' only allowed in front. '{t}' is invalid")# easy wayreturn int(t,12)# self-calculated way# return sum(Base12Convert.d[num]*12**idx for idx,num in enumerate(t[::-1]))@staticmethoddef int_to_text(num):"""Converts an int into a base-12 string."""sign = ""if not isinstance(num,int):raise ValueError(f"Only integer as input allowed: '{num} of type {type(num)}' is invalid")if num < 0:sign = "-"num *= -1# get highest possible numberp = 1while p < num:p *= 12# create string rv = [sign]while True:p /= 12div = num // pnum -= div*prv.append(Base12Convert.d[div])if p == 1:breakreturn ''.join(rv)

Then you can use them to convert what you want:

text = "14:54:31"  # some time # convert each int of the time into base12 - join with | again
base12 = '|'.join(Base12Convert.int_to_text (int(t)) for t in text.split(":"))# split base12 at |, convert to int, make str from int and join with | again
base10 = '|'.join(map(str,(Base12Convert.text_to_int (b12) for b12 in base12.split("|"))))# print all 3
print(text,base12,base10,sep="\n")

Output:

14:54:31
12|46|27
14|54|31

and enforce whatever restrictions you have on your "digits" using normal ints. You should split up your "digits" of a clock ... 14b (203 in base 10) does not make sense, 1:4b might if you mean 1:59 hours/minutes.

https://en.xdnf.cn/q/118754.html

Related Q&A

Python Program to check if a number is armstrong or not is not working, what am I doing wrong?

n=int(input("Enter a Number: ")) x=0 y=0 z=0while(n>0):x=n%10y=x**3z=z+yn=n//10print (z) #The z here is the same value which I enter, yet it doesnt work. #If I enter 407 as n, z becomes (4…

Python(Scrapy) unpredictable mistake with import load_entry_point

I have such problem, I did nothing with Python or Scrapy, but when I started today my computer I got such error. I have found many different posts and tried some tips and advices, unfortunately, they a…

Debugging RadioButtons program in Python

from Tkinter import *class Application (Frame):def __init__(self, master):Frame.__init__(self, master)self.grid()self.create_widgets()def create_widgets(self):Label(self, text = "Select the last b…

Why am I getting an Internal Server error

My python script runs just fine on the Apache server locally set up on my computer, however, on importing the json2html library I am getting an internal server errorThe moment I comment the import stat…

Save pixel data in txt format in PIL

My program is to extract the pixel from an image and to save the pixel data in the text file for analysis. My picture is a binary image that gives only 255 and 0 sHere is the program:from PIL import Im…

ValueError: view limit minimum 0.0 is less than 1 and is an invalid Matplotlib date value

Ive been given the python script where matplotlib is used , when running the script it opens the window and display graph. its working perfectly on my laptop. But this error occurs when I upload the fi…

Python win32com Outlook Stores not iterable

Trying to list all Outllook stores (and finally all e-mails in those stores):import win32com.clientoutlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") sto…

Using a users input to search dictionary keys and print that keys matching value

I am working with tkinter, as I have my gui set up with and entry, label, and button. Im trying to search my dictionarys keys with the users input from the entry, and print the value of the key that wa…

How to write CSV into the next column

I have output that I can write into a CSV. However, because of how i setup my XML to text, the output iterates itself incorrectly. Ive tried a lot to fix my XML output, but I dont see any way to fix it…

Comparing date from pandas dataframe to current date

Im currently trying to write a script that does a specific action on a certain day. So for example, if today is the 6/30/2019 and in my dataframe there is a 6/30/2019 entry, xyz proceeds to happen. How…