Guest User

Untitled

a guest
May 27th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Finding highest amount of diamonds\n",
  8. "## Job interview exercise for Medecide"
  9. ]
  10. },
  11. {
  12. "cell_type": "markdown",
  13. "metadata": {},
  14. "source": [
  15. "The maximum amount of diamonds are determined by a recursive function as depicted in \"maxdiamonds\" function.<br>\n",
  16. "The function also keeps documentation of the amount of diamonds collected in a numpy array \"path\" for backtracking through the recursive path later."
  17. ]
  18. },
  19. {
  20. "cell_type": "code",
  21. "execution_count": 25,
  22. "metadata": {},
  23. "outputs": [],
  24. "source": [
  25. "import numpy as np\n",
  26. "def maxdiamonds(A, m, n):\n",
  27. " if n > 2 or m > 2:\n",
  28. " return -1\n",
  29. " elif (m == 2 and n == 2):\n",
  30. " return A[m][n] \n",
  31. " else:\n",
  32. " temp = max( maxdiamonds(A, m+1, n),maxdiamonds(A, m, n+1))\n",
  33. " path[m,n] = A[m][n] + temp #documentation for backtracking the path\n",
  34. " return A[m][n] + temp"
  35. ]
  36. },
  37. {
  38. "cell_type": "code",
  39. "execution_count": 26,
  40. "metadata": {},
  41. "outputs": [
  42. {
  43. "name": "stdout",
  44. "output_type": "stream",
  45. "text": [
  46. "Number of Diamonds collected : 30\n"
  47. ]
  48. }
  49. ],
  50. "source": [
  51. "A = [[1, 2, 3],[15, 4, 7],[0, 0, 3]]\n",
  52. "path = np.zeros((3,3))\n",
  53. "print('Number of Diamonds collected :',maxdiamonds(A, 0, 0))\n",
  54. " "
  55. ]
  56. },
  57. {
  58. "cell_type": "markdown",
  59. "metadata": {},
  60. "source": [
  61. "A greedy algorithm could be easily used in order to find the path with the highest amount of diamonds."
  62. ]
  63. },
  64. {
  65. "cell_type": "code",
  66. "execution_count": 27,
  67. "metadata": {},
  68. "outputs": [
  69. {
  70. "name": "stdout",
  71. "output_type": "stream",
  72. "text": [
  73. "Path of collected diamonds:\n",
  74. "0 0\n",
  75. "1 0\n",
  76. "1 1\n",
  77. "1 2\n",
  78. "2 2\n"
  79. ]
  80. }
  81. ],
  82. "source": [
  83. "i,j=0,0\n",
  84. "print('Path of collected diamonds:')\n",
  85. "print(i,j)\n",
  86. "while (i!=2) or (j!=2):\n",
  87. " if i==2:\n",
  88. " j+=1\n",
  89. " print(i,j)\n",
  90. " continue\n",
  91. " if j==2:\n",
  92. " i+=1\n",
  93. " print(i,j)\n",
  94. " continue\n",
  95. " if (path[i,j]-path[i+1,j])<(path[i,j]-path[i,j+1]):\n",
  96. " i+=1\n",
  97. " print(i,j)\n",
  98. " else:\n",
  99. " j+=1\n",
  100. " print(i,j)"
  101. ]
  102. },
  103. {
  104. "cell_type": "markdown",
  105. "metadata": {},
  106. "source": [
  107. "どうもありがとうございます!"
  108. ]
  109. }
  110. ],
  111. "metadata": {
  112. "kernelspec": {
  113. "display_name": "Python (myenv)",
  114. "language": "python",
  115. "name": "myenv"
  116. },
  117. "language_info": {
  118. "codemirror_mode": {
  119. "name": "ipython",
  120. "version": 3
  121. },
  122. "file_extension": ".py",
  123. "mimetype": "text/x-python",
  124. "name": "python",
  125. "nbconvert_exporter": "python",
  126. "pygments_lexer": "ipython3",
  127. "version": "3.5.4"
  128. }
  129. },
  130. "nbformat": 4,
  131. "nbformat_minor": 2
  132. }
Add Comment
Please, Sign In to add comment