This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def update_location(bodies, time_step = 1): | |
for target_body in bodies: | |
target_body.location.x += target_body.velocity.x * time_step | |
target_body.location.y += target_body.velocity.y * time_step | |
target_body.location.z += target_body.velocity.z * time_step |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def run_simulation(bodies, names = None, time_step = 1, number_of_steps = 10000, report_freq = 100): | |
#create output container for each body | |
body_locations_hist = [] | |
for current_body in bodies: | |
body_locations_hist.append({"x":[], "y":[], "z":[], "name":current_body.name}) | |
for i in range(1,number_of_steps): | |
compute_gravity_step(bodies, time_step = 1000) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def compute_velocity(bodies, time_step = 1): | |
for body_index, target_body in enumerate(bodies): | |
acceleration = calculate_single_body_acceleration(bodies, body_index) | |
target_body.velocity.x += acceleration.x * time_step | |
target_body.velocity.y += acceleration.y * time_step | |
target_body.velocity.z += acceleration.z * time_step |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#planet data (location (m), mass (kg), velocity (m/s) | |
sun = {"location":point(0,0,0), "mass":2e30, "velocity":point(0,0,0)} | |
mercury = {"location":point(0,5.7e10,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)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def compute_gravity_step(bodies, time_step = 1): | |
compute_velocity(bodies, time_step = time_step) | |
update_location(bodies, time_step = time_step) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_reddit_agent(user_agent, client_id, client_secret, redirect='http://127.0.0.1'): | |
reddit_agent = praw.Reddit(user_agent = user_agent) | |
reddit_agent.set_oauth_app_info(client_id = client_id, | |
client_secret = client_secret, | |
redirect_uri = redirect) | |
return reddit_agent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def read_file(filename): | |
with open(filename, "r") as file: | |
contents = file.read().replace('\n\n',' ') | |
return contents |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def write_file(filename, message): | |
with open(filename, "w") as file: | |
file.write(message) |