Skip to content

Instantly share code, notes, and snippets.

View PEZ's full-sized avatar
🎯
Focusing

Peter Strömberg PEZ

🎯
Focusing
View GitHub Profile
" Vim syntax file
" Language: Haml (XHTML Abstraction Markup Language)
" Maintainer: Dmitry A. Ilyashevich <dmitry.ilyashevich@gmail.com>
" License: This file can be redistribued and/or modified under the same terms
" as Vim itself.
"
" Version: 0.3.it
" Last Change: 2008-10-21
" Notes: Last synced with Haml 1.8
" TODO: Support for indented multiline sections
@PEZ
PEZ / factors.py
Created January 14, 2009 00:03
A Python solution to Project Euler Problem 3
def factors(num):
"""Generates the factors for num.
By PEZ, from a description by BradC over at Stackoverflow.com
http://stackoverflow.com/questions/439814#439850
"""
from math import sqrt
def _candidates():
"""Generates the sequence 2, then all odd ints starting with 3"""
@PEZ
PEZ / euler1.py
Created January 14, 2009 18:32
Python solutions to Project Euler Problem 1
"""Find the sum of all numbers below 1000 divisible by 3 or 5.
http://projecteuler.net/index.php?section=problems&id=1"""
#Brute force #1
sum(x for x in xrange(3,1000) if x % 3 == 0 or x % 5 == 0)
#Brute force #2
sum(set(range(3, 1000, 3) + range(5, 1000, 5)))
#Less brute (after reading the problem discussion PDF)
"""Find the largest palindrom that is the product of two three-digit factors:
http://projecteuler.net/index.php?section=problems&id=4"""
# Brute force
def big_products(max):
for a in xrange(100, 1000):
for b in xrange(100, 1000):
yield str(a * b)
max(int(p) for p in big_products(999) if p == p[::-1])
@PEZ
PEZ / euler99.py
Created January 14, 2009 16:04
A Python solution to Project Euler Problem 99
""" Project Euler Problem 99 http://projecteuler.net/index.php?section=problems&id=99 """
def max_base_exp(base_exps):
from math import log
return reduce(lambda a, b: a if a[2] * log(a[1]) > b[2] * log(b[1]) else b, base_exps)
print max_base_exp([
(1, 519432, 525806),
(2, 632382, 518061),
(3, 78864, 613712),
(4, 466580, 530130),
@PEZ
PEZ / euler2.py
Created January 14, 2009 20:16
Project Euler Problem 2, some Python solutions
"""Sum of all even fibonacci numbers below 4000000
http://projecteuler.net/index.php?section=problems&id=2"""
#Brute force
def fibs_up_to(t, a=1, b=2):
while a < t:
yield a
a, b = b, a + b
sum(n for n in fibs_up_to(4000000) if n % 2 == 0)
=== Search on search ===
Contributors: cobpez
Donate link: http://wikimediafoundation.org/wiki/Support_Wikipedia/en
Tags: search, widget, search engines
Requires at least: 2.8
Tested up to: 2.9.1
Stable tag: trunk
A Widget that links to posts and pages matching search engine terms used to find the page.
@property
def type(self):
stripped = re.sub(r'Notification$', '', self.__class__.__name__)
if stripped != '':
return re.sub(r'([a-z])([A-Z])', '%s_%s' % (r'\1', r'\2'), stripped).lower()
else:
return 'generic'
@PEZ
PEZ / fbhack01.py
Created January 19, 2011 15:03
Solving double squares problem posted by Facebook
'''
Created on Jan 12, 2011
@author: cobpez
'''
import math
def double_squares(v):
for x in xrange(int(math.sqrt(v))):
y = math.sqrt(v - x*x)
@PEZ
PEZ / bf.sh
Last active December 13, 2015 21:29
A brainfuck bash frontend. Use like so: $ ./bf.sh '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.' Hello World! $
#!/bin/sh
CF="foo.c"
echo "#include <stdio.h>" > ${CF}
echo "int main() { char d[30000]; char *p=d;" >> ${CF}
echo $1 | while read -n1 c
do
case $c in
\>) echo "++p;" >> ${CF};;
\<) echo "--p;" >> ${CF};;
\+) echo "++*p;" >> ${CF};;