Skip to content

Instantly share code, notes, and snippets.

@habnabit
Created February 6, 2018 22:24
Show Gist options
  • Save habnabit/c84f2e397ea0c159d2e20d26588d7fd8 to your computer and use it in GitHub Desktop.
Save habnabit/c84f2e397ea0c159d2e20d26588d7fd8 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import random"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[6, 10, 7, 13]"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"albums = [random.randrange(5, 15) for _ in xrange(4)]\n",
"albums"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([[0, 0, 0, 0],\n",
" [1, 0, 0, 0],\n",
" [1, 0, 0, 0],\n",
" [1, 0, 0, 0],\n",
" [1, 0, 0, 0],\n",
" [0, 2, 0, 0],\n",
" [1, 2, 0, 4],\n",
" [1, 0, 0, 4],\n",
" [0, 2, 0, 4],\n",
" [0, 0, 0, 4],\n",
" [0, 2, 0, 4],\n",
" [0, 0, 0, 0],\n",
" [0, 2, 0, 4],\n",
" [0, 0, 0, 4],\n",
" [0, 2, 0, 4],\n",
" [0, 2, 0, 4],\n",
" [0, 0, 0, 0],\n",
" [0, 2, 0, 4],\n",
" [0, 0, 3, 4],\n",
" [0, 2, 3, 4],\n",
" [0, 0, 3, 4],\n",
" [0, 2, 3, 0],\n",
" [0, 0, 3, 0],\n",
" [0, 0, 3, 0],\n",
" [0, 0, 3, 0],\n",
" [0, 0, 0, 0]]),\n",
" array([1, 1, 1, 1, 2, 1, 2, 4, 1, 4, 2, 4, 4, 2, 4, 2, 4, 4, 2, 4, 2, 4, 2,\n",
" 4, 3, 4, 2, 3, 4, 3, 4, 2, 3, 3, 3, 3]))"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def stretch_shuffle(albums):\n",
" space = max(albums) * 2\n",
" mat = np.zeros((space, len(albums)), dtype=int)\n",
" for e, album in enumerate(albums, start=1):\n",
" start = random.randrange(space - album)\n",
" stop = random.randrange(start + album, space)\n",
" step = float(stop - start) / album\n",
" indices = np.arange(start, stop, step).astype(int)\n",
" assert len(indices) == len(set(indices)) == album\n",
" mat[indices,e - 1] = e\n",
" flat = mat.reshape(-1)\n",
" return mat, flat[flat > 0]\n",
"\n",
"stretch_shuffle(albums)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment