Skip to content

Instantly share code, notes, and snippets.

View ndunn219's full-sized avatar

Nat Dunn ndunn219

View GitHub Profile
# import modules
import re
def sort_streets(street_list):
"""
Sort streets alphabetically, ignoring cardinal direction prefixes such as North, South, East and West
:param street_list: list of street names
"""
prefix = re.compile('^North|South|East|West|N\.?|S\.?|E\.?|W\.$', re.IGNORECASE)
@ndunn219
ndunn219 / monty_hall.py
Last active October 23, 2015 17:05
This is a Python model for the Monty Hall problem. See comment below for credit.
import random
class MontyHall:
def __init__(self):
self._prize_door = random.randint(1,3)
self._selected_door = random.randint(1,3)
self._removed_door = self.remove_door()
def remove_door(self):
doors = [1,2,3]
@ndunn219
ndunn219 / FixMarkDownHeaders.ipynb
Last active November 6, 2015 15:08
Adds a space after series of hash marks at the beginning of lines. The purpose is to fix markdown headers that didn't include a space after heading hash marks. USE AT YOUR OWN RISK.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ndunn219
ndunn219 / adventure.py
Created February 3, 2016 17:46
This script was created as part of a series of training videos based on Python projects created by Shelly Tan of Northwestern University's Knight Lab. The video explaining the code is at https://youtu.be/8uJFN7OZ2Yo
import random
def msg(room):
if room['msg'] == '': #There is no custom message
return "You have entered the " + room['name'] + '.'
else:
return room['msg']
def get_choice(room,dir):
if dir=='N':
@ndunn219
ndunn219 / StackExchange PY2 v. PY3.ipynb
Created March 10, 2016 16:10
IPython Notebook creating plots showing trend in StackExchange posts tagged Python 2 and Python 3
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ndunn219
ndunn219 / sitemap_checker.py
Last active November 19, 2022 17:18
This code shows how to check a sitemap to make sure there are no links pointing to missing pages and to see if 301s are working correctly. It is explained at https://www.webucator.com/blog/2016/05/checking-your-sitemap-for-broken-links-with-python/
import requests
from bs4 import BeautifulSoup
sitemap = 'http://www.nasa.gov/sitemap/sitemap_nasa.html'
r = requests.get(sitemap)
html = r.content
soup = BeautifulSoup(html, 'html.parser')
links = soup.find_all('a')
from bs4 import BeautifulSoup
import requests
import pandas as pd
url = "https://www.akc.org/reg/dogreg_stats.cfm"
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data, "lxml")
import os, re
dir = 'path-to-your-directory'
i=0
results = [] #Store changed files so you can look into it later
for dirname, dirnames, filenames in os.walk(dir):
for filename in filenames:
ext = filename.split('.')[-1] #Get Extension
if ext in ('cfc','cfm'): #Limit search to specific file types
@ndunn219
ndunn219 / srt_to_txt.py
Last active May 5, 2023 13:18
Simple Python Script for Extracting Text from an SRT File
"""
Creates readable text file from SRT file.
"""
import re, sys
def is_time_stamp(l):
if l[:2].isnumeric() and l[2] == ':':
return True
return False
@ndunn219
ndunn219 / combo_lock.py
Created October 5, 2019 23:08
How to pick a combo lock.
# based on https://youtu.be/w4wkCcsWs4c
def guess_numbers(num1, num2):
nums = [num1, num1+10, num1+20, num1+30,
num2, num2+10, num2+20, num2+30]
return nums
def main():
print('''The first step is to find the "Sticky number"
by applying pressure to the shackle and turning clockwise