Skip to content

Instantly share code, notes, and snippets.

@back-seat-driver
Last active July 30, 2017 19:24
Show Gist options
  • Save back-seat-driver/242de9459371ee96a2fa6fcf7fe8202d to your computer and use it in GitHub Desktop.
Save back-seat-driver/242de9459371ee96a2fa6fcf7fe8202d to your computer and use it in GitHub Desktop.
CALCULATOR-CORE
def conversion(str_list):
for i in range(len(str_list)):
str_list[i]=int(str_list[i])
return(str_list)
def trim_end(a_string):
return(a_string[0:len(a_string)-1])
def dec_shift(a_string):
to_build_0=''
for i in range(len(a_string)):
if a_string[i]!='.':
to_build_0+=a_string[i]
if a_string[i]=='.':
break
to_build_1=''
for i in range(1,len(a_string)+1):
if a_string[-i]!='.':
to_build_1+=a_string[-i]
if a_string[-i]=='.':
break
return(trim_end(to_build_0)+'.'+to_build_0[-1]+flip_string(to_build_1))
def flip_string(a_string):
to_return=''
for i in range(1,len(a_string)+1):
to_return+=a_string[-i]
return(to_return)
def can_div(value,to_div):
if value%to_div != value:
return(True)
def repeat_return(value,to_div,iter_len):
a_string=''
count=0
#Note that to_div is what can be
#manipulated to change len of output.
while count != iter_len:
if can_div(value,to_div)!=None:
a_string+=str(value//to_div)
value=value%to_div
if can_div(value,to_div)==None:
value=10*value
a_string+='0'
count+=1
return(a_string)
def sub_set_trim(a_string):
to_return=''
j=0
k=1
while k!=len(a_string):
try:
while a_string[j:k][-1]=='0':
k+=1
if k > len(a_string):
break
if trim_end(a_string[j:k])!='0':
to_return+='0'*(len(trim_end(a_string[j:k]))-1)
to_return+=a_string[j:k][-1]
j=k
k=j+1
except IndexError:
return(to_return)
return(to_return)
def return_small(str_0,str_1):
if len(str_0) > len(str_1):
return(len(str_0),len(str_1),str_0,str_1)
if len(str_1) > len(str_0):
return(len(str_1),len(str_0),str_1,str_0)
if len(str_1)==len(str_0):
return(len(str_1),len(str_0),str_1,str_0)
def zero_pad(str_0,str_1):
to_check=return_small(str_0,str_1)
return(to_check[-1]+'0'*(to_check[0]-to_check[1]),to_check[2])
def zero_front_pad(str_0,str_1):
to_check=return_small(str_0,str_1)
return('0'*(to_check[0]-to_check[1])+to_check[-1],to_check[2])
def large_return(str_0,str_1):
if int(str_0) > int(str_1):
return(str_0,str_1)
if int(str_1) > int(str_0):
return(str_1,str_0)
if int(str_1)==int(str_0):
return(None)
def str_int(a_list):
to_return=[]
for i in range(len(a_list)):
to_return.append(int(a_list[i]))
return(to_return)
def main(i):
for i in range(i):
print(sub_set_trim(repeat_return(1,i)))
def decimal_expand(d,iter_len):
return(sub_set_trim(repeat_return(1,d,iter_len)))
def true_fraction(n,d,iter_len):
return(dec_shift(dec_multi(decimal_expand(d,iter_len),n)))
'''
Below contains multi root expansion functions
leverging deciaml extension functions form above.
'''
def find_bound(n,t):
#Returns the first root bound, so given
#the nth root of interger t, this function
#will return the interger root first value
c=1
while c**n < t:
c+=1
d_0=abs(t-((c-1)**n))
d_1=abs(t-((c)**n))
if d_0 < d_1:
return(c-1)
if d_0 > d_1:
return(c)
if d_0==d_1:
return(c)
def point_convert(dec_str_point):
return('0'+dec_str_point[1:len(dec_str_point)])
def point_strip(dec_str_point):
to_return=''
for i in range(1,len(dec_str_point)+1):
if dec_str_point!='.':
to_return+=dec_str_point[-i]
if dec_str_point[-i]=='.':
return(flip_string(to_return))
def int_front_pull(dec_str_point):
#Will return the str left to right up till decimal
#Given '37.989' will return the string '37'
to_return=''
for i in range(len(dec_str_point)):
if dec_str_point!='.':
to_return+=dec_str_point[i]
if dec_str_point[i]=='.':
return(trim_end(to_return))
def shift_return(dec_str_0,dec_str_1):
#Form '0.0013' '0.312'
return((len(dec_str_0)+len(dec_str_1))-4)
def index_rip_dec(dec_str):
for i in range(len(dec_str)):
if dec_str[i]!='0' and dec_str[i]!='.':
break
return(dec_str[i:len(dec_str)])
def trim_lead(dec_str):
to_return=''
for i in range(len(dec_str)):
if dec_str[i]!='.':
to_return+=dec_str[i]
return(to_return)
def zero_push(a_str):
if a_str[0]=='.':
return('0'+a_str)
return(a_str)
def dec_remove(dec_str):
to_return=''
for i in range(len(dec_str)):
if dec_str[i]!='.':
to_return+=dec_str[i]
return(to_return)
def specific_padd(dec_str_0,dec_str_1):
var_0=trim_lead(point_strip(dec_str_0))
var_1=trim_lead(point_strip(dec_str_1))
'''
Below are pure calc operations
'''
def dec_add(dec_str_0,dec_str_1):
#Format: dec_str_0 as ex '02343'==0.2343
#Will add only two decimals
init=zero_pad(dec_str_0,dec_str_1)
var_0,var_1=init[0],init[1]
to_return=''
rem=0
for i in range(1,len(var_0)+1):
inter_0=int(var_0[-i])+int(var_1[-i])
inter_1=rem+inter_0
if i==len(var_0):
to_return+='.'+flip_string(str(inter_1))
if i!=len(var_0):
to_return+=str(inter_1)[-1]
if len(str(inter_1))==1:
rem=0
if len(str(inter_1))!=1:
rem=int(trim_end(str(inter_1)))
return(flip_string(to_return))
def dec_sub(dec_str_0,dec_str_1):
#ex 0.5322345 - 0.4325
#format as '5322345' and '4325' function will handle rest
#No leading zeros when formating either string
#and function will auto rotate as to not have negative
#number issues
#init[0] should always be the larger
#value and thus the value to be
#subtracted from.
init=zero_pad(dec_str_0,dec_str_1)
init=large_return(init[0],init[1])
var_0,var_1=str_int(list(init[0])),str_int(list(init[1]))
to_return=''
for i in range(1,len(var_0)+1):
if var_0[-i] >= var_1[-i]:
to_return+=str(var_0[-i]-var_1[-i])
if var_0[-i] < var_1[-i]:
var_0[-i]+=10
to_return+=str(var_0[-i]-var_1[-i])
while True==True:
if var_0[-(i+1)]==0:
var_0[-(i+1)]=9
i+=1
if var_0[-(i+1)]!=0:
var_0[-(i+1)]-=1
break
return('0.'+flip_string(to_return))
def dec_multi(dec_str,multi):
#Format dec_str as '041234'==0.41234
#Format multi at whole int
#Can't handel input decimal with leading int ex '15.34'
#Can only handle '.34' with respect to input form
to_return=''
rem=0
for i in range(1,len(dec_str)+1):
inter_0=int(dec_str[-i])*multi
inter_1=rem+inter_0
if i==len(dec_str):
to_return+='.'+flip_string(str(inter_1))
if i!=len(dec_str):
to_return+=str(inter_1)[-1]
if len(str(inter_1))==1:
rem=0
if len(str(inter_1))!=1:
rem=int(trim_end(str(inter_1)))
return(flip_string(to_return))
def pure_dec_multi(dec_str_0,dec_str_1):
#Take two decimals of form '0.123','0.22314'
#Is a hack should be tested
dec_shift=shift_return(dec_str_0,dec_str_1)
var_0=index_rip_dec(dec_str_0)
var_1=index_rip_dec(dec_str_1)
total=int(var_0)*int(var_1)
len_diff='0'*abs(len(str(total))-dec_shift)
return('0.'+len_diff+str(total))
def pure_multi(a_int,str_dec):
#Take a_int as 55 and str_dec='123.34' and
#will return the true calculator value.
#Decimals len unbounded. Improvment of previous
#single dec multiply.
whole_int=a_int*int(int_front_pull(str_dec))
whole_dec_str=dec_multi(point_convert(point_strip(str_dec)),a_int)
whole_dec_add=whole_int+int(int_front_pull(whole_dec_str))
return(str(whole_dec_add)+point_strip(whole_dec_str))
def ADD(dec_str_0,dec_str_1):
#Input two string of form '2.00123' and will return the true calculator value
back_0=point_convert(point_strip(dec_str_0))
back_1=point_convert(point_strip(dec_str_1))
front_0=int_front_pull(dec_str_0)
front_1=int_front_pull(dec_str_1)
to_add=dec_add(back_0,back_1)
back_total=point_strip(to_add)
front_total=str(int(front_0)+int(front_1)+int(int_front_pull(to_add)))
return(front_total+back_total)
def MULTI(dec_str_0,dec_str_1):
#Takes any string input ex '52450010.32274192', '24123910.3212192'
#And will return the the true calculator value scaled to as many
#decimal palces are you pass to the function. No call to float
#function, and will extend decimal point. Will work will both whole ints
#but the decimal needs to be called out. Call out decimal '234' as '234.0'
var_0=int_front_pull(dec_str_0)#str
var_1=int_front_pull(dec_str_1)#str
var_2=point_strip(dec_str_0)#str
var_3=point_strip(dec_str_1)#str
var_4=pure_multi(int(var_0),'0'+var_3)#str always split
var_5=pure_dec_multi('0'+var_2,'0'+var_3)#str will always be whole dec
var_6=pure_multi(int(var_1),'0'+var_2)#str always split
var_7=str(int(var_0)*int(var_1))#str will always be whole int
int_net_0=int(var_7)+int(int_front_pull(var_4))+int(int_front_pull(var_6))
dec_0=dec_add(point_convert(point_strip(var_4)),point_convert(point_strip(var_5)))
var_8=int(int_front_pull(dec_0))
int_net_1=int_net_0+var_8
dec_1=dec_add(point_convert(point_strip(dec_0)),point_convert(point_strip(var_6)))
var_9=int(int_front_pull(dec_1))
int_net_2=int_net_1+var_9
return(str(int_net_2)+point_strip(dec_1))
def POWER(dec_str,p_int):
#This function will take some decimal string and raise the value to some
#power '32.1231'**3 will return. Will work will both whole ints
#but the decimal needs to be called out. Call out decimal '234' as '234.0''
dec_str_copy=str(dec_str)
for i in range(p_int-1):
to_pass=MULTI(dec_str,dec_str_copy)
dec_str_copy=to_pass
return(dec_str_copy)
def SUB(dec_str_0,dec_str_1):
##('-4825.3273', -4824.6727, '1862.8110', '6687.4837')
##('-5225.80401', -5224.195, '3441.5396', '8665.7346')
##(('-547.1735', True), -546.8264999999997, '3403.7175', '3950.5440')
var_00=int_front_pull(dec_str_0)
var_11=int_front_pull(dec_str_1)
var_0=trim_lead(point_strip(dec_str_0))
var_1=trim_lead(point_strip(dec_str_1))
if len(var_0)==len(var_1):
var_2=var_0
var_3=var_1
if len(var_0) > len(var_1):
var_2=var_0
var_3=zero_pad(var_0,var_1)[0]
if len(var_0) < len(var_1):
var_2=zero_pad(var_0,var_1)[0]
var_3=var_1
var_4=var_00+dec_remove(var_2)
var_5=var_11+dec_remove(var_3)
if len(var_4) > len(var_5):
var_7=var_4
var_8=zero_front_pad(var_4,var_5)[0]
if len(var_4) < len(var_5):
var_7=zero_front_pad(var_4,var_5)[0]
var_8=var_5
if len(var_4) == len(var_5):
var_7=var_4
var_8=var_5
if int(dec_remove(dec_str_0)) < int(dec_remove(dec_str_1)):
var_6='-'
if int(dec_remove(dec_str_0)) > int(dec_remove(dec_str_1)):
var_6=''
if int(dec_remove(dec_str_0)) == int(dec_remove(dec_str_1)):
return('0.0')
var_9=trim_lead(point_strip(dec_sub(var_7,var_8)))
back_build=''
for i in range(1,len(var_3)+1):
back_build+=var_9[-i]
front_build=''
for i in range(len(var_3)+1,len(var_9)+1):
front_build+=var_9[-i]
var_10=var_6+flip_string(front_build)+'.'+flip_string(back_build)
return(var_10)
def DIV(num_dec_str_0,dem_dec_str_1,to_iter_main):
#Function will take two random dec_str ex '3.123' and '0.023' and
#will return decimal of the fractional. Run point strip return
#largest len minus one is true decimal shift. Append if not zero.
#The to_iter_main value needs a min len for the value to correctly converge
#on the answer. Unexpected. true_fraction(3127212344,3000,856) can be irrational.
#6 str will scale WTF? Tested and passed. Will work will both whole ints
#but the decimal needs to be called out. Call out decimal '234' as '234.0'
for i in range(1,len(num_dec_str_0)+1):
if num_dec_str_0[-i]=='.':
break
for x in range(1,len(dem_dec_str_1)+1):
if dem_dec_str_1[-x]=='.':
break
zero_append=abs(i-x)
if i > x:
new_dem=trim_lead(dem_dec_str_1+'0'*zero_append)
new_num=trim_lead(num_dec_str_0)
if i < x:
new_num=trim_lead(num_dec_str_0+'0'*zero_append)
new_dem=trim_lead(dem_dec_str_1)
if i==x:
new_num=trim_lead(num_dec_str_0)
new_dem=trim_lead(dem_dec_str_1)
return(zero_push(true_fraction(int(new_num),int(new_dem),to_iter_main)))
def N_ROOT(n_root,root_of,spc_perc,ger_perc):
#Check Windows 10 bug in Windows 7 nth root calculator
#WINDOWS BUG NTH ROOT FAIL
#ADD POWER CONDITIONS
#n_root=6
#root_of=2
#spc_perc=100
#ger_perc=210
x_0=str(str(find_bound(n_root,root_of))+'.'+'0'*spc_perc)
start_len_guess=len(x_0)-spc_perc
to_search=[]
for i in range(spc_perc):
x_k=MULTI(zero_push(true_fraction(1,n_root,ger_perc)),ADD(MULTI(str(n_root-1)+'.0',x_0),DIV(str(root_of)+'.0',POWER(x_0,n_root-1),ger_perc)))
x_0=x_k[0:start_len_guess+i+2]
to_search.append(x_0)
for i in range(1,len(to_search)+1):
to_check=SUB(to_search[-i],to_search[-(i+1)])
if to_check.count('0') == len(to_check)-2:
return(to_search[-i])
'''
Special Functions to Pass to GUI
'''
def is_in_truth(value,a_set):
#Will search through a_set looking to see if value is contained in that set
#and returns true if the values is in the set and returns false if the value
#is not in the set.
for i in range(len(a_set)):
if value==a_set[i]:
return(True)
return(False)
def str_splice(a_str,index):
return(a_str[0:index],a_str[index+1:len(a_str)])
def dda(a_str):
if is_in_truth('.',a_str)==False:
return(a_str+'.0')
return(a_str)
def main(input_str,gp,sp):
for i in range(len(input_str)):
if is_in_truth(input_str[i],('+','-','/','*','R','^'))==True:
break
if input_str[i]=='+':
insert=str_splice(input_str,i)
return(ADD(dda(insert[0]),dda(insert[1])))
if input_str[i]=='-':
insert=str_splice(input_str,i)
return(SUB(dda(insert[0]),dda(insert[1])))
if input_str[i]=='/':
insert=str_splice(input_str,i)
return(DIV(dda(insert[0]),dda(insert[1]),gp))
if input_str[i]=='*':
insert=str_splice(input_str,i)
return(MULTI(dda(insert[0]),dda(insert[1])))
if input_str[i]=='^':
insert=str_splice(input_str,i)
return(POWER(dda(insert[0]),int(insert[1])))
if input_str[i]=='R':
insert=str_splice(input_str,i)
return(N_ROOT(int(insert[0]),int(insert[1]),sp,gp))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment