Instantly share code, notes, and snippets.

# Marek Stepniowskimstepniowski

• Sort options
Created May 15, 2014
View bin_palindrome.py
 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:
Last active Dec 17, 2015
Custom Django test suite runners, which can be used to speed up your test suite.
View testing.py
 # 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
Created Mar 16, 2013
View hashswag.py
 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):
Created Mar 16, 2013
PyCon 2013 Thumbtack Anagram Challenge
View anagrams.py
 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))
Last active Dec 12, 2015
Solutions to Facebook Hacker Cup Round 1 problems (in Python)
View _hackercup_2013_round_1.rst

# 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.

Created Jan 29, 2013
View Tree.hs
 -- Module implementing a dictionary-like abstract data structure -- on a 2-3 tree concrete data structure . module Tree (Tree, empty, singleton, Tree.lookup, insert, fromList, toList) where
Created Jan 29, 2013
Run tests of a standalone Django package.
View runtests.py
 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',
Last active Jan 23, 2020
Solutions to Facebook Hacker Cup 2013 Qualification Round problems (in Python)
View _hackercup_2013_qualifications.rst

# 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?

Created Jan 20, 2013
My little Python helper for http://adventure.cueup.com/