Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python
# Four men want to cross a bridge. The bridge is of a width such that a
# maximum of 2 people may cross at a time.
#
# One man takes 10 minutes to cross, another takes 5 minutes to cross,
# another takes 2 minutes to cross, and the last takes 1 minute to
# cross.
# I will call these men A, B, C, and D, respectively.
#!/usr/bin/env python
import timeit
def w_swap(min, max):
if min > max: min, max = max, min
def w_builtin(x, y):
min_val, max_val = min(x, y), max(x, y)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>untitled</title>
<script type="text/javascript"
src="http://www.google.com/jsapi"></script>
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# http://stackoverflow.com/questions/3744048/python-how-to-merge-a-list-into-clusters/3744095#3744095
import itertools
def merge_it(lot):
"""
>>> merge_it( [(3,4), (18,27), (4,14)] )
@miku
miku / mysqldump2sqlite3.sh
Created December 14, 2010 20:28
Convert a mysql database dump into something sqlite3 understands.
#!/bin/sh
# ================================================================
#
# Convert a mysql database dump into something sqlite3 understands.
#
# Adapted from
# http://stackoverflow.com/questions/489277/script-to-convert-mysql-dump-sql-file-into-format-that-can-be-imported-into-sqlit
#
# (c) 2010 Martin Czygan <martin.czygan@gmail.com>
@miku
miku / find-the-two-numbers-in-a-sorted-array-which-sum-up-to-given-number.py
Created December 15, 2010 02:10
Given a sorted array of integers and a target integer, find a pair of integers in the array that sum to the target in linear time.
#!/usr/bin/env python
# http://www.disinformatics.com/blog/2010/12/14/a-google-internship-interview-question-in-scala/
# The question is:
# given a sorted array of integers and a target integer,
# find a pair of integers in the array that sum to the target in linear time.
# The Solution
#
@miku
miku / codility-sample-test.py
Created December 16, 2010 17:19
Codility.com sampletest 100% in python.
#!/usr/bin/env python
def equi(A):
upper = sum(A)
lower = 0
for position in range(0, len(A)):
if upper - A[position] == lower:
return position
else:
lower += A[position]
@miku
miku / maximize.py
Last active September 16, 2016 08:06
Find the maximal difference between items u, v (where index(u) < index(v)) of a given sequence ``a``. Think stockmarket prices. Dynamic programming.
#!/usr/bin/env python
def maximize(a):
""" Find the maximal difference between items u, v
(where index(u) < index(v)) of a given sequence ``a``.
Think stockmarket prices.
Returns the actual difference, start and end position in ``a`` and
the sublist yielding the best result.
#!/usr/bin/env python
# http://stackoverflow.com/questions/4585255/how-to-filter-all-words-which-contain-n-or-more-characters/4585315
import sys, re
def morethan(n, file_or_string):
try:
content = open(file_or_string, 'r').read()
except:
@miku
miku / slideshow.js
Created January 20, 2011 23:48
Create a simple slideshow that cycles through the images...
<!DOCTYPE html>
<head>
<meta name="found-at" content="http://www.computedstyle.com/2010/12/hiring-front-end-engineers.html">
<meta name="description" content="A div with an id of 'slideshow' contains
five images, the first of which is shown and the others are hidden using a
display style of none. Using Javascript, create a simple slideshow that cycles
through the images, displaying each image for three seconds at a time, looping
back to the first image when the end is reached. You cannot use jQuery or any
other library.">