Skip to content

Instantly share code, notes, and snippets.

View pmallory's full-sized avatar

Philip Mallory pmallory

View GitHub Profile
@pmallory
pmallory / ThumbnailGetter.py
Created November 9, 2016 23:54
Search a page for a representative image (a big, square one). Inspired by the code Reddit uses to pick images to put next to headlines
import io
import sys
import urllib
import bs4
import requests
from PIL import Image
def get_image_list(url):
"""Given the url of an HTML document, return a list of urls of all images
@pmallory
pmallory / WikipediaCrawl.py
Last active May 10, 2022 00:38
Crawl Wikipedia, starting from a random article. Click the first link in each article and see where we wind up! spoiler alert: probably at the Philosophy article
import time
import urllib
import bs4
import requests
start_url = "https://en.wikipedia.org/wiki/Special:Random"
target_url = "https://en.wikipedia.org/wiki/Philosophy"
@pmallory
pmallory / filmlist.py
Last active March 23, 2016 20:48
filmlist.py
films = [('Brief Encounter', '1945'),
('Casablanca', '1942'),
('Before Sunrise', '1995'),
('Before Sunset', '2004'),
('Breathless', '1960'),
('In the Mood for Love', '2000'),
('The Apartment', '1960'),
('Hannah & Her Sisters', '1986'),
('Eternal Sunshine of the Spotless Mind', '2004'),
('Room With a View', '1985'),
@pmallory
pmallory / githubrepo.py
Last active March 18, 2016 23:47
A class that demonstrates the iterable interface and generator functions.
import math
import requests
class GitHubRepo:
"""
A demonstration of iterables.
You can iterate over the commits made to a GitHub repo by iterating over
an instance of this class.
P1 - Movie Trailer Website - Eduardo
# Project: Movie Trailer Website - [Author’s Name]
================================
## Required Libraries and Dependencies
-----------------------------------
[In this section list your project’s dependencies. A basic Movie Website Trailer project requires Python v2.* to be installed]
def fact(n):
result =1
for i in range(1, n+1):
result *= i
return result
{
"retirementStartYear": 2014,
"retirementEndYear": 2043,
"portfolio": {
"initial": 8000,
"percentEquities": 75,
"percentBonds": 25,
"percentGold": 0,
"percentCash": 0,
"percentFees": 0,
# coding: utf-8
import csv
from collections import Counter
from pprint import pprint
with open('./440801.csv') as f:
# data from
obs = []
csv = csv.DictReader(f)
for line in csv:
@pmallory
pmallory / stairs.py
Last active March 1, 2017 01:29
Stairs problem: dynamic programming solution
from decimal import *
def count_ways(n, max_step_size=2):
table = [Decimal(1), Decimal(1), Decimal(2)]
assert len(table) >= max_step_size
for i in xrange(3,n+1):
table.append(sum(table[i-max_step_size:i]))
return table[n]
@pmallory
pmallory / binary_search.py
Created October 30, 2014 17:39
Binary search
import matplotlib.pyplot as plt
import random
def binary_search(target, epsilon):
guess = 1.0
bottom = 1.0
top = float("inf")
steps = 0
while abs(target-guess)>epsilon: