chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
import math
|
||||
|
||||
# this function is used to round the result to 2 decimal places
|
||||
# e.g. 52.3523 -> 52.35, 52.0011 -> 52, 0.00000233 -> 0.0000023
|
||||
def custom_round(x, decimal_places=2):
|
||||
str_x = f"{x:.10f}"
|
||||
before_decimal = str_x.split('.')[0]
|
||||
after_decimal = str_x.split('.')[1]
|
||||
leading_zeros = len(after_decimal) - len(after_decimal.lstrip('0'))
|
||||
|
||||
if leading_zeros >= 1 and before_decimal == "0":
|
||||
return round(x, leading_zeros + 2)
|
||||
else:
|
||||
return round(x, decimal_places)
|
||||
|
||||
# this function converts a number in scientific notation to decimal notation
|
||||
def scito_decimal(sci_str):
|
||||
def split_exponent(number_str):
|
||||
parts = number_str.split("e")
|
||||
coefficient = parts[0]
|
||||
exponent = int(parts[1]) if len(parts) == 2 else 0
|
||||
return coefficient, exponent
|
||||
|
||||
def multiplyby_10(number_str, exponent):
|
||||
if exponent == 0:
|
||||
return number_str
|
||||
|
||||
if exponent > 0:
|
||||
index = number_str.index(".") if "." in number_str else len(number_str)
|
||||
number_str = number_str.replace(".", "")
|
||||
new_index = index + exponent
|
||||
number_str += "0" * (new_index - len(number_str))
|
||||
if new_index < len(number_str):
|
||||
number_str = number_str[:new_index] + "." + number_str[new_index:]
|
||||
return number_str
|
||||
|
||||
if exponent < 0:
|
||||
index = number_str.index(".") if "." in number_str else len(number_str)
|
||||
number_str = number_str.replace(".", "")
|
||||
new_index = index + exponent
|
||||
number_str = "0" * (-new_index) + number_str
|
||||
number_str = "0." + number_str
|
||||
return number_str
|
||||
|
||||
coefficient, exponent = split_exponent(sci_str)
|
||||
decimal_str = multiplyby_10(coefficient, exponent)
|
||||
|
||||
# remove trailing zeros
|
||||
if "." in decimal_str:
|
||||
decimal_str = decimal_str.rstrip("0")
|
||||
|
||||
return decimal_str
|
||||
|
||||
# normalize the result to 2 decimal places and remove trailing zeros
|
||||
def normalize(res, round_to=2):
|
||||
# we round the result to 2 decimal places
|
||||
res = custom_round(res, round_to)
|
||||
res = str(res)
|
||||
if "." in res:
|
||||
while res[-1] == "0":
|
||||
res = res[:-1]
|
||||
res = res.strip(".")
|
||||
|
||||
# scientific notation
|
||||
if "e" in res:
|
||||
res = scito_decimal(res)
|
||||
|
||||
return res
|
||||
|
||||
# 1. add
|
||||
def add_(args):
|
||||
|
||||
return normalize(sum(args))
|
||||
|
||||
# 2. subtract
|
||||
def subtract_(args):
|
||||
|
||||
res = args[0]
|
||||
for arg in args[1:]:
|
||||
res -= arg
|
||||
return normalize(res)
|
||||
|
||||
# 3. multiply
|
||||
def multiply_(args):
|
||||
|
||||
res = args[0]
|
||||
for arg in args[1:]:
|
||||
res *= arg
|
||||
return normalize(res)
|
||||
|
||||
# 4. divide
|
||||
def divide_(args):
|
||||
|
||||
res = args[0]
|
||||
for arg in args[1:]:
|
||||
res /= arg
|
||||
return normalize(res)
|
||||
|
||||
# 5. power
|
||||
def power_(args):
|
||||
|
||||
res = args[0]
|
||||
for arg in args[1:]:
|
||||
res **= arg
|
||||
return normalize(res)
|
||||
|
||||
# 6. square root
|
||||
def sqrt_(args):
|
||||
res = args[0]
|
||||
return normalize(math.sqrt(res))
|
||||
|
||||
# 7. 10th log
|
||||
def log_(args):
|
||||
# if only one argument is passed, it is 10th log
|
||||
if len(args) == 1:
|
||||
res = args[0]
|
||||
return normalize(math.log10(res))
|
||||
# if two arguments are passed, it is log with base as the second argument
|
||||
elif len(args) == 2:
|
||||
res = args[0]
|
||||
base = args[1]
|
||||
return normalize(math.log(res, base))
|
||||
else:
|
||||
raise Exception("Invalid number of arguments passed to log function")
|
||||
|
||||
# 8. natural log
|
||||
def ln_(args):
|
||||
res = args[0]
|
||||
return normalize(math.log(res))
|
||||
|
||||
|
||||
# 9. choose
|
||||
def choose_(args):
|
||||
n = args[0]
|
||||
r = args[1]
|
||||
return normalize(math.comb(n, r))
|
||||
|
||||
# 10. permutation
|
||||
def permutate_(args):
|
||||
n = args[0]
|
||||
r = args[1]
|
||||
return normalize(math.perm(n, r))
|
||||
|
||||
# 11. greatest common divisor
|
||||
def gcd_(args):
|
||||
res = args[0]
|
||||
for arg in args[1:]:
|
||||
res = math.gcd(res, arg)
|
||||
return normalize(res)
|
||||
|
||||
# 12. least common multiple
|
||||
def lcm_(args):
|
||||
res = args[0]
|
||||
for arg in args[1:]:
|
||||
res = res * arg // math.gcd(res, arg)
|
||||
return normalize(res)
|
||||
|
||||
# 13. remainder
|
||||
def remainder_(args):
|
||||
dividend = args[0]
|
||||
divisor = args[1]
|
||||
return normalize(dividend % divisor)
|
||||
@@ -0,0 +1,249 @@
|
||||
{
|
||||
"0":{
|
||||
"ID":0,
|
||||
"standardized_name":"add_",
|
||||
"API_description":"'add_' returns the sum of all the arguments passed to it, normalized to 2 decimal places.",
|
||||
"Usage":{
|
||||
"required_parameters":[
|
||||
{
|
||||
"name":"input",
|
||||
"type":"List"
|
||||
}
|
||||
],
|
||||
"Example":{
|
||||
"Scenario":"if you want to add 2 to 1.",
|
||||
"Parameters":{
|
||||
"input":[2,1]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"1": {
|
||||
"ID": 1,
|
||||
"standardized_name": "subtract_",
|
||||
"API_description": "'subtract_' returns the difference of the arguments passed to it, starting with the first argument and subtracting all subsequent arguments, normalized to 2 decimal places.",
|
||||
"Usage": {
|
||||
"required_parameters": [
|
||||
{
|
||||
"name": "input",
|
||||
"type": "List"
|
||||
}
|
||||
],
|
||||
"Example": {
|
||||
"Scenario": "if you want to subtract 2 from 1.",
|
||||
"Parameters": {
|
||||
"input": [1,2]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"2": {
|
||||
"ID": 2,
|
||||
"standardized_name": "multiply_",
|
||||
"API_description": "'multiply_' returns the product of all the arguments passed to it, normalized to 2 decimal places.",
|
||||
"Usage": {
|
||||
"required_parameters": [
|
||||
{
|
||||
"name": "input",
|
||||
"type": "List"
|
||||
}
|
||||
],
|
||||
"Example": {
|
||||
"Scenario": "if you want to calculate 2*1.",
|
||||
"Parameters": {
|
||||
"input": [2,1]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"3": {
|
||||
"ID": 3,
|
||||
"standardized_name": "divide_",
|
||||
"API_description": "'divide_' returns the quotient of the first argument divided by all the subsequent arguments, normalized to 2 decimal places.",
|
||||
"Usage": {
|
||||
"required_parameters": [
|
||||
{
|
||||
"name": "input",
|
||||
"type": "List"
|
||||
}
|
||||
],
|
||||
"Example": {
|
||||
"Scenario": "if you want to calculate 4/2.",
|
||||
"Parameters": {
|
||||
"input": [4,2]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"4": {
|
||||
"ID": 4,
|
||||
"standardized_name": "power_",
|
||||
"API_description": "'power_' returns the result of raising the first argument to the power of all the subsequent arguments, normalized to 2 decimal places.",
|
||||
"Usage": {
|
||||
"required_parameters": [
|
||||
{
|
||||
"name": "input",
|
||||
"type": "List"
|
||||
}
|
||||
],
|
||||
"Example": {
|
||||
"Scenario": "if you want to calculate 2^3.",
|
||||
"Parameters": {
|
||||
"input": [2,3]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"5": {
|
||||
"ID": 5,
|
||||
"standardized_name": "sqrt_",
|
||||
"API_description": "'sqrt_' returns the square root of the first argument, normalized to 2 decimal places.",
|
||||
"Usage": {
|
||||
"required_parameters": [
|
||||
{
|
||||
"name": "input",
|
||||
"type": "List"
|
||||
}
|
||||
],
|
||||
"Example": {
|
||||
"Scenario": "if you want to get the square root of 9.",
|
||||
"Parameters": {
|
||||
"input": [9]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"6": {
|
||||
"ID": 6,
|
||||
"standardized_name": "log_",
|
||||
"API_description": "'log_' returns the base-10 logarithm of the first argument if one argument is provided, or the logarithm with base as the second argument if two arguments are provided, normalized to 2 decimal places.",
|
||||
"Usage": {
|
||||
"required_parameters": [
|
||||
{
|
||||
"name": "input",
|
||||
"type": "List"
|
||||
}
|
||||
],
|
||||
"Example": {
|
||||
"Scenario": "if you want to get the logarithm of 100 with base 10.",
|
||||
"Parameters": {
|
||||
"input": [100,10]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"7": {
|
||||
"ID": 7,
|
||||
"standardized_name": "ln_",
|
||||
"API_description": "'ln_' returns the natural logarithm of the first argument, normalized to 2 decimal places.",
|
||||
"Usage": {
|
||||
"required_parameters": [
|
||||
{
|
||||
"name": "input",
|
||||
"type": "List"
|
||||
}
|
||||
],
|
||||
"Example": {
|
||||
"Scenario": "if you want to get the natural logarithm of 2.718.",
|
||||
"Parameters": {
|
||||
"input": [2.718281828459045]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"8": {
|
||||
"ID": 8,
|
||||
"standardized_name": "lcm_",
|
||||
"API_description": "'lcm_' returns the least common multiple of all the arguments passed to it, normalized to 2 decimal places.",
|
||||
"Usage": {
|
||||
"required_parameters": [
|
||||
{
|
||||
"name": "input",
|
||||
"type": "List"
|
||||
}
|
||||
],
|
||||
"Example": {
|
||||
"Scenario": "if you want to find the LCM of 12 and 18.",
|
||||
"Parameters": {
|
||||
"input": [12,18]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"9": {
|
||||
"ID": 9,
|
||||
"standardized_name": "gcd_",
|
||||
"API_description": "'gcd_' returns the greatest common divisor of all the arguments passed to it, normalized to 2 decimal places.",
|
||||
"Usage": {
|
||||
"required_parameters": [
|
||||
{
|
||||
"name": "input",
|
||||
"type": "List"
|
||||
}
|
||||
],
|
||||
"Example": {
|
||||
"Scenario": "if you want to find the GCD of 54 and 24.",
|
||||
"Parameters": {
|
||||
"input": [54,24]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"10": {
|
||||
"ID": 10,
|
||||
"standardized_name": "remainder_",
|
||||
"API_description": "'remainder_' returns the remainder of the division of the first argument by the second argument, normalized to 2 decimal places.",
|
||||
"Usage": {
|
||||
"required_parameters": [
|
||||
{
|
||||
"name": "input",
|
||||
"type": "List"
|
||||
}
|
||||
],
|
||||
"Example": {
|
||||
"Scenario": "if you want to find the remainder of 10 divided by 3.",
|
||||
"Parameters": {
|
||||
"input": [10,3]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"11": {
|
||||
"ID": 11,
|
||||
"standardized_name": "choose_",
|
||||
"API_description": "'choose_' returns the number of ways to choose 'r' items from 'n' options without regard to order, normalized to 2 decimal places.",
|
||||
"Usage": {
|
||||
"required_parameters": [
|
||||
{
|
||||
"name": "input",
|
||||
"type": "List"
|
||||
}
|
||||
],
|
||||
"Example": {
|
||||
"Scenario": "if you want to choose 2 items out of 5.",
|
||||
"Parameters": {
|
||||
"input": [5,2]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"12": {
|
||||
"ID": 12,
|
||||
"standardized_name": "permutate_",
|
||||
"API_description": "'permutate_' returns the number of ways to arrange 'r' items out of 'n' options, normalized to 2 decimal places.",
|
||||
"Usage": {
|
||||
"required_parameters": [
|
||||
{
|
||||
"name": "input",
|
||||
"type": "List"
|
||||
}
|
||||
],
|
||||
"Example": {
|
||||
"Scenario": "if you want to find the number of arrangements of 3 items out of 5.",
|
||||
"Parameters": {
|
||||
"input": [5,3]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
{"ID": 0, "description": "'add_' returns the sum of all the arguments passed to it, normalized to 2 decimal places."}
|
||||
{"ID": 1, "description": "'subtract_' returns the difference of the arguments passed to it, starting with the first argument and subtracting all subsequent arguments, normalized to 2 decimal places."}
|
||||
{"ID": 2, "description": "'multiply_' returns the product of all the arguments passed to it, normalized to 2 decimal places."}
|
||||
{"ID": 3, "description": "'divide_' returns the quotient of the first argument divided by all the subsequent arguments, normalized to 2 decimal places."}
|
||||
{"ID": 4, "description": "'power_' returns the result of raising the first argument to the power of all the subsequent arguments, normalized to 2 decimal places."}
|
||||
{"ID": 5, "description": "'sqrt_' returns the square root of the first argument, normalized to 2 decimal places."}
|
||||
{"ID": 6, "description": "'log_' returns the base-10 logarithm of the first argument if one argument is provided, or the logarithm with base as the second argument if two arguments are provided, normalized to 2 decimal places."}
|
||||
{"ID": 7, "description": "'ln_' returns the natural logarithm of the first argument, normalized to 2 decimal places."}
|
||||
{"ID": 8, "description": "'lcm_' returns the least common multiple of all the arguments passed to it, normalized to 2 decimal places."}
|
||||
{"ID": 9, "description": "'gcd_' returns the greatest common divisor of all the arguments passed to it, normalized to 2 decimal places."}
|
||||
{"ID": 10, "description": "'remainder_' returns the remainder of the division of the first argument by the second argument, normalized to 2 decimal places."}
|
||||
{"ID": 11, "description": "'choose_' returns the number of ways to choose 'r' items from 'n' options without regard to order, normalized to 2 decimal places."}
|
||||
{"ID": 12, "description": "'permutate_' returns the number of ways to arrange 'r' items out of 'n' options, normalized to 2 decimal places."}
|
||||
Reference in New Issue
Block a user