Skip to content

Instantly share code, notes, and snippets.

@mtayseer
mtayseer / euler_2.py
Last active August 29, 2015 13:55
Project euler #2
def fibonacci(max):
a, b = 1, 2
while a < max:
yield a
a, b = b, a+b
print sum(x for x in fibonacci(4000000) if x % 2 == 0)
import base64;exec base64.b64decode('ZGVmIGZpYm9uYWNjaShtYXgpOgogICAgYSwgYiA9IDEsIDIKICAgIHdoaWxlIGEgPCBtYXg6CiAgICAgICAgeWllbGQgYQogICAgICAgIGEsIGIgPSBiLCBhK2IKCnByaW50IHN1bSh4IGZvciB4IGluIGZpYm9uYWNjaSg0MDAwMDAwKSBpZiB4ICUgMiA9PSAwKQ==')
#
# Example init.py for documentation purposes
#
# Use this file as a template/example for creating an
# initialization/configuration script for PyCmd. Scripts are loaded and applied
# based on the following rules:
#
# * If present, an init.py script in PyCmd's installation directory is
# automatically executed and defines "global" (system-wide) settings
#
def sieve(numbers, n):
for i in range(n * 2, len(numbers), n):
numbers[i] = False
def skip_to_next_prime(numbers, n):
n += 1
while n < max and not numbers[n]:
n += 1
return n
def is_palindrome(x):
x = str(x)
return x == x[::-1]
def find_palindromes(m, n):
for i in xrange(m, n):
for j in xrange(i, n):
x = i * j
if is_palindrome(x):
yield x
# Translation of https://gist.github.com/alaafqandil/9009412 to Python
def multiple_of_3x3(num):
return any(num % i == 0 and len(str(num / i)) == 3 for i in reversed(xrange(100, 1000)))
def find_max_palindrome():
for i in reversed(xrange(10)):
for j in reversed(xrange(10)):
for k in reversed(xrange(10)):
palindrome = int('{0}{1}{2}{2}{1}{0}'.format(i, j, k))
if palindrome < 999 ** 2:
@mtayseer
mtayseer / compress_images.py
Created February 23, 2014 11:55
Given a directory, compress all images in it
import sys, os
from PIL import Image
files = os.listdir(sys.argv[1])
for i, f in enumerate(files):
fname = os.path.join(sys.argv[1], f)
im = Image.open(fname).save(fname)
print 'Compressing {} of {}'.format(i, len(files))
@mtayseer
mtayseer / watch_n_serve.py
Last active August 29, 2015 13:57
Launch a webserver to view generated output & at the same time watch the content for changes & rebuild it.
# [Pelican](http://docs.getpelican.com/) is a static site generator. I'm using it for building [my own website](http://mtayseer.net/).
# To make the editing experience easier, I created this script to launch a webserver to view generated output & at the same time watch
# the content for changes & rebuild it.
#
# I'm using [Bottle](http://bottlepy.org/) for the webserver & [watchdog](https://pythonhosted.org/watchdog/) to watch directory
#
from bottle import route, run, static_file
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
@mtayseer
mtayseer / local_file_server.py
Created March 15, 2014 06:59
Static files server using Bottle web framework
'''Static files server using Bottle web framework'''
# You can use the command `python -m SimpleHTTPServer`, but when you want to
# do more, use this snippet
from bottle import route, run, static_file
@route('/')
@route('/<path:path>')
def serve(path='index.html'):
response = static_file(path, root=OUTPUT_ROOT)
@mtayseer
mtayseer / kill_portal.sh
Created March 16, 2014 09:47
One-liner which kills a Unix process given its name
# Kill a process, given part of its name
#
# How I'm doing it:-
#
# 1. Get a list of all processes
# 2. Grep for any process where "portal" is part of the
# command line used to launch it
# 3. I will get 2 processes: 1 of them is what I'm searching for &
# the other is just this grep command I'm running. I remove it by
# removing anything with "grep" in its command line