Guest User

Untitled

a guest
Dec 13th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. "use strict";
  12.  
  13. var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  14.  
  15. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  16.  
  17. Elevator:
  18. // There is an elevator in a building with M floors.
  19. // This elevator can take a max of X people at a time or max of total weight Y.
  20. // Given that a set of people and their weight and the floor they need to stop,
  21. // how many stops has the elevator taken to serve all the people?
  22. //Consider the elevator serves in "first come first serve" basis and the order for the queue can not be changed.
  23. //
  24. // Example:
  25. // Let Array A be the weight of each person A = [60, 80, 40].
  26. // Let Array B be the floors where each person needs to be dropped off B = [2, 3, 5].
  27. //
  28. // The building has 5 floors, maximum of 2 persons are allowed in the elevator
  29. // at a time with max weight capacity being 200. For this example, the elevator would take total
  30. // of 5 stops in floors: 2, 3, G, 5 and finally G.
  31. "use strict";
  32.  
  33. var A = [50, 80, 40, 120];
  34. var B = [2, 3, 5, 1];
  35. var maxWeight = 200;
  36. var maxCapacity = 2;
  37.  
  38. var journeys = undefined;
  39.  
  40. // are there more people
  41. // if yes, add first person to lift
  42.  
  43. // up to maxCapacity
  44. // if there are still more people, add next person IF maxWeight is not met
  45.  
  46. // if lift is empty go back to ground floor
  47. // repeat
  48. var Persons = [{ weight: 50, target: 2 }, { weight: 80, target: 3 }, { weight: 40, target: 5 }, { weight: 120, target: 1 }];
  49.  
  50. var Lift = (function () {
  51. function Lift() {
  52. _classCallCheck(this, Lift);
  53.  
  54. this.maxWeight = 200;
  55. this.maxCapacity = 2;
  56. this.currentCapacity = 0;
  57. this.currentWeight = 0;
  58. this.totalMoves = 0;
  59. this.currentFloor = 0;
  60. this.peopleInTheLift = [];
  61. }
  62.  
  63. _createClass(Lift, [{
  64. key: "onBoardPerson",
  65. value: function onBoardPerson(person) {
  66. if (person.weight + this.currentWeight <= this.maxWeight) {
  67. this.peopleInTheLift.push(person);
  68. this.currentCapacity += 1;
  69. this.currentWeight += person.weight;
  70. }
  71. }
  72. }, {
  73. key: "dropOffPeople",
  74. value: function dropOffPeople() {
  75. var _this = this;
  76.  
  77. this.peopleInTheLift.forEach(function (person) {
  78. if (_this.currentFloor !== person.target) {
  79. _this.totalMoves++;
  80. _this.currentFloor = person.target;
  81. _this.currentWeight = _this.currentWeight - person.weight;
  82. _this.currentCapacity = _this.currentCapacity - 1;
  83. _this.peopleInTheLift = _this.peopleInTheLift.slice(1, _this.peopleInTheLift.length);
  84. } else {
  85. _this.currentWeight = _this.currentWeight - person.weight;
  86. _this.currentCapacity = _this.currentCapacity - 1;
  87. _this.peopleInTheLift = _this.peopleInTheLift.slice(1, _this.peopleInTheLift.length);
  88. }
  89. });
  90.  
  91. this.currentFloor = 0;
  92. this.totalMoves++;
  93. }
  94. }, {
  95. key: "countMove",
  96. value: function countMove() {
  97. return this.totalMoves;
  98. }
  99. }]);
  100.  
  101. return Lift;
  102. })();
  103. </script>
  104.  
  105.  
  106.  
  107. <script id="jsbin-source-javascript" type="text/javascript">Elevator:
  108. // There is an elevator in a building with M floors.
  109. // This elevator can take a max of X people at a time or max of total weight Y.
  110. // Given that a set of people and their weight and the floor they need to stop,
  111. // how many stops has the elevator taken to serve all the people?
  112. //Consider the elevator serves in "first come first serve" basis and the order for the queue can not be changed.
  113. //
  114. // Example:
  115. // Let Array A be the weight of each person A = [60, 80, 40].
  116. // Let Array B be the floors where each person needs to be dropped off B = [2, 3, 5].
  117. //
  118. // The building has 5 floors, maximum of 2 persons are allowed in the elevator
  119. // at a time with max weight capacity being 200. For this example, the elevator would take total
  120. // of 5 stops in floors: 2, 3, G, 5 and finally G.
  121. "use strict";
  122.  
  123. const A = [50, 80, 40, 120];
  124. const B = [2, 3, 5, 1];
  125. const maxWeight = 200;
  126. const maxCapacity = 2;
  127.  
  128.  
  129. let journeys;
  130.  
  131.  
  132. // are there more people
  133. // if yes, add first person to lift
  134.  
  135. // up to maxCapacity
  136. // if there are still more people, add next person IF maxWeight is not met
  137.  
  138. // if lift is empty go back to ground floor
  139. // repeat
  140. const Persons = [
  141. {weight: 50, target: 2},
  142. {weight: 80, target: 3},
  143. {weight: 40, target: 5},
  144. {weight: 120, target: 1},
  145. ];
  146.  
  147.  
  148. class Lift {
  149. constructor() {
  150. this.maxWeight = 200;
  151. this.maxCapacity = 2;
  152. this.currentCapacity = 0;
  153. this.currentWeight = 0;
  154. this.totalMoves = 0;
  155. this.currentFloor = 0;
  156. this.peopleInTheLift = [];
  157. }
  158.  
  159. onBoardPerson(person) {
  160. if ((person.weight + this.currentWeight) <= this.maxWeight ) {
  161. this.peopleInTheLift.push(person);
  162. this.currentCapacity += 1;
  163. this.currentWeight += person.weight;
  164. }
  165. }
  166.  
  167. dropOffPeople() {
  168.  
  169. this.peopleInTheLift.forEach(person => {
  170. if (this.currentFloor !== person.target) {
  171. this.totalMoves ++;
  172. this.currentFloor = person.target;
  173. this.currentWeight = this.currentWeight - person.weight;
  174. this.currentCapacity = this.currentCapacity - 1;
  175. this.peopleInTheLift = this.peopleInTheLift.slice(1, this.peopleInTheLift.length);
  176. } else {
  177. this.currentWeight = this.currentWeight - person.weight;
  178. this.currentCapacity = this.currentCapacity - 1;
  179. this.peopleInTheLift = this.peopleInTheLift.slice(1, this.peopleInTheLift.length);
  180. }
  181. });
  182.  
  183. this.currentFloor = 0;
  184. this.totalMoves ++;
  185.  
  186. }
  187.  
  188. countMove() {
  189. return this.totalMoves;
  190. }
  191.  
  192. }
  193.  
  194.  
  195.  
  196.  
  197.  
  198. </script></body>
  199. </html>
Add Comment
Please, Sign In to add comment