Skip to content

Instantly share code, notes, and snippets.

@SilvaEmerson
Last active November 22, 2017 02:11
Show Gist options
  • Save SilvaEmerson/80f4107336df01b7d4410a22a4d207ef to your computer and use it in GitHub Desktop.
Save SilvaEmerson/80f4107336df01b7d4410a22a4d207ef 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": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3 3\n"
]
}
],
"source": [
"#user type the matrix order\n",
"n, m = map(int, input().split())"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"0\n",
"0\n",
"0\n",
"0\n"
]
}
],
"source": [
"mat = {}\n",
"\n",
"for l in range(n):\n",
" for c in range(m):\n",
" value = float(input())\n",
" if value != 0:\n",
" mat[l,c] = value"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"print(len(mat))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{(0, 1): 2.0, (1, 0): 4.0, (0, 0): 1.0, (0, 2): 3.0, (1, 1): 5.0}\n"
]
}
],
"source": [
"print(mat)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def makeSparseMatrix(n, m):\n",
" mat = {}\n",
"\n",
" for l in range(n):\n",
" for c in range(m):\n",
" value = float(input())\n",
" if value != 0:\n",
" mat[l,c] = value\n",
" return mat"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"0\n",
"0\n",
"0\n",
"0\n",
"0\n",
"0\n",
"0\n",
"0\n"
]
}
],
"source": [
"mat1 = makeSparseMatrix(3,3)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"0\n",
"0\n",
"0\n",
"0\n",
"0\n",
"0\n",
"0\n"
]
}
],
"source": [
"mat2 = makeSparseMatrix(3,3)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{(0, 0): 1.0} {(0, 1): 2.0, (0, 0): 1.0}\n"
]
}
],
"source": [
"print(mat1, mat2)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"matSumKeys = list(set(list(mat1.keys())+list(mat2.keys())))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(0, 1), (0, 0)]\n"
]
}
],
"source": [
"print(matSumKeys)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"matSum = {}\n",
"for k in matSumKeys:\n",
" if mat2.__contains__(k) and mat1.__contains__(k):\n",
" matSum[k] = mat1[k]+mat2[k]\n",
" else:\n",
" if mat1.__contains__(k):\n",
" matSum[k] = mat1[k]\n",
" else:\n",
" matSum[k] = mat2[k] "
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{(0, 1): 2.0, (0, 0): 2.0}\n"
]
}
],
"source": [
"print(matSum)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"print(len(matSum))"
]
}
],
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment