Skip to content

Instantly share code, notes, and snippets.

#planet info which can be added as parameters
sun = {"location":point(0,0,0), "mass":2e30, "velocity":point(0,0,0)}
mercury = {"location":point(0,5.0e7,0), "mass":3.285e23, "velocity":point(47000,0,0)}
venus = {"location":point(0,1.1e11,0), "mass":4.8e24, "velocity":point(35000,0,0)}
earth = {"location":point(0,1.5e11,0), "mass":6e24, "velocity":point(30000,0,0)}
mars = {"location":point(0,2.2e11,0), "mass":2.4e24, "velocity":point(24000,0,0)}
jupiter = {"location":point(0,7.7e11,0), "mass":1e28, "velocity":point(13000,0,0)}
saturn = {"location":point(0,1.4e12,0), "mass":5.7e26, "velocity":point(9000,0,0)}
uranus = {"location":point(0,2.8e12,0), "mass":8.7e25, "velocity":point(6835,0,0)}
neptune = {"location":point(0,4.5e12,0), "mass":1e26, "velocity":point(5477,0,0)}
@benrules2
benrules2 / Gravity.py
Last active November 14, 2023 16:39
Python N-Body Orbit Simulation
import math
import random
import matplotlib.pyplot as plot
from mpl_toolkits.mplot3d import Axes3D
class point:
def __init__(self, x,y,z):
self.x = x
self.y = y
@benrules2
benrules2 / Gravity.py
Last active March 12, 2020 14:31
Python N-Body Orbital Simulation
import math
import random
import matplotlib.pyplot as plot
from mpl_toolkits.mplot3d import Axes3D
class point:
def __init__(self, x,y,z):
self.x = x
self.y = y
self.z = z
@benrules2
benrules2 / Gravity.py
Created November 17, 2016 18:03
Python N-Body Orbit Simulation
def compute_gravity_step(bodies, time_step = 1):
compute_velocity(bodies, time_step = time_step)
update_location(bodies, time_step = time_step)
@benrules2
benrules2 / Markov.py
Created January 5, 2017 23:13
Read input file
def read_file(filename):
with open(filename, "r") as file:
contents = file.read().replace('\n\n',' ')
return contents
@benrules2
benrules2 / Markov.py
Created January 5, 2017 23:14
Generate Markov Chain
def build_chain(text, chain = {}):
words = text.split(' ')
index = 1
for word in words[index:]:
key = words[index - 1]
if key in chain:
chain[key].append(word)
else:
chain[key] = [word]
index += 1
@benrules2
benrules2 / Markov.py
Created January 5, 2017 23:29
Generate message from Markov Chain
def generate_message(chain, count = 100):
word1 = random.choice(list(chain.keys()))
message = word1.capitalize()
while len(message.split(' ')) < count:
word2 = random.choice(chain[word1])
word1 = word2
message += ' ' + word2
return message
@benrules2
benrules2 / Markov.py
Created January 5, 2017 23:31
File output containing message
def write_file(filename, message):
with open(filename, "w") as file:
file.write(message)
@benrules2
benrules2 / Markov.py
Created January 5, 2017 23:34
Main Markov Chain Function
if __name__ == '__main__':
message = read_file(sys.argv[1])
chain = build_chain(message)
message = generate_message(chain)
write_file("output.txt", message)
@benrules2
benrules2 / Markov.py
Created January 5, 2017 23:35
Markov Chain message generator
import random
import sys
def build_chain(text, chain = {}):
words = text.split(' ')
index = 1
for word in words[index:]:
key = words[index - 1]
if key in chain:
chain[key].append(word)