Skip to content

Instantly share code, notes, and snippets.

@sam-lb
Created May 29, 2021 14:06
Show Gist options
  • Save sam-lb/fb1337c7e22c4a66f17cf4ee594baea3 to your computer and use it in GitHub Desktop.
Save sam-lb/fb1337c7e22c4a66f17cf4ee594baea3 to your computer and use it in GitHub Desktop.
# convert python data to desmos tables/lists
# http://sambrunacini.com
def to_desmos_list(iterable):
""" convert a python list of numbers or points into a string that can be pasted into desmos """
iterable = list(iterable)
string = ",".join([r"("+fix_point(element)+r")" for element in iterable])
return r"\left[" + string.replace(r"(",r"\left(").replace(r")",r"\right)") + r"\right]"
def fix_scientific_notation(num_string):
if num_string.find("e") != -1:
sides = num_string.split("e")
result = str(float(sides[0])) + r"\cdot10^{" + str(float(sides[1])) + "}"
return result
return num_string
def fix_point(point):
point = str(point)[1:][:-1].replace(" ","").split(",")
return fix_scientific_notation(point[0]) + "," + fix_scientific_notation(point[1])
def to_desmos_table(iterable):
""" convert a python list of points to a string that can be pasted into desmos to make a table """
string = ""
for point in iterable:
fixed = fix_point(point) + "\n"
string += fixed
return string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment