Created
May 12, 2020 04:55
-
-
Save mvanorder/297c110c096e154ff1022a7836f45dcf to your computer and use it in GitHub Desktop.
diagonal list from a matrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"l1 = [[1, 2, 3 ],\n", | |
" [5, 7, 13],\n", | |
" [17, 19, 23]]\n", | |
"\n", | |
"l2 = [[1, 2, 3 ],\n", | |
" [5, 7, 13],\n", | |
" [17, 19, 23],\n", | |
" [29, 31]]\n", | |
"\n", | |
"l3 = [[1, 2, 3 ],\n", | |
" [5, 7, 13],\n", | |
" [17, 19, 23],\n", | |
" [29, 31, 37]]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## diag\n", | |
"\n", | |
"**Arguements:**\n", | |
"* matrix: A 2 dimentional list\n", | |
"\n", | |
"**return:** Boolean of if the number of rows and columns are equal\n", | |
"\n", | |
"**Expected output:**\n", | |
"\n", | |
"```\n", | |
"l1: (3, 3)\n", | |
"Rows are not a uniform lenth\n", | |
"l3: (4, 3)\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"l1: (3, 3)\n", | |
"Rows are not a uniform lenth\n", | |
"l3: (4, 3)\n" | |
] | |
} | |
], | |
"source": [ | |
"def shape(matrix):\n", | |
" if len(row_lens := set([len(l) for l in matrix])) > 1:\n", | |
" raise ValueError(\"Rows are not a uniform lenth\")\n", | |
" return len(matrix), row_lens.pop()\n", | |
" \n", | |
"print(f'l1: {shape(l1)}')\n", | |
"\n", | |
"try:\n", | |
" print(f'l2: {shape(l2)}')\n", | |
"except ValueError as e:\n", | |
" print(e)\n", | |
"\n", | |
"print(f'l3: {shape(l3)}')\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## diag\n", | |
"\n", | |
"**Arguements:**\n", | |
"* matrix: A square 2 dimentional list\n", | |
"\n", | |
"**return:** list of matrix\\[0\\]\\[0\\] through matrix\\[n\\]\\[n\\]\n", | |
"\n", | |
"**Expected output:**\n", | |
"\n", | |
"```\n", | |
"l1: [1, 7, 23]\n", | |
"l1 reversed: [17, 7, 3]\n", | |
"l2: ValueError Rows are not a uniform lenth\n", | |
"l3: ValueError Matrix is now a square.\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"l1: [1, 7, 23]\n", | |
"l1 reversed: [17, 7, 3]\n", | |
"l2: ValueError Rows are not a uniform lenth\n", | |
"l3: ValueError Matrix is now a square.\n" | |
] | |
} | |
], | |
"source": [ | |
"def diag(matrix):\n", | |
" if (matrix_shape := shape(matrix))[0] != matrix_shape[1]:\n", | |
" raise ValueError(\"Matrix is now a square.\")\n", | |
"\n", | |
" return [row[index] for index, row in enumerate(matrix)]\n", | |
"\n", | |
"print(f'l1: {diag(l1)}')\n", | |
"print(f'l1 reversed: {diag(l1[::-1])}')\n", | |
"\n", | |
"try:\n", | |
" print(f'l2: {diag(l2)}')\n", | |
"except ValueError as e:\n", | |
" print(f'l2: ValueError {e}')\n", | |
"\n", | |
"try:\n", | |
" print(f'l3: {diag(l3)}')\n", | |
"except ValueError as e:\n", | |
" print(f'l3: ValueError {e}')\n" | |
] | |
} | |
], | |
"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.8.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment