Skip to content

Instantly share code, notes, and snippets.

def bin_palindrome(n):
"""Checks whether binary representation of integer n is a palindrome in O(#1)."""
ones = []
while n > 0:
# find position j of least significant lit bit (one); x = 2^j
j = (n & (n - 1)) ^ n
ones.append(j)
n -= j
if len(ones) == 0:
@mstepniowski
mstepniowski / testing.py
Last active December 17, 2015 07:49
Custom Django test suite runners, which can be used to speed up your test suite.
# Copyright (c) 2011-2013, Marek Stepniowski
# All rights reserved. (FreeBSD License).
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
@mstepniowski
mstepniowski / hashswag.py
Created March 16, 2013 20:50
PyCon 2013 Twitter #HASHSWAG Challenge
from heapq import heappush, heappop
############################
# Game rules
############################
def next_positions(board):
return [(execute_move(board, move), move) for move in possible_moves(board)]
def possible_moves(board):
@mstepniowski
mstepniowski / anagrams.py
Created March 16, 2013 17:42
PyCon 2013 Thumbtack Anagram Challenge
import re
import sys
from collections import defaultdict
from itertools import combinations, chain
def powerset(iterable):
"""powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"""
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
@mstepniowski
mstepniowski / _hackercup_2013_round_1.rst
Last active December 12, 2015 02:58
Solutions to Facebook Hacker Cup Round 1 problems (in Python)

Card Game

John is playing a game with his friends. The game's rules are as follows: There is deck of N cards from which each person is dealt a hand of K cards. Each card has an integer value representing its strength. A hand's strength is determined by the value of the highest card in the hand. The person with the strongest hand wins the round. Bets are placed before each player reveals the strength of their hand.

John needs your help to decide when to bet. He decides he wants to bet when the strength of his hand is higher than the average hand strength. Hence John wants to calculate the average strength of ALL possible sets of hands. John is very good at division, but he needs your help in calculating the sum of the strengths of all possible hands.

Problem You are given an array a with N ≤ 10 000 different integer numbers and a number, K, where 1 ≤ K ≤ N. For all possible subsets of a of size K find the sum of their maximal elements modulo 1 000 000 007.

@mstepniowski
mstepniowski / Tree.hs
Created January 29, 2013 22:03
2-3 tree implemented in Haskell
-- Module implementing a dictionary-like abstract data structure
-- on a 2-3 tree concrete data structure <en.wikipedia.org/wiki/2-3_tree>.
module Tree (Tree,
empty,
singleton,
Tree.lookup,
insert,
fromList,
toList) where
@mstepniowski
mstepniowski / runtests.py
Created January 29, 2013 20:51
Run tests of a standalone Django package.
import sys
from django.conf import settings
settings.configure(
DEBUG=True,
DATABASES={'default': {'ENGINE': 'django.db.backends.sqlite3'},
'secondary': {'ENGINE': 'django.db.backends.sqlite3'}},
INSTALLED_APPS=['django.contrib.auth',
'django.contrib.contenttypes',
@mstepniowski
mstepniowski / _hackercup_2013_qualifications.rst
Last active April 23, 2020 21:51
Solutions to Facebook Hacker Cup 2013 Qualification Round problems (in Python)

Beautiful strings

When John was a little kid he didn't have much to do. There was no internet, no Facebook, and no programs to hack on. So he did the only thing he could... he evaluated the beauty of strings in a quest to discover the most beautiful string in the world.

Given a string s, little Johnny defined the beauty of the string as the sum of the beauty of the letters in it.

The beauty of each letter is an integer between 1 and 26, inclusive, and no two letters have the same beauty. Johnny doesn't care about whether letters are uppercase or lowercase, so that doesn't affect the beauty of a letter. (Uppercase 'F' is exactly as beautiful as lowercase 'f', for example.)

You're a student writing a report on the youth of this famous hacker. You found the string that Johnny considered most beautiful. What is the maximum possible beauty of this string?

@mstepniowski
mstepniowski / adventure.py
Created January 20, 2013 00:32
My little Python helper for http://adventure.cueup.com/
#########
# LEVEL 1
#########
RANDOM_SEQ = [6, 19, 16]
def vax_mth_random(seed):
"""VAX's MTH$RANDOM is a linear congruential generator.
@mstepniowski
mstepniowski / results.txt
Last active December 10, 2015 22:31
Script showing top 25 talk proposals for DjangoCon Europe 2013
# 25 best talk proposals as of Saturday, 19th of January at 11:59 PM UTC
# ----------------------------------------------------------------------
# Calculated by votes.py from 15383 votes of 790 people.
1. (411) Asynchronous code in Django.
2. (398) How to combine JavaScript & Django in a smart way
3. (375) Website security in Django
4. (364) Advanced PostgreSQL in Django
5. (359) Designing a good API: Lessons Learned
6. (344) Let’s bankrupt Heroku: make your Django run fast