Skip to content

Instantly share code, notes, and snippets.

Avatar

Karol Kuczmarski Xion

View GitHub Profile
@Xion
Xion / truck_time.py
Created Nov 3, 2011
Very simple optimization problem
View truck_time.py
# The wreck of a plane in a desert is 15 miles from the nearest point A on a straight road.
# A rescue truck starts for the wreck at a point on the road that is 30 miles distant from A.
# If the truck can travel at 80 mph on the road and at 40 mph on a straight path in the desert,
# how far from A should the truck leave the road to reach the wreck in a minimum time?
from math import sqrt
def truck_time(turn_point):
road_distance = turn_point
skipped_road_distance = 30 - turn_point
@Xion
Xion / compact.py
Created Nov 13, 2011
PHP compact() function in Python
View compact.py
def compact(*args):
''' compact() function, analoguous to the PHP version.
Converts a series of (possible nested) iterables containing variable names
into a dictionary mapping those names to variable values.
Example:
>>> x, y, z, a, b, c = 1, 2, 3, 4, 5, 6
>>> compact('x', ['y','z'], ['a', ['b'], [['c']]])
{'a': 4, 'b': 5, 'c': 6, 'x': 1, 'y': 2, 'z': 3}
'''
def _compact_arg(res, arg):
@Xion
Xion / Coded4.hs
Created Dec 27, 2011
Haskell implementation of coded4 analyzer (simplified)
View Coded4.hs
-- Coded4.hs
-- Simplified version of coded4 analyzer (http://github.com/Xion/coded4)
-- Supports only Git repositories and produces less neatly formatted output
-- usage:
-- $ runghc Coded4.hs <path-to-git-repo>
module Coded4 where
View jinja_capture.py
from jinja2.ext import Extension
from jinja2.nodes import CallBlock, Output, MarkSafe
class CaptureExtension(Extension):
''' Generic capture extension, useful for providing
content that should go into specific places in HTML template,
such as the <head> tag for JavaScript includes.
Syntax for providing content for so-called 'dataset' (here it's "js"):
@Xion
Xion / gist:1804474
Created Feb 11, 2012
A neat little numerical puzzle
View gist:1804474
This puzzle is (allegedly) solved by elementary school children in 5 to 10 minutes,
but adults usually struggle with it much longer.
8809=6
7111=0
2172=0
6666=4
1111=0
3213=0
7662=2
@Xion
Xion / monads.py
Created Mar 23, 2012
Monads in Python!
View monads.py
# Monadic IO
def print_(s):
def _():
print s
return _
def some_io():
for x in ['Hello', 'World']:
yield print_(x)
@Xion
Xion / missing_dict.py
Created Apr 5, 2012
dict with "missing" values
View missing_dict.py
import collections
missing = object()
class MissingDict(dict):
"""Dictionary that supports a special 'missing' value.
Assigning a missing value to key will remove the key from dictionary.
Initializing a key with missing value will result in key not being
added to dictionary at all.
@Xion
Xion / pytz_patch.py
Created May 6, 2012
Patching pytz with import hook to have usable generic timezones
View pytz_patch.py
"""
Import hook extending pytz package with usable, generic timezones:
GMT-14 up to GMT+12.
Note that pytz already has Etc/GMT+X timezones, but
(quoting Wikipedia):
"In order to conform with the POSIX style, those zones beginning with "Etc/GMT"
have their sign reversed from what most people expect. In this style,
zones west of GMT have a positive sign and those east have a negative sign."
@Xion
Xion / keyword_stats.py
Created May 28, 2012
Analysis of keywords in few popular languages
View keyword_stats.py
"""
Analyzing keywords from different programming languages.
"""
def word_stats(words):
count = len(words)
len_sum = sum(map(len, words))
return {
'count': count,
'total_chars': len_sum,
@Xion
Xion / almost_roguelike.py
Created Aug 5, 2012
Arrow-controlled movement in console with ncurses and Python
View almost_roguelike.py
#!/usr/bin/env python
import curses
SHIFTS = {
curses.KEY_LEFT: (0, -1),
curses.KEY_UP: (-1, 0),
curses.KEY_RIGHT: (0, 1),
curses.KEY_DOWN: (1, 0),
You can’t perform that action at this time.