Skip to content

Instantly share code, notes, and snippets.

@robo-corg
Created June 30, 2016 20:17
Show Gist options
  • Select an option

  • Save robo-corg/1c46476f8495d746107f293ba257afe3 to your computer and use it in GitHub Desktop.

Select an option

Save robo-corg/1c46476f8495d746107f293ba257afe3 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": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"('x', ' ', 'o', 'x', 'x', 'x', 'o', ' ', 'o')"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tic_tac = (\n",
" 'x o',\n",
" 'xxx',\n",
" 'o o'\n",
")\n",
"\n",
"all_values = tuple(\n",
" val\n",
" for row in tic_tac\n",
" for val in row\n",
")\n",
"\n",
"all_values"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"('x', 'x', 'x', 'x')"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"just_xes = tuple(\n",
" val\n",
" for row in tic_tac\n",
" for val in row\n",
" if val == 'x'\n",
")\n",
"\n",
"just_xes"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(((0, 0), 'x'),\n",
" ((1, 0), ' '),\n",
" ((2, 0), 'o'),\n",
" ((0, 1), 'x'),\n",
" ((1, 1), 'x'),\n",
" ((2, 1), 'x'),\n",
" ((0, 2), 'o'),\n",
" ((1, 2), ' '),\n",
" ((2, 2), 'o'))"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"all_values_with_pos = tuple(\n",
" ((x_pos, y_pos), val)\n",
" for y_pos, row in enumerate(tic_tac)\n",
" for x_pos, val in enumerate(row)\n",
")\n",
"\n",
"all_values_with_pos"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(((0, 0), (1, 0), (2, 0)), ((0, 1), (1, 1), (2, 1)), ((0, 2), (1, 2), (2, 2)))"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rows = tuple(\n",
" tuple((x, y) for x in range(3))\n",
" for y in range(3)\n",
")\n",
"rows"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(((0, 0), (0, 1), (0, 2)), ((1, 0), (1, 1), (1, 2)), ((2, 0), (2, 1), (2, 2)))"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cols = tuple(\n",
" tuple((x, y) for y in range(3))\n",
" for x in range(3)\n",
")\n",
"cols"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(('x', ' ', 'o'), ('x', 'x', 'x'), ('o', ' ', 'o'))"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"row_values = tuple(\n",
" tuple(\n",
" val\n",
" for pos, val in all_values_with_pos\n",
" if pos in row\n",
" )\n",
" for row in rows\n",
")\n",
"row_values"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(False, True, False)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def check_winner(strip):\n",
" return all(\n",
" strip[0] == item\n",
" for item in strip\n",
" )\n",
"\n",
"tuple(map(check_winner, row_values))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"any(map(check_winner, row_values))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment