-
Notifications
You must be signed in to change notification settings - Fork 1
/
currency.py
172 lines (156 loc) · 3.74 KB
/
currency.py
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
168
169
170
171
172
import math
CURRENCY_CHART = {
'pp': {
'cp': {
'rate': 1e3,
'label': "1000"
},
'sp': {
'rate': 100,
'label': "100"
},
'ep': {
'rate': 20,
'label': "20"
},
'gp': {
'rate': 10,
'label': "10"
},
'pp': {
'rate': 1,
'label': "1"
}
},
'gp': {
'cp': {
'rate': 100,
'label': "100"
},
'sp': {
'rate': 10,
'label': "10"
},
'ep': {
'rate': 2,
'label': "2"
},
'gp': {
'rate': 1,
'label': "1"
},
'pp': {
'rate': .1,
'label': "1/10"
}
},
'ep': {
'cp': {
'rate': 50,
'label': "50"
},
'sp': {
'rate': 5,
'label': "5"
},
'ep': {
'rate': 1,
'label': "1"
},
'gp': {
'rate': .5,
'label': "1/2"
},
'pp': {
'rate': .05,
'label': "1/20"
}
},
'sp': {
'cp': {
'rate': 10,
'label': "10"
},
'sp': {
'rate': 1,
'label': "1"
},
'ep': {
'rate': .2,
'label': "1/5"
},
'gp': {
'rate': .1,
'label': "1/10"
},
'pp': {
'rate': .01,
'label': "1/100"
}
},
'cp': {
'cp': {
'rate': 1,
'label': "1"
},
'sp': {
'rate': .1,
'label': "1/10"
},
'ep': {
'rate': .02,
'label': "1/50"
},
'gp': {
'rate': .01,
'label': "1/100"
},
'pp': {
'rate': .001,
'label': "1/1000"
}
}
}
REVERSED_CHART = ['cp', 'sp', 'ep', 'gp', 'pp']
def decimal_validate(value):
return not math.isnan(int(value))
def format_coins(r):
t = {};
for coin in CURRENCY_CHART:
t[coin] = int(r[coin]) if decimal_validate(r[coin]) else 0
return t
def rate(rate):
return int(rate.split('/')[1]) if '/' in rate else 1
def exchange(target_currency, amount, currency):
return amount * CURRENCY_CHART[currency][target_currency]['rate']
def modulus(target_currency, amount, currency):
return amount % rate(CURRENCY_CHART[currency][target_currency]['label'])
def positive_calc(amount, currency, currency_obj):
for coin in CURRENCY_CHART:
if currency == coin:
currency_obj[currency] = currency_obj[currency] + amount
return currency_obj[currency]
exchanged = math.floor(exchange(coin, amount, currency))
amount = modulus(coin, amount, currency)
currency_obj[coin] += exchanged
def negative_calc(amount, currency, currency_obj):
for coin in REVERSED_CHART:
if currency == coin:
currency_obj[currency] = currency_obj[currency] + amount
if currency_obj[currency] > 0:
return currency_obj[currency]
exchanged = math.floor(exchange(coin, amount, currency))
amount = modulus(coin, amount, currency)
#currency_obj[coin] += exchanged
def optimal_exchange(coin_args):
pouch = {}
for coin in CURRENCY_CHART:
pouch[coin] = 0;
formated = format_coins(coin_args);
for f in formated:
if formated[f] > 0:
positive_calc(formated[f], f, pouch);
for f in formated:
if formated[f] < 0:
negative_calc(formated[f], f, pouch);
return pouch