main.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. function HotChocolatePool() {
  2. this.scoreboard = [];
  3. this.loopCount =0;
  4. }
  5. HotChocolatePool.prototype.create = function(score) {
  6. this.scoreboard.push(score);
  7. return score;
  8. }
  9. HotChocolatePool.prototype.combine = function(a, b) {
  10. var result = [],
  11. resultStr = "" +(a +b);
  12. for (var i =0, len = resultStr.length; i < len; ++i) {
  13. var child = this.create(parseInt(resultStr.charAt(i)));
  14. result.push(child);
  15. }
  16. return result;
  17. }
  18. HotChocolatePool.prototype.getAtIndex = function(i) {
  19. return this.scoreboard[i];
  20. }
  21. function simPool(a, b, stopCondition) {
  22. var pool = new HotChocolatePool(),
  23. a = pool.create(a),
  24. aIndex = 0,
  25. b = pool.create(b),
  26. bIndex = 1;
  27. while (stopCondition(pool)) {
  28. pool.combine(a, b);
  29. a = pool.getAtIndex(aIndex = ((aIndex +a +1) % pool.scoreboard.length))
  30. b = pool.getAtIndex(bIndex = ((bIndex +b +1) % pool.scoreboard.length));
  31. ++pool.loopCount;
  32. }
  33. return pool;
  34. }
  35. function ex1(count) {
  36. var pool = simPool(3, 7, p => p.scoreboard.length < 10 +count),
  37. result = pool.scoreboard.slice(count, count +10);
  38. console.log(result);
  39. }
  40. function ex2(arr) {
  41. var pool = simPool(3, 7, p => {
  42. var sb = p.scoreboard;
  43. if (sb.length < arr.length)
  44. return true;
  45. for (var i =Math.max(0, sb.length -arr.length -5), lenI = sb.length -arr.length; i < lenI; ++i) {
  46. var found = true;
  47. for (var j =0, lenJ =arr.length; j < lenJ && found; ++j)
  48. if (arr[j] != sb[i +j])
  49. found = false;
  50. if (found) {
  51. console.log("Found at index ", i);
  52. return false;
  53. }
  54. }
  55. return true;
  56. });
  57. }
  58. ex1(430971);
  59. ex2([4, 3, 0, 9, 7, 1]);