Skip to content

Instantly share code, notes, and snippets.

@mkolod
Created September 14, 2019 16:33
Show Gist options
  • Save mkolod/08670efb1b1e0f762294fe29e99559a3 to your computer and use it in GitHub Desktop.
Save mkolod/08670efb1b1e0f762294fe29e99559a3 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import copy\n",
"import random"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def shuffle(x):\n",
" data = copy.deepcopy(x)\n",
" for hi in range(len(data)-1, -1, -1):\n",
" idx = random.randint(0, hi)\n",
" # Swap value at idx and at hi\n",
" # We store the shuffled data at the end (hi),\n",
" # and then keep shuffling the remainder (0, hi-1)\n",
" data[hi], data[idx] = data[idx], data[hi]\n",
" return data"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"data = list(range(20))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n",
"[5, 17, 6, 16, 0, 18, 11, 9, 4, 8, 1, 14, 19, 13, 12, 2, 3, 10, 15, 7]\n"
]
}
],
"source": [
"shuffled = shuffle(data)\n",
"print(data)\n",
"print(shuffled)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Check that there is no replacement\n",
"# The original data can have replacement also,\n",
"# so we need to make sure we have the same counts\n",
"# of each item.\n",
"\n",
"def validate_shuffle(orig_data, shuffled_data):\n",
" for orig, shuffled in zip(sorted(orig_data), sorted(shuffled_data)):\n",
" if orig != shuffled:\n",
" return False\n",
" return True"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"validate_shuffle(data, shuffled)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment