Spaces:
Paused
Paused
File size: 10,148 Bytes
1fefee0 | 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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | import html
import math
import statistics
def _nums(text: str):
values = []
for part in text.replace(',', ' ').split():
values.append(float(part))
return values
def setup(registry):
async def sumx_cmd(ctx):
try:
nums = _nums(ctx.args)
except Exception:
await ctx.respond('⚠️ Usa números separados por espacios. Ejemplo: <code>.sumx 10 20 30</code>')
return
if not nums:
await ctx.respond('⚠️ Pásame números.')
return
await ctx.respond(f'➕ <b>Suma</b>\n\n<code>{sum(nums)}</code>')
async def avg_cmd(ctx):
try:
nums = _nums(ctx.args)
except Exception:
await ctx.respond('⚠️ Pásame números válidos.')
return
if not nums:
await ctx.respond('⚠️ Pásame números.')
return
await ctx.respond(f'📊 <b>Promedio</b>\n\n<code>{statistics.mean(nums)}</code>')
async def median_cmd(ctx):
try:
nums = _nums(ctx.args)
except Exception:
await ctx.respond('⚠️ Pásame números válidos.')
return
if not nums:
await ctx.respond('⚠️ Pásame números.')
return
await ctx.respond(f'📈 <b>Mediana</b>\n\n<code>{statistics.median(nums)}</code>')
async def minx_cmd(ctx):
try:
nums = _nums(ctx.args)
await ctx.respond(f'🔽 <b>Mínimo</b>\n\n<code>{min(nums)}</code>')
except Exception:
await ctx.respond('⚠️ Pásame números válidos.')
async def maxx_cmd(ctx):
try:
nums = _nums(ctx.args)
await ctx.respond(f'🔼 <b>Máximo</b>\n\n<code>{max(nums)}</code>')
except Exception:
await ctx.respond('⚠️ Pásame números válidos.')
async def sortnums_cmd(ctx):
try:
nums = _nums(ctx.args)
except Exception:
await ctx.respond('⚠️ Pásame números válidos.')
return
if not nums:
await ctx.respond('⚠️ Pásame números.')
return
await ctx.respond(f'🔢 <b>Números ordenados</b>\n\n<code>{" ".join(str(x) for x in sorted(nums))}</code>')
async def uniqnums_cmd(ctx):
try:
nums = _nums(ctx.args)
except Exception:
await ctx.respond('⚠️ Pásame números válidos.')
return
out = []
for n in nums:
if n not in out:
out.append(n)
await ctx.respond(f'🧹 <b>Números únicos</b>\n\n<code>{" ".join(str(x) for x in out)}</code>')
async def rangegen_cmd(ctx):
parts = ctx.args.split()
if not 1 <= len(parts) <= 3 or not all(p.lstrip('-').isdigit() for p in parts):
await ctx.respond('⚠️ Usa <code>.rangegen 1 10 2</code>')
return
nums = list(map(int, parts))
if len(nums) == 1:
start, stop, step = 0, nums[0], 1
elif len(nums) == 2:
start, stop = nums
step = 1
else:
start, stop, step = nums
if step == 0:
await ctx.respond('⚠️ El paso no puede ser 0.')
return
out = list(range(start, stop, step))[:300]
await ctx.respond(f'📚 <b>Rango</b>\n\n<code>{" ".join(str(x) for x in out)}</code>')
async def pct_cmd(ctx):
parts = ctx.args.split()
if len(parts) != 2:
await ctx.respond('⚠️ Usa <code>.pct 25 300</code>')
return
try:
a, b = map(lambda x: float(x.replace(',', '.')), parts)
except Exception:
await ctx.respond('⚠️ Valores inválidos.')
return
await ctx.respond(f'📐 <b>Porcentaje</b>\n\n<code>{(a/100)*b}</code>')
async def rule3_cmd(ctx):
parts = ctx.args.split()
if len(parts) != 3:
await ctx.respond('⚠️ Usa <code>.rule3 a b c</code> para resolver a:b = c:x')
return
try:
a, b, c = map(lambda x: float(x.replace(',', '.')), parts)
x = (b * c) / a
except Exception:
await ctx.respond('⚠️ Valores inválidos.')
return
await ctx.respond(f'📏 <b>Regla de 3</b>\n\n<code>x = {x}</code>')
async def vat_cmd(ctx):
parts = ctx.args.split()
if len(parts) != 2:
await ctx.respond('⚠️ Usa <code>.vat 100 21</code>')
return
try:
amount, tax = map(lambda x: float(x.replace(',', '.')), parts)
except Exception:
await ctx.respond('⚠️ Valores inválidos.')
return
total = amount * (1 + tax / 100)
await ctx.respond(f'🧾 <b>Total con impuesto</b>\n\n<code>{total:.2f}</code>')
async def discount_cmd(ctx):
parts = ctx.args.split()
if len(parts) != 2:
await ctx.respond('⚠️ Usa <code>.discount 100 15</code>')
return
try:
amount, pct = map(lambda x: float(x.replace(',', '.')), parts)
except Exception:
await ctx.respond('⚠️ Valores inválidos.')
return
total = amount * (1 - pct / 100)
await ctx.respond(f'🏷️ <b>Con descuento</b>\n\n<code>{total:.2f}</code>')
async def interest_cmd(ctx):
parts = ctx.args.split()
if len(parts) != 3:
await ctx.respond('⚠️ Usa <code>.interest capital tasa años</code>')
return
try:
principal, rate, years = map(lambda x: float(x.replace(',', '.')), parts)
except Exception:
await ctx.respond('⚠️ Valores inválidos.')
return
total = principal * ((1 + rate / 100) ** years)
await ctx.respond(f'💹 <b>Interés compuesto</b>\n\n<code>{total:.2f}</code>')
async def splitbill_cmd(ctx):
parts = ctx.args.split()
if len(parts) != 2:
await ctx.respond('⚠️ Usa <code>.splitbill 250 5</code>')
return
try:
amount = float(parts[0].replace(',', '.'))
people = int(float(parts[1]))
except Exception:
await ctx.respond('⚠️ Valores inválidos.')
return
if people <= 0:
await ctx.respond('⚠️ Personas debe ser mayor que 0.')
return
await ctx.respond(f'🍽️ <b>Cuenta por persona</b>\n\n<code>{amount / people:.2f}</code>')
async def tip_cmd(ctx):
parts = ctx.args.split()
if len(parts) != 2:
await ctx.respond('⚠️ Usa <code>.tip 100 10</code>')
return
try:
amount, pct = map(lambda x: float(x.replace(',', '.')), parts)
except Exception:
await ctx.respond('⚠️ Valores inválidos.')
return
val = amount * pct / 100
await ctx.respond(f'💸 <b>Propina</b>\n\n<code>{val:.2f}</code>')
async def gcdx_cmd(ctx):
parts = ctx.args.split()
if len(parts) != 2 or not all(p.lstrip('-').isdigit() for p in parts):
await ctx.respond('⚠️ Usa <code>.gcdx 12 18</code>')
return
a, b = map(int, parts)
await ctx.respond(f'🧠 <b>MCD</b>\n\n<code>{math.gcd(a, b)}</code>')
async def lcmx_cmd(ctx):
parts = ctx.args.split()
if len(parts) != 2 or not all(p.lstrip('-').isdigit() for p in parts):
await ctx.respond('⚠️ Usa <code>.lcmx 12 18</code>')
return
a, b = map(int, parts)
await ctx.respond(f'🧠 <b>MCM</b>\n\n<code>{math.lcm(a, b)}</code>')
async def stddev_cmd(ctx):
try:
nums = _nums(ctx.args)
except Exception:
await ctx.respond('⚠️ Pásame números válidos.')
return
if len(nums) < 2:
await ctx.respond('⚠️ Necesito al menos 2 números.')
return
await ctx.respond(f'📉 <b>Desviación estándar</b>\n\n<code>{statistics.pstdev(nums)}</code>')
registry.add('sumx', sumx_cmd, 'Suma varios números.', usage='.sumx 10 20 30', category='Números')
registry.add('avg', avg_cmd, 'Promedia varios números.', usage='.avg 10 20 30', category='Números')
registry.add('medianx', median_cmd, 'Calcula la mediana.', usage='.medianx 10 20 30', category='Números')
registry.add('minx', minx_cmd, 'Muestra el mínimo.', usage='.minx 10 20 30', category='Números')
registry.add('maxx', maxx_cmd, 'Muestra el máximo.', usage='.maxx 10 20 30', category='Números')
registry.add('sortnums', sortnums_cmd, 'Ordena números.', usage='.sortnums 5 1 9 3', category='Números')
registry.add('uniqnums', uniqnums_cmd, 'Elimina números repetidos.', usage='.uniqnums 1 1 2 2 3', category='Números')
registry.add('rangegen', rangegen_cmd, 'Genera un rango numérico.', usage='.rangegen 1 10 2', category='Números')
registry.add('pct', pct_cmd, 'Calcula porcentaje de un valor.', usage='.pct 25 300', category='Números')
registry.add('rule3', rule3_cmd, 'Resuelve regla de 3 simple.', usage='.rule3 2 10 5', category='Números')
registry.add('vat', vat_cmd, 'Aplica impuesto a un importe.', usage='.vat 100 21', category='Números')
registry.add('discount', discount_cmd, 'Aplica descuento a un importe.', usage='.discount 100 15', category='Números')
registry.add('interest', interest_cmd, 'Calcula interés compuesto.', usage='.interest 1000 12 2', category='Números')
registry.add('splitbill', splitbill_cmd, 'Divide una cuenta entre personas.', usage='.splitbill 250 5', category='Números')
registry.add('tip', tip_cmd, 'Calcula propina.', usage='.tip 100 10', category='Números')
registry.add('gcdx', gcdx_cmd, 'Máximo común divisor.', usage='.gcdx 12 18', category='Números')
registry.add('lcmx', lcmx_cmd, 'Mínimo común múltiplo.', usage='.lcmx 12 18', category='Números')
registry.add('stddev', stddev_cmd, 'Desviación estándar poblacional.', usage='.stddev 10 20 30', category='Números')
|