Guest User

Untitled

a guest
Nov 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {},
  7. "outputs": [
  8. {
  9. "name": "stdout",
  10. "output_type": "stream",
  11. "text": [
  12. "3 3\n"
  13. ]
  14. }
  15. ],
  16. "source": [
  17. "#user type the matrix order\n",
  18. "n, m = map(int, input().split())"
  19. ]
  20. },
  21. {
  22. "cell_type": "code",
  23. "execution_count": 2,
  24. "metadata": {},
  25. "outputs": [
  26. {
  27. "name": "stdout",
  28. "output_type": "stream",
  29. "text": [
  30. "1\n",
  31. "2\n",
  32. "3\n",
  33. "4\n",
  34. "5\n",
  35. "0\n",
  36. "0\n",
  37. "0\n",
  38. "0\n"
  39. ]
  40. }
  41. ],
  42. "source": [
  43. "mat = {}\n",
  44. "\n",
  45. "for l in range(n):\n",
  46. " for c in range(m):\n",
  47. " value = float(input())\n",
  48. " if value != 0:\n",
  49. " mat[l,c] = value"
  50. ]
  51. },
  52. {
  53. "cell_type": "code",
  54. "execution_count": 3,
  55. "metadata": {},
  56. "outputs": [
  57. {
  58. "name": "stdout",
  59. "output_type": "stream",
  60. "text": [
  61. "5\n"
  62. ]
  63. }
  64. ],
  65. "source": [
  66. "print(len(mat))"
  67. ]
  68. },
  69. {
  70. "cell_type": "code",
  71. "execution_count": 4,
  72. "metadata": {},
  73. "outputs": [
  74. {
  75. "name": "stdout",
  76. "output_type": "stream",
  77. "text": [
  78. "{(0, 1): 2.0, (1, 0): 4.0, (0, 0): 1.0, (0, 2): 3.0, (1, 1): 5.0}\n"
  79. ]
  80. }
  81. ],
  82. "source": [
  83. "print(mat)"
  84. ]
  85. },
  86. {
  87. "cell_type": "code",
  88. "execution_count": 5,
  89. "metadata": {},
  90. "outputs": [],
  91. "source": [
  92. "def makeSparseMatrix(n, m):\n",
  93. " mat = {}\n",
  94. "\n",
  95. " for l in range(n):\n",
  96. " for c in range(m):\n",
  97. " value = float(input())\n",
  98. " if value != 0:\n",
  99. " mat[l,c] = value\n",
  100. " return mat"
  101. ]
  102. },
  103. {
  104. "cell_type": "code",
  105. "execution_count": 6,
  106. "metadata": {},
  107. "outputs": [
  108. {
  109. "name": "stdout",
  110. "output_type": "stream",
  111. "text": [
  112. "1\n",
  113. "0\n",
  114. "0\n",
  115. "0\n",
  116. "0\n",
  117. "0\n",
  118. "0\n",
  119. "0\n",
  120. "0\n"
  121. ]
  122. }
  123. ],
  124. "source": [
  125. "mat1 = makeSparseMatrix(3,3)"
  126. ]
  127. },
  128. {
  129. "cell_type": "code",
  130. "execution_count": 7,
  131. "metadata": {},
  132. "outputs": [
  133. {
  134. "name": "stdout",
  135. "output_type": "stream",
  136. "text": [
  137. "1\n",
  138. "2\n",
  139. "0\n",
  140. "0\n",
  141. "0\n",
  142. "0\n",
  143. "0\n",
  144. "0\n",
  145. "0\n"
  146. ]
  147. }
  148. ],
  149. "source": [
  150. "mat2 = makeSparseMatrix(3,3)"
  151. ]
  152. },
  153. {
  154. "cell_type": "code",
  155. "execution_count": 8,
  156. "metadata": {},
  157. "outputs": [
  158. {
  159. "name": "stdout",
  160. "output_type": "stream",
  161. "text": [
  162. "{(0, 0): 1.0} {(0, 1): 2.0, (0, 0): 1.0}\n"
  163. ]
  164. }
  165. ],
  166. "source": [
  167. "print(mat1, mat2)"
  168. ]
  169. },
  170. {
  171. "cell_type": "code",
  172. "execution_count": 12,
  173. "metadata": {},
  174. "outputs": [],
  175. "source": [
  176. "matSumKeys = list(set(list(mat1.keys())+list(mat2.keys())))"
  177. ]
  178. },
  179. {
  180. "cell_type": "code",
  181. "execution_count": 13,
  182. "metadata": {},
  183. "outputs": [
  184. {
  185. "name": "stdout",
  186. "output_type": "stream",
  187. "text": [
  188. "[(0, 1), (0, 0)]\n"
  189. ]
  190. }
  191. ],
  192. "source": [
  193. "print(matSumKeys)"
  194. ]
  195. },
  196. {
  197. "cell_type": "code",
  198. "execution_count": 19,
  199. "metadata": {},
  200. "outputs": [],
  201. "source": [
  202. "matSum = {}\n",
  203. "for k in matSumKeys:\n",
  204. " if mat2.__contains__(k) and mat1.__contains__(k):\n",
  205. " matSum[k] = mat1[k]+mat2[k]\n",
  206. " else:\n",
  207. " if mat1.__contains__(k):\n",
  208. " matSum[k] = mat1[k]\n",
  209. " else:\n",
  210. " matSum[k] = mat2[k] "
  211. ]
  212. },
  213. {
  214. "cell_type": "code",
  215. "execution_count": 20,
  216. "metadata": {},
  217. "outputs": [
  218. {
  219. "name": "stdout",
  220. "output_type": "stream",
  221. "text": [
  222. "{(0, 1): 2.0, (0, 0): 2.0}\n"
  223. ]
  224. }
  225. ],
  226. "source": [
  227. "print(matSum)"
  228. ]
  229. },
  230. {
  231. "cell_type": "code",
  232. "execution_count": 21,
  233. "metadata": {},
  234. "outputs": [
  235. {
  236. "name": "stdout",
  237. "output_type": "stream",
  238. "text": [
  239. "2\n"
  240. ]
  241. }
  242. ],
  243. "source": [
  244. "print(len(matSum))"
  245. ]
  246. }
  247. ],
  248. "metadata": {
  249. "kernelspec": {
  250. "display_name": "Python 3",
  251. "language": "python",
  252. "name": "python3"
  253. },
  254. "language_info": {
  255. "codemirror_mode": {
  256. "name": "ipython",
  257. "version": 3
  258. },
  259. "file_extension": ".py",
  260. "mimetype": "text/x-python",
  261. "name": "python",
  262. "nbconvert_exporter": "python",
  263. "pygments_lexer": "ipython3",
  264. "version": "3.5.2"
  265. }
  266. },
  267. "nbformat": 4,
  268. "nbformat_minor": 2
  269. }
Add Comment
Please, Sign In to add comment