File size: 7,820 Bytes
e9c9636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43a02e9
 
 
 
 
 
 
 
 
 
 
 
e9c9636
 
43a02e9
e9c9636
 
43a02e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#%%
# Given (25)6 and (43)6, calculate the sum, multiplication and difference

def basecalc(base, num1, num2): 

    # Order the numbers in a way that the first given number will always be the greater than the second (or it will be corrected to it)
    # This may not seem important for sum or multiplication, but it is important to calculate the difference
    order_list = []
    order_list.append(num1)
    order_list.append(num2)
    order_list.sort()
    ndiff2, ndiff1 = order_list
    
    # Creating lists with each digit of the given numbers been an element
    # The numbers of a given base are shown as (Number * Base ** (Exponent))
    # Reverting the list will not only make the the Number be equal to the element, but the index equal the exponent
    # This logic will be the same for the dictionaries used afterwards where: dict = {"exponent": number}
    n1list_rev = [int(z) for z in reversed(str(ndiff1))]
    n2list_rev = [int(z) for z in reversed(str(ndiff2))]

    # Creating the dictionary for the SUM of numbers of a given base
    nsum = {}

    # Adding the first given number to the dictionary as dict = {"exponent": number}
    for i_1 in range(0, len(n1list_rev)):
        nsum[str(i_1)] = n1list_rev[i_1]
    
    # Adding the secong given number to the dictionary as dict = {"exponent": number}
    for i_2 in range(0, len(n2list_rev)):
        nsum[str(i_2)] = nsum[str(i_2)] + n2list_rev[i_2]

    
    # Function to Correct the Dictionary numbers according to bases
    nsum = correct_bases(nsum, base)
    
    # Converting the dictionary into a number of the given base
    nsum_final = 0
    for numbers in nsum:
        nsum_final = nsum_final + ( (10 ** int(numbers)) * nsum[str(numbers)] )


    # Creating the dictionary for the MULTIPLICATION of numbers of a given base
    nmult = {}

    # Importing itertools for avoiding several loops
    import itertools

    # Considering that numbers of a given base have its structure as: XYZ -> X * BASE ** 2 + Y * BASE ** 1 + Z * BASE ** 1
    # Also, the dictionaries are acting with the exponents as indexes as:  dict = {"exponent": number}
    # Then, the multiplication must consider the index of each number. The index in the dictionary that will store the multiplication of the numbers will be the sum of the indexes in the list. 
        #Ex: [A, B] * [C, D]
        # A and C both have index 0, so A*C will be stored in dict[str(index(A) + index(C))] = A*C -> dict["0"] = A*C
        # A has index 0 and D has index 1, so A*D will be stored in dict[str(index(A) + index(D))] = A*D -> dict["1"] = A*D
        # B has index 1 and C has index 0, so B*C will be stored in dict[str(index(B) + index(C))] = B*C -> dict["1"] = B*C
        # B and D both have index 1, so B*D will be stored in dict[str(index(B) + index(D))] = B*D -> dict["2"] = B*D
        # If a index already have a value in it, than both values mus be added. Like A*D + B*C in index "1"
    for i_5, i_6 in itertools.product(n1list_rev, n2list_rev):
        try:
            nmult[str(n1list_rev.index(i_5) + n2list_rev.index(i_6))] = nmult[str(n1list_rev.index(i_5) + n2list_rev.index(i_6))] + i_5 * i_6
        except:
            nmult[str(n1list_rev.index(i_5) + n2list_rev.index(i_6))] = 0
            nmult[str(n1list_rev.index(i_5) + n2list_rev.index(i_6))] = nmult[str(n1list_rev.index(i_5) + n2list_rev.index(i_6))] + i_5 * i_6
    
    # Function to Correct the Dictionary numbers according to bases
    nmult = correct_bases(nmult, base)
    
    # Converting the dictionary into a number of the given base
    nmult_final = 0
    for numbers in nmult:
        nmult_final = nmult_final + ( ( 10 ** int(numbers) ) * nmult[numbers])


    # Creating the dictionary for the DIFFERENCE of numbers of a given base
    ndiff = {}

    # Adding the first given number (highest) to the dictionary as dict = {"exponent": number}
    for i_8 in range(0, len(n1list_rev)):
        ndiff[str(i_8)] = n1list_rev[i_8]

    # Subtracting the second given number (lowest) to the dictionary as dict = {"exponent": number}
    for i_8 in range(0, len(n2list_rev)):
        ndiff[str(i_8)] = ndiff[str(i_8)] - n2list_rev[i_8]
    
    # Function to Correct the Dictionary numbers according to bases
    ndiff = correct_bases(ndiff, base)

    # Converting the dictionary into a number of the given base
    ndiff_final = 0
    for numbers in ndiff:
        ndiff_final = ndiff_final + ((10 ** int(numbers)) * ndiff[numbers])
        
    # Returning Arguments
    return base, nsum_final, nmult_final, ndiff_final


