PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Section 3/Notebooks/Numbers in List.ipynb

https://gitlab.com/TravBus/CS_101
Jupyter | 100 lines | 100 code | 0 blank | 0 comment | 0 complexity | 6bcb6f3ab44b18f72dc7f5cdb6ff69c0 MD5 | raw file
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": null,
  6. "metadata": {
  7. "collapsed": true
  8. },
  9. "outputs": [],
  10. "source": [
  11. "# Numbers in lists by SeanMc from forums\n",
  12. "# define a procedure that takes in a string of numbers from 1-9 and\n",
  13. "# outputs a list with the following parameters:\n",
  14. "# Every number in the string should be inserted into the list.\n",
  15. "\n",
  16. "# If a number x in the string is less than or equal \n",
  17. "# to the preceding number y, the number x should be inserted \n",
  18. "# into a sublist. Continue adding the following numbers to the \n",
  19. "# sublist until reaching a number z that\n",
  20. "# is greater than the number y. \n",
  21. "# Then add this number z to the normal list and continue."
  22. ]
  23. },
  24. {
  25. "cell_type": "code",
  26. "execution_count": 58,
  27. "metadata": {
  28. "collapsed": false
  29. },
  30. "outputs": [
  31. {
  32. "name": "stdout",
  33. "output_type": "stream",
  34. "text": [
  35. "[4, 5, [5, 5, 3, 2, 1, 2, 3, 2], 6, [6]]\n"
  36. ]
  37. }
  38. ],
  39. "source": [
  40. "myString = '455532123266'\n",
  41. "#result = [4, 5, [5, 5, 3, 2, 1, 2, 3, 2], 6, [6]]\n",
  42. "\n",
  43. "def numbers_in_lists(string):\n",
  44. " limit = len(string)\n",
  45. " i = 0\n",
  46. " z = int(string[0])\n",
  47. " blist = [int(string[0])]\n",
  48. " slist = []\n",
  49. " while i+1 < limit:\n",
  50. " if int(string[i+1]) <= z:\n",
  51. " slist.append(int(string[i+1]))\n",
  52. " i = i + 1\n",
  53. " else:\n",
  54. " if len(slist) > 0:\n",
  55. " blist.append(slist)\n",
  56. " slist = []\n",
  57. " else:\n",
  58. " z = int(string[i + 1])\n",
  59. " blist.append(int(string[i+1]))\n",
  60. " i = i + 1\n",
  61. " if len(slist) > 0:\n",
  62. " blist.append(slist)\n",
  63. " return blist\n",
  64. " \n",
  65. "print(numbers_in_lists(myString))\n",
  66. " "
  67. ]
  68. },
  69. {
  70. "cell_type": "code",
  71. "execution_count": null,
  72. "metadata": {
  73. "collapsed": true
  74. },
  75. "outputs": [],
  76. "source": []
  77. }
  78. ],
  79. "metadata": {
  80. "kernelspec": {
  81. "display_name": "Python 3",
  82. "language": "python",
  83. "name": "python3"
  84. },
  85. "language_info": {
  86. "codemirror_mode": {
  87. "name": "ipython",
  88. "version": 3
  89. },
  90. "file_extension": ".py",
  91. "mimetype": "text/x-python",
  92. "name": "python",
  93. "nbconvert_exporter": "python",
  94. "pygments_lexer": "ipython3",
  95. "version": "3.5.2"
  96. }
  97. },
  98. "nbformat": 4,
  99. "nbformat_minor": 0
  100. }