Created
December 10, 2018 15:16
-
-
Save bemorr/6e8df85448a242f1a1a649232a173434 to your computer and use it in GitHub Desktop.
Rain water over buildings - Python3 - Analyst test
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": [ | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# Rain water over buildings - Python3 - Analyst test" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "**Question:**\n\nIt is rainy season in the city. What vol. of water will be held above adjoining buildings of `h` height, where:\n\n**Assumptions:**\n\n1) Water does not leak to the front or rear of the buildings.\n\n2) Water 'leaks' from all buildings if, at n index, all preceding and subsequent building heights decline sequentially resulting in 0 cubic metres of water being retained above all buildings.\n\n3) At the end of the row of buildings or, technically speaking, above i[0] and i[-1], no water is held due to an absensce of adjoing buildings to both sides." | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2018-12-10T13:10:08.625823Z", | |
"start_time": "2018-12-10T13:10:08.557901Z" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "h = [9,8,7,8,9,5,6]\n#h1 = [4,5,6,6,6,5,1]\n#h2 = [4,5,8,6,6,8,9,7,6,8,5,1]\n#h3 = [6,5,7,9,9,3,8,6,5,3,7,9,9,3,7,3,6,5,5,3,7,8,9,9,3]\n\n\ndef cubicM(h): \n \n l = len(h)\n hmax = max(h)\n hr = list(reversed(h))\n if [x for x in h if h.count(x) > 1]:\n maxpeaks = max(set([x for x in h if h.count(x) > 1]))\n else: None\n firstmaxindex = h.index(maxpeaks)\n lastmaxindex = (len(h)-1)-hr.index(maxpeaks)\n peaksrange = h[firstmaxindex:lastmaxindex+1]\n\n #keep count of cubic metres of water held above building with:\n \n cubcount = 0\n \n #Logic to calculate the amount of water held over the building in the sub-retaining range:\n \n if maxpeaks is not None:\n for i in peaksrange:\n if maxpeaks - i > 0:\n cubcount+= maxpeaks - i\n \n #where a sub-retaining range is found, creating 2 sub lists list from the remaining to the left and right:\n \n ltarr = h[:(firstmaxindex)]\n ltlen = len(ltarr)\n rtarr = h[lastmaxindex+1:]\n revrtarr =list(reversed(rtarr))\n rtlen = len(rtarr)\n \n #logic to calculate the amount of water held over buildings before peak range:\n \n if ltarr:\n a = 0\n while a < ltlen-1:\n tmpmax = 0\n if ltarr[a] < tmpmax:\n cubcount+=ltarr[a] - tmpmax\n a+=1\n elif ltarr[a] > tmpmax: \n tmpmax = ltarr[a]\n a+=1\n else:\n a+=1\n \n #logic to calculate the amount of water held over buildings after peak range:\n \n elif rtarr:\n b = 0\n tmpmax = revrtarr[0]\n while b < rtlen:\n if revrtarr[b] < tmpmax:\n cubcount+=tmpmax - revrtarr[b]\n b+=1\n elif rtarr[b] > tmpmax: \n tmpmax = revrtarr[b]\n b+=1\n else:\n b+=1 \n \n #where no sub-retaining range is found in list, at the tallest building create sub list to the left: \n \n elif maxpeaks is None:\n x = 0 \n arr1 = h[0:h.index(hmax)]\n tmpmax = arr1[0]\n while x < len(arr1)-1:\n if arr1[x] < tmpmax:\n cubcount+=arr1[x] - tmpmax\n x+=1\n elif arr1[x] > tmpmax: \n tmpmax = arr1[x]\n x+=1\n else:\n x+=1 \n #...and to the right:\n \n arr2 = h[h.index(hmax):]\n revarr2 = list(reversed(arr2))\n tmpmax = revarr2[0]\n while x in len(revarr2) -1:\n if revarr2[x] < tmpmax:\n cubcount+=revarr2[x] - tmpmax\n x+=1\n elif revarr2[x] > tmpmax: \n tmpmax = revarr2[x]\n x+=1\n else:\n x+=1\n return(cubcount)\n\nprint(cubicM(h))", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "5\n" | |
} | |
] | |
} | |
], | |
"metadata": { | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/29ea4a8fc7964ee19a4390c9a92afcad" | |
}, | |
"gist": { | |
"id": "29ea4a8fc7964ee19a4390c9a92afcad", | |
"data": { | |
"description": "Rain water over buildings - Python3 - Analyst test", | |
"public": true | |
} | |
}, | |
"hide_input": false, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3", | |
"language": "python" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.6.6", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"base_numbering": 1, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": true | |
}, | |
"varInspector": { | |
"window_display": false, | |
"cols": { | |
"lenName": 16, | |
"lenType": 16, | |
"lenVar": 40 | |
}, | |
"kernels_config": { | |
"python": { | |
"library": "var_list.py", | |
"delete_cmd_prefix": "del ", | |
"delete_cmd_postfix": "", | |
"varRefreshCmd": "print(var_dic_list())" | |
}, | |
"r": { | |
"library": "var_list.r", | |
"delete_cmd_prefix": "rm(", | |
"delete_cmd_postfix": ") ", | |
"varRefreshCmd": "cat(var_dic_list()) " | |
} | |
}, | |
"types_to_exclude": [ | |
"module", | |
"function", | |
"builtin_function_or_method", | |
"instance", | |
"_Feature" | |
] | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment