NicolasSP90 commited on
Commit
e9c9636
1 Parent(s): 7422791

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +166 -0
app.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #%%
2
+ # Given (25)6 and (43)6, calculate the sum, multiplication and difference
3
+
4
+ def basecalc(base, num1, num2):
5
+
6
+ # 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)
7
+ # This may not seem important for sum or multiplication, but it is important to calculate the difference
8
+ order_list = []
9
+ order_list.append(num1)
10
+ order_list.append(num2)
11
+ order_list.sort()
12
+ ndiff2, ndiff1 = order_list
13
+
14
+ # Creating lists with each digit of the given numbers been an element
15
+ # The numbers of a given base are shown as (Number * Base ** (Exponent))
16
+ # Reverting the list will not only make the the Number be equal to the element, but the index equal the exponent
17
+ # This logic will be the same for the dictionaries used afterwards where: dict = {"exponent": number}
18
+ n1list_rev = [int(z) for z in reversed(str(ndiff1))]
19
+ n2list_rev = [int(z) for z in reversed(str(ndiff2))]
20
+
21
+ # Creating the dictionary for the SUM of numbers of a given base
22
+ nsum = {}
23
+
24
+ # Adding the first given number to the dictionary as dict = {"exponent": number}
25
+ for i_1 in range(0, len(n1list_rev)):
26
+ nsum[str(i_1)] = n1list_rev[i_1]
27
+
28
+ # Adding the secong given number to the dictionary as dict = {"exponent": number}
29
+ for i_2 in range(0, len(n2list_rev)):
30
+ nsum[str(i_2)] = nsum[str(i_2)] + n2list_rev[i_2]
31
+
32
+
33
+ # Function to Correct the Dictionary numbers according to bases
34
+ nsum = correct_bases(nsum, base)
35
+
36
+ # Converting the dictionary into a number of the given base
37
+ nsum_final = 0
38
+ for numbers in nsum:
39
+ nsum_final = nsum_final + ( (10 ** int(numbers)) * nsum[str(numbers)] )
40
+
41
+
42
+ # Creating the dictionary for the MULTIPLICATION of numbers of a given base
43
+ nmult = {}
44
+
45
+ # Importing itertools for avoiding several loops
46
+ import itertools
47
+
48
+ # Considering that numbers of a given base have its structure as: XYZ -> X * BASE ** 2 + Y * BASE ** 1 + Z * BASE ** 1
49
+ # Also, the dictionaries are acting with the exponents as indexes as: dict = {"exponent": number}
50
+ # 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.
51
+ #Ex: [A, B] * [C, D]
52
+ # 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
53
+ # 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
54
+ # 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
55
+ # 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
56
+ # If a index already have a value in it, than both values mus be added. Like A*D + B*C in index "1"
57
+ for i_5, i_6 in itertools.product(n1list_rev, n2list_rev):
58
+ try:
59
+ 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
60
+ except:
61
+ nmult[str(n1list_rev.index(i_5) + n2list_rev.index(i_6))] = 0
62
+ 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
63
+
64
+ # Function to Correct the Dictionary numbers according to bases
65
+ nmult = correct_bases(nmult, base)
66
+
67
+ # Converting the dictionary into a number of the given base
68
+ nmult_final = 0
69
+ for numbers in nmult:
70
+ nmult_final = nmult_final + ( ( 10 ** int(numbers) ) * nmult[numbers])
71
+
72
+
73
+ # Creating the dictionary for the DIFFERENCE of numbers of a given base
74
+ ndiff = {}
75
+
76
+ # Adding the first given number (highest) to the dictionary as dict = {"exponent": number}
77
+ for i_8 in range(0, len(n1list_rev)):
78
+ ndiff[str(i_8)] = n1list_rev[i_8]
79
+
80
+ # Subtracting the second given number (lowest) to the dictionary as dict = {"exponent": number}
81
+ for i_8 in range(0, len(n2list_rev)):
82
+ ndiff[str(i_8)] = ndiff[str(i_8)] - n2list_rev[i_8]
83
+
84
+ # Function to Correct the Dictionary numbers according to bases
85
+ ndiff = correct_bases(ndiff, base)
86
+
87
+ # Converting the dictionary into a number of the given base
88
+ ndiff_final = 0
89
+ for numbers in ndiff:
90
+ ndiff_final = ndiff_final + ((10 ** int(numbers)) * ndiff[numbers])
91
+
92
+ # Returning Arguments
93
+ return base, nsum_final, nmult_final, ndiff_final
94
+
95
+
96
+ # Function to correct the number of a given base after calculating the sum, multiplication or difference of two given numbers
97
+ # Considering that numbers of a given base have its structure as: XYZ -> X * BASE ** 2 + Y * BASE ** 1 + Z * BASE ** 1
98
+ # Also, the dictionary MUST be written as dict = {"exponent": number}
99
+ def correct_bases(base_dict, given_base):
100
+
101
+ # Function to correct positive numbers
102
+ # Two variables are created:
103
+ # n_int will store the integer part of the division of the given number and the given base
104
+ # n_rest will store the rest of the division of the given number and the given base
105
+ # n_int will be added to the next index (or Exponent)
106
+ # n_rest will be the value of the current index (or Exponent)
107
+ def correct_positive(value):
108
+ n_int = int(int(value) / given_base)
109
+ n_rest = int(int(value) % given_base)
110
+ current_index = n_rest
111
+ next_index = n_int
112
+ return current_index, next_index
113
+
114
+ # 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
115
+ keyslist = [k for k in base_dict.keys()]
116
+
117
+ for i_7 in keyslist:
118
+ # 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.
119
+ if base_dict[i_7] >= given_base and str(int(i_7)+1) not in base_dict.keys():
120
+ base_dict[str(int(i_7)+1)] = 0
121
+
122
+ # If the value (Number) is positive, use the correct positive function
123
+ if base_dict[i_7] >= 0:
124
+
125
+ # Two variables are created:
126
+ # n_int will store the integer part of the division of the given number and the given base
127
+ # n_rest will store the rest of the division of the given number and the given base
128
+ # n_int will be added to the next index (or Exponent)
129
+ # n_rest will be the value of the current index (or Exponent)
130
+ n_int = int(int(base_dict[i_7]) / given_base)
131
+ n_rest = int(int(base_dict[i_7]) % given_base)
132
+
133
+ base_dict[i_7] = n_rest
134
+
135
+ try:
136
+ base_dict[str(int(i_7)+1)] += n_int
137
+ except:
138
+ pass
139
+
140
+ # If the value (Number) is negative, correcting it by adding the Base to the current number and subtracting 1 from the previous.
141
+ # This could be achieved in this form because the numbers are been sorted in the beginning.
142
+ if base_dict[i_7] < 0:
143
+ base_dict[i_7] = base_dict[i_7] + given_base
144
+ base_dict[str(int(i_7)+1)] -= 1
145
+
146
+ # Returning the corrected Dictionary
147
+ return base_dict
148
+
149
+ #%%
150
+ b = 6
151
+ n1 = 25
152
+ n2 = 43
153
+ base_, nsum_final_, nmult_final_, ndiff_final_= basecalc(b,n1,n2)
154
+ print(f"Base: {base_}")
155
+ print(f"Sum: {nsum_final_}")
156
+ print(f"Multiplication: {nmult_final_}")
157
+ print(f"Difference: {ndiff_final_}")
158
+
159
+ # %%
160
+ import gradio as gr
161
+
162
+ calc = gr.Interface(fn=basecalc, inputs=[gr.Number(), gr.Slider(0,1000, gr.Slider(0,1000))], outputs=["text", "text", "text", "text"])
163
+
164
+ if __name__ == "__main__":
165
+ calc.launch()
166
+