# Function to correct the number of a given base after calculating the sum, multiplication or difference of two given numbers
 # Considering that numbers of a given base have its structure as: XYZ -> X * BASE ** 2 + Y * BASE ** 1 + Z * BASE ** 1
# Also, the dictionary MUST be written as dict = {"exponent": number}
def correct_bases(base_dict, given_base):
    
    # Function to correct positive numbers
    # Two variables are created:
        # n_int will store the integer part of the division of the given number and the given base
        # n_rest will store the rest of the division of the given number and the given base
    # n_int will be added to the next index (or Exponent)
    # n_rest will be the value of the current index (or Exponent)
    def correct_positive(value):
        n_int = int(int(value) / given_base)
        n_rest = int(int(value) % given_base)
        current_index = n_rest
        next_index = n_int
        return current_index, next_index
    
    # Storing the Dictionary keys (or Exponents). The reason to store it in a list is that the number of keys may increase if adding or multiplying
    keyslist = [k for k in base_dict.keys()]

    for i_7 in keyslist:
        # Checking if the value (Number) is greater that the Base and the next index is NOT in the dictionary. If so, a new index must be created.
        if base_dict[i_7] >= given_base and str(int(i_7)+1) not in base_dict.keys():
            base_dict[str(int(i_7)+1)] = 0
        
        # If the value (Number) is positive, use the correct positive function
        if base_dict[i_7] >= 0:

            # Two variables are created:
                # n_int will store the integer part of the division of the given number and the given base
                # n_rest will store the rest of the division of the given number and the given base
            # n_int will be added to the next index (or Exponent)
            # n_rest will be the value of the current index (or Exponent)
            n_int = int(int(base_dict[i_7]) / given_base)
            n_rest = int(int(base_dict[i_7]) % given_base)
            
            base_dict[i_7] = n_rest

            try:
                base_dict[str(int(i_7)+1)] += n_int
            except:
                pass

        # If the value (Number) is negative, correcting it by adding the Base to the current number and subtracting 1 from the previous.
        # This could be achieved in this form because the numbers are been sorted in the beginning.
        if base_dict[i_7] < 0:
            base_dict[i_7] = base_dict[i_7] + given_base
            base_dict[str(int(i_7)+1)] -= 1
    
    # Returning the corrected Dictionary
    return base_dict

#%%
def resposta(base, número1, numero2):
    base_, nsum_final_, nmult_final_, ndiff_final_= basecalc(base, número1, numero2)
    base_ = f"Base: {base_}"
    nsum_final_ = f"Sum: {nsum_final_}"
    nmult_final_= f"Multiplication: {nmult_final_}"
    ndiff_final_= f"Difference: {ndiff_final_}"

    return base_, nsum_final_, nmult_final_, ndiff_final_


#%%
import gradio as gr

calc = gr.Interface(fn=resposta, inputs=[gr.Number(), gr.Number(), gr.Number()], outputs=["text", "text", "text", "text"], )

if __name__ == "__main__":
    calc.launch